How to create an event in AWS Cloudwatch to trigger a Lambda Function
Amazon CloudWatch Events describe changes in AWS resources. We can match events and route them to one or more target functions. CloudWatch Events come to know about operational changes as they occur, e.g. if a defined resource in the Cloudwatch Rule has been created then the rule would come to know about it and in return it will trigger a target function.
Before we proceed and create an event rule, let's understand basics of it.
An event means there is a change in the AWS Environment.
Rules are used to match the event. Here the events are compared and checked.
Here the events are processed. Targets can include Amazon EC2 instances, AWS Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in targets.
In this article, we will create an event rule which will check for event of EC2 State change and trigger a lambda function. If you are not aware of Lambda Function then click here to create 1 for you. What Lambda does can be your choice, but in this document I have a Lambda which deletes the unused EBS Volumes. I have mentioned the Lambda code in this article. You are free to use your own Lambda Function too.
- AWS Account (Create if you don’t have one).
- Lambda Function.
What will we do?
- Login to AWS.
- Create a Cloudwatch Event Rule to trigger a Lambda Function.
Login to AWS
Click here to go to AWS Login Page.
When we hit the above link, we will see a web page as follows where we are required to login using our login details.
Once you successfully login in to your account, you will see the screen as follows.
Create a Cloudwatch Event to trigger a Lambda Function
To create a Cloudwatch Event Rule, search for cloudwatch and click on the result to go to the Cloudwatch dashboard.
On the Cloudwatch dashboard, click on Rules under Events in the left panel.
Here, click in "Create rule" to create your first rule to trigger a Lambda Function in your region.
Select the "Event Pattern" radio button, under Service Name select "EC2", select "EC2 instance state change notification" for Event Type.
Click on Add Targets to add an existing Lambda Function as a target and click on "Configure details".
Click here to learn to create a Lambda Function fo you. Find the following reference Lambda Code for you.
Lambda Code: Use this code to create a Lambda Function if you dont have 1 in you account.
import boto3 ec2 = boto3.resource('ec2',region_name='eu-west-3') def lambda_handler(event, context): for vol in ec2.volumes.all(): if vol.state=='available': if vol.tags is None: vid=vol.id v=ec2.Volume(vol.id) v.delete() print ('EBS Volume Deleted ' +vid) continue for tag in vol.tags: if tag['Key'] == 'Name': value=tag['Value'] if value != 'DoNNotDel' and vol.state=='available': vid=vol.id v=ec2.Volume(vol.id) v.delete() print ('EBS Volume Deleted ' +vid)
Give a name to the event along with the description and click on "Creare rule".
This will create an Event Rule which will trigger a target Lambda function whenever there is a change in State of EC2 Instances.
In this article, we saw how to create a Cloudwatch Event Rule which indicates the change in the EC2 instance state and triggers the Lambda Function when the rule matches the event. You can select the target of your choice.