SIMON CULL
Full Stack Developer

Automating stopping and starting of AWS EC2 instances using EventBridge and Python

If an organisation runs their EC2 instances outside of business hours they will still be paying for compute, storage and memory even when they are not actually being used. With EC2 instances you are charged by the second, so ensuring these instances are stopped when they are not being used can result in significant savings.

This could be achieved by using an AWS Lambda function triggered using an EventBridge schedule rule. There are two Lambda functions in this project, one will run at 8am to start the instances that are in the ‘Stopped’ instance state, and the other will run at 6pm to stop the instances in the ‘Running’ instance state. These functions will be scheduled to run daily, from Monday to Friday.



Services Used

EC2

Lambda (using Python (Boto3))

IAM

EventBridge

Terraform


Steps Taken

i. The Lambda functions need permission to talk to both CloudWatch and EC2, a custom IAM policy is attached to a role, and the role is assigned to the function, the Lambda Execution Role. In the custom IAM policy, permission is granted to stop and start instances, along with permissions to connect with CloudWatch.

ii. The Lambda functions will use the AWS SDK for Python, Boto3, for programmatic access to features of Amazon EC2. The functions will iterate through every AWS region. For each region, using a boto3 resource filter, the functions will find and list either the stopped or the running instances, depending on which function.

iii. This list of instances can be iterated over and the stopped instaces will be started, and the running instaces will be stopped. This information will be recorded in CloudWatch Logs.

iv. When creating the CloudWatch rule a cron expression can be used for scheduling the event - cron(0 8 * * MON-FRI *) and cron(0 18 * * MON-FRI *)




Custom IAM policy
Lambda function to start instances
Lambda function to stop instances
EventBridge rule using cron expressions for scheduling

Testing

For testing, I altered the timings of the EventBridge rule
The Start instances function was triggered at 14:57
The Stop instances function was triggered at 15:01