It's been surprisingly hard to schedule AWS EC2 instances launches/stops. This repo contains a set of artifacts to make it working in 10 minutes for simple cases when you need to launch/terminate a few instances. The following installation guide describes how to end up having an instance launched on working days at 09:45 and terminating at 10:10. Inspired by https://blog.goodmirek.com/periodically-start-and-stop-ec2-instance-bf25c01e68f1
- Create a new IAM policy
ec2_start_stop_policy
usingec2_start_stop_policy.json
- Create a new IAM role
ec2_start_stop_role
usingec2_start_stop_role.json
- Create a new lambda function
ec2-start
, useec2_start_stop_role
as the default execution role andlambda/ec2-start/index.js
as source code. Put your instance id and region into the source code. - Create a new lambda function, use
ec2_start_stop_role
as the default execution role andlambda/ec2-stop/index.js
as source code. Put your instance id and region into the source code - Create a new rule in CloudWatch rules
ec2-start
. Useec2_start_stop_role
andec2-start
function as a target. Use45 9 ? * MON-FRI *
as a schedule expression - Create a new rule in CloudWatch rules
ec2-stop
. Useec2_start_stop_role
andec2-stop
function as a target. Use10 10 ? * MON-FRI *
as a schedule expression - If you need hibernation instead of termination use
lambda/ec2-hibernate/index.js
at step 4
- make instance ids and region configurable
- make CloudFormation templates to do the job