In this tutorial, we will look at how to create an AWS lambda functionsetup. The scenario that we will use is to detect change in EC2 instance state and then send an email to notify the EC2 instance state change. Following resources are required to create the above.
- EC2 instance
- Lambda function
- Eventbridge
- SNS topic
Below we will see how this setup works.
When an EC2 instance state changes from Stop → Start or Start → Stop, the event is logged in Cloudwatch. The lambda function will monitor the Cloudwatch events (AWS Eventbridge) and once the state change is detected, will trigger the SNS service.
Steps to create AWS Lambda function:
Sign in to the AWS console using https://console.aws.amazon.com and go to the Lambda service by searching for the service in the search bar.
Click on the “Create Function” option.
In the next step, we will give a name for the function and select the Runtime. Then click on “create runtime”. Here we will choose python 3.11 as the runtime.
In the next step, we will add a trigger for the Lambda function. The trigger will be a cloudwatch event. Click on “Add trigger”.
Click on the “Code” tab for the lambda function, and add the below python code. This code will be triggered everytime the EC2 instance state changes.
Add a trigger for the Lambda function
In the trigger configuration, add the following configuration settings.
- Select “Eventbridge (Cloudwatch Events)
- Select “Create a new rule”. Then add a rule name and add a rule description.
- Select rule type as “Event pattern”
- Under event pattern select EC2 and EC2 instance state-change notification
The event pattern preview for the trigger will be shown as below. Click on Add to add the trigger.
In the next screen, we can see that the cloudwatch event is added as trigger.
Add destination for Lambda function
In the next step we will add the destination for the function. Click on Add destination. In the destination configuration, select destination type as “SNS Topic” and select destination as “Notify Me”. This will send email notifications for the root account of this AWS account. You could create any other SNS topic and select that as well for destination.
Test Lambda function and SNS topic with an event trigger
Now, we will test the lambda function and SNS topic with an event trigger. We will stop/start an EC2 instance and check if the lambda function executes and SNS topic sends email.
Here we are starting the EC2 instance.
To check the Lambda function logs, click on Monitor tab in Lambda function, and click on “View Cloudwatch Logs”.
Once the cloudwatch logs are opened, in the log streams, click on the latest log stream. This log stream will have the output of the last run of the lambda function. In the log we can find the output of the python code that was configured in the lambda function.
Now, check the email configured in SNS. You would have received an email from AWS notifications with the below message as the body of the email. The email will contain the message printed as the output of the Lambda function.
{“version”:”1.0″,”timestamp”:”2023-07-30T00:33:52.091Z”,”requestContext”:{“requestId”:”dfe356-d233-976d-baa4-03sdfsdd62ed736″,”functionArn”:”arn:aws:lambda:us-east-1:53485352399393:function:Lambdatst:$LATEST”,”condition”:”Success”,”approximateInvokeCount”:1}, “requestPayload”:{“version”:”0″,”id”:”0cewtf22-3457-4fdd-8bea-952487c7a7a8″,”detail-type”:”EC2 Instance State-change Notification”,”source”:”aws.ec2″,”account”:”53485352399393″,”time”:”2023-07-30T00:32:51Z”,”region”:”us-east-1″,”resources”:[“arn:aws:ec2:us-east-1:53485352399393:instance/i-0767237de229de12″],”detail”:{“instance-id”:”i-0767237de229de12″,”state”:”running”}},”responseContext”:{“statusCode”:200,”executedVersion”:”$LATEST”},”responsePayload”:{“statusCode”: 200, “body”: “{\”message\”: \”Hello, this is a test Lambda function \”}”}}
Conclusion:
In this tutorial, we have seen how to configure Lambda with eventbridge and SNS in order to monitor the EC2 instance state change. This is known as event driven architecture using which in real time an event can be monitored and trigger an action based on specific conditions. This architecture is scalable, resilient and cost efficient. Similar configuration can be made to monitor any other AWS service and trigger the Lambda function accordingly.
Read more on AWS:
AWS for Beginners: What is AWS Lambda & How it Works? – Part 56
Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.