Create A Lambda Function

Create A Lambda Function

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Lambda function and its components.

https://cloudaffaire.com/lambda-function/

In this blog post, we are going to create our 1st Lambda function using Python in AWS console.

Create a lambda function:

Step 1: Login to AWS console and navigate to ‘Lambda’.

Create A Lambda Function

Step 2: Click ‘Create a function’.

Create A Lambda Function

Step 3: Provide a name to your function and select runtime.

Create A Lambda Function

Note: We will use python as runtime throughout this series.

Step 4: Create a role and click ‘Create function’.

Create A Lambda Function

Note: We are providing CloudWatch access to our Lambda function to write logs in CloudWatch.

Our first Lambda function successfully created.

Create A Lambda Function

Observe: If you scroll down in the function code section, you can observe that AWS has created our 1st lambda function with few lines of sample code.

Create A Lambda Function

import json
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}

Next, we are going to manually invoke this lambda function using AWS CLI.

Step 5: Login to EC2 instance and execute below command

aws lambda invoke --function-name myfuction --log-type Tail outputfile.txt

Create A Lambda Function

To view the tail execute

echo <LogResult_from_previous_output> | base64 --decode

Create A Lambda Function

To view the response

Create A Lambda Function

Observe: Our Lambda function executed successfully and as a result, it has returned the status code 200 along with ‘Hello from Lamba!”.

Hope you have enjoyed this article. In the next blog post, we will modify this function and also discuss different aspects of lambda function with example.

To get more details on Lambda, please refer below AWS documentation

https://docs.aws.amazon.com/lambda/index.html

 

Leave a Reply

Close Menu