What we are going to do throughout this project
- Creating IAM policy and Roles for lambda to stop and describe instances
- Set Up AWS Lambda function
- Create a Trigger with Event Rule
- Identify Idle Instances with a python script in the lambda function
- Implement the Shutdown by checking the last time they was an activity in the instance and subtracting it from the current time, if its equal to 1800 seconds (30 mins)
- Test and Monitor your lambda function
To create an AWS Lambda Function that automatically shuts down an EC2 instance when it’s idle for more than 30 minutes, you can use a combination of AWS Lambda , CloudWatch Events, and IAM roles. Here’s a high-level overview of the steps involved:
Head over to your AWS management console and search for IAM . Create an IAM policy:On the pane at the left click policy, at the top right corner click create policy, select json and paste the below JSON code snippet below is the policy in json format