Create A Lambda Function
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Lambda function and its components.
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.
def lambda_handler(event, context):
# TODO implement
'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
To view the tail execute
echo <LogResult_from_previous_output> | base64 --decode
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