CloudWatch Events is an event driven service that allows you to
- Monitoring particular services with event types and to trigger actions to chosen target
- Use crontab like function to schedule desired actions to chosen target
Schedule function to power off instance every weekday at 1900 CST
- Get the instance id that will be power off
- Configure CloudWatch Events
- Choose Schedule function
- Choose and set crontab expression
- Configure function / action to perform and the target
Navigate to CloudWatch Events Rules
Event Pattern: For particular service and event function
Schedule: for actions to perform regularly. Choose this option for this post.
For Schedule, we can use a fixed rate of Minutes, Hours or Days. If you need more complicated conditions, you can use the
Cron expression. The Cron expression is pretty much the same as regular linux crontab except that it has the
6th value which represent
Year. Refer Cron Expressions for more details.
For our objective, we will use Cron expression and set the expression as
00 11 ? * 2-6 *
- The configure time is on UTC, so I must +8 so it will reflect to CST
- Date-of-month uses
?, AWS doc says that it means one or another, but I take it as “doesn’t matter which date”
- Month uses * which means all
- Day-of-week use 2-6 means Monday to Friday. Sat is 7, Sun is 1
- Year use * which means all
Once the value is set, a list will show below for you to verify the result, you can adjust the expression to correct it.
The last step is to configure the target and action to perform at scheduled time.
- From the drop down list, there are a lot of functions / services can be chosen from. Here we choose
EC2 StopInstances API call, which fits our objective.
- Input the target instance id for the action to perform to
- In order for CloudWatch Events to perform action (StopInstances) on EC2, proper role needs to be given. Here we can choose to create a new one or an existing role that has appropriate privileges for the action.
Give the rule a name and description and that’s it!
A finishing view
What is Amazon CloudWatch Events