AWS Lambda : Create your first function

AWS Lambda is one of the compute service provided by AWS. Let’s get started by a brief intro and then some hands on lab to create a basic function in Python 3.7 runtime.

What is AWS Lambda ?

AWS Lambda is a compute service that allows to create a function in one of the supported runtime (e.g. Java,Python, .Net, Node.js and others), configure the function, and the code is executed without the need to provision servers —paying only for the resources used during the execution. In this article we are going to create an AWS Lambda function and test the same from AWS console.

To create and test a Lambda function from scratch we would require to:

  • Create an IAM role with basic permissions for lambda to push logs to CloudWatch
  • Write the function code and create function from AWS Console
  • Test your lambda function code

CREATE IAM ROLE

  • Navigate to AWS IAM console
  • Click Create Role .
  • Select Lambda and Click Next: Permissions
  • Enter ‘AWSLambdaBasicExecutionRole’ in the search box and select the ‘AWSLambdaBasicExecutionRole’ policy.
  • Navigate through defaults and in Review Page enter the name of role as ‘LambdaBasicExecutionRole’ as shown below. Click Create Role.

WRITE AWS LAMBDA FUNCTION CODE

  • Navigate to AWS Lambda Console and Click Create Function.
  • On Create Function Page, Select the following Options

Option to create your function = Author From Scratch

Function Name = MyFirstLambdaFunction

Runtime = Python 3.7

Execution role = Use an existing role

Existing role  = LambdaBasicExecutionRole

In the Inline AWS Cloud9 Code Editor, You will see default lambda_function.py selected and opened in the code editor. At the time you create a Lambda function, you specify a handler, which is a function in your code, that AWS Lambda can invoke when the service executes your code. You can see this in the Handler field. In your case, it should show as lambda_function.lambda_handler where lambda_function is the python file and lambda_handler is the function in the python file. Next, Paste the code below in code editor and Click the Save button on the top-right corner.

import json

import os

import logging

logger = logging.getLogger()

logger.setLevel(logging.INFO)

def lambda_handler(event, context):

    logger.info('Printing ENVIRONMENT VARIABLES')

    logger.info(os.environ)

    logger.info('Printing EVENT payload')

    logger.info(event)

    return {

        'statusCode': 200,

        'body': json.dumps('This is a Success !!!')     }

TEST YOUR AWS LAMBDA FUNCTION

  • On the top right of your Lambda Function page, just next to save, Click the Test button to launch and Configure test event pop-up and save as name LambdaTestEvent as shown below.

Once Test Event is created Click the Test button on Top right of Lambda Function page. This will pass the Event payload we just created to the lambda function and will be printed to the console as shown below. You can see that In Log Output section ENVIRONMENT VARIABLES and EVENT payload we passed to function has been printed to console. This lambda function has also returned a result with status code 200 OK result.

Isn’t it awesome feeling to create and execute a function without worry about the underlying infrastructure. I hope you like this article. Feel free to leave some feedback 🙂

Have a look at all the published content on our Library page

If getting bored of reading , watch it on our youtube channel to have a hands on.