AWS Lambda automation to get the EC2 and RDS details on Email
We have received a requirement from my manager to obtain details of the following things via email without logging into the AWS console: the count of EC2, RDS instances details including start and stopped instances and the instances that were created in the past 7 days. We may need to make further modifications in the future.
There may be multiple ways to accomplish the same task, but we have created a Python script that uses AWS Lambda and CloudWatch events, along with Simple Notification Service (SNS), to send an email on a daily basis
Steps that need to be performed:
Step 1 Create an SNS topic and subscription
Step 2 Create a role with the required resources and access
Step 3 Create a Lambda function and attach a role created above
Step 4 Create a Lambda trigger which will be sending the email
Step 1 Create an SNS topic and subscription.
Search for SNS and Click on create a topic
Select the type as standard and provide the name
Create a subscription
Select protocol as Email
Enter the email address under Endpoint and click on create subscription
Now we will get an email click on that email and click on the subscription
check the status will be in the confirmed state
Copy the ARN (Amazon Resource Name). This will be needed to be changed in the below python code.
Step 2 Create a role with the required resources and access
Search for IAM service
Click on IAM roles
Click on Create role
Select lambda and click on next
Provide the following permission
Enter the role
Click on create role the same will use in the lambda below
Step 3 Create a Lambda function and attach a role created above
Click on create a function
and provide the required details like name, language, and the role which we created in the above step2
Increase the timeout for the lambda function Click on configuration edit and update the time
Paste the below code and modify the ARN which we get in the above step
Step 4 Create a Lambda trigger which will be sending the email
We need to create a trigger so that we can receive an email on a daily basis
Search for cloudwatch events
Now provide the name of the event and enter the schedule expression
Click on Add button
Now you will see the trigger on the lambda as shown below
Sample output of the email
These are the number of EC2 running instances
These are the number of EC2 Stopped instances
Sample for RDS running instances email
Successfully got the email Hurray!!!