Schedule and automate custom start & stop of EC2 and RDS instances, by setting tags.
Cross accounts, cross regions
Set a “master” and “remotes” to use tags in additional accounts
CloudFormation templates
instance-scheduler.template
instance-scheduler-remote.template
Services used
- CloudWatch
- Lambda
- DynamoDB
- IAM
- SNS
- KMS
- EC2 / RDS
Solution
We configure a CloudWatch event that triggers a λ function to start or stop EC2/RDS instances at pre-configured intervals.
- λ checks the current state of each tagged instance against the targeted state defined in the associated schedule in DynamoDB.
- λ records:
- The name of the schedule
- the number of instances associated with that schedule
- the number of running instances (as an optional custom metric in CW)
- Can be multi regions and cross accounts.
DynamoDB tables
ConfigTable
- 1 config item (stores cross_account_roles, regions tagnames)
- schedule items (includes the periods used, time zone)
- period items (store weekdays, begin & end time)
StateTable
- states every single tagged resource (either EC2 or RDS)
Adding a tag
- Make sure the account is added to the master account (CloudFormation parameters)
- Make sure the region is added/active (CloudFormation parameters)
- Duplicate and set a new Period
- Create a new schedule using the new Period