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’.
Step 2: Click ‘Create a function’.
Step 3: Provide a name to your function and select runtime.
Note: We will use python as runtime throughout this series.
Step 4: Create a role and click ‘Create function’.
Note: We are providing CloudWatch access to our Lambda function to write logs in CloudWatch.
Our first Lambda function successfully created.
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.
1 2 3 4 5 6 7 |
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
1 |
aws lambda invoke --function-name myfuction --log-type Tail outputfile.txt |
To view the tail execute
1 |
echo |
To view the response
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