Infra/Dev-ops team do have instance created for POC/Demo/testing purpose which we need to stop daily (office off-hours) or during weekends for cost saving purpose. As this adds an overhead for us to daily stop the instance manually before leaving office and some times we might forget to stop the instance which again will add up the cost.So there was a demand to automate this process in order to save cost.
Now we have a feature in AWS to schedule a job in CloudWatch Events to stop a running instance daily or during weekend which is used for POC/Demo/testing purpose without any manual intervention.
I have to schedule it for one of my POC “poc_rundeck” instance to stop automatically daily during office off hours say after 9pm IST which will help us in saving cost .
Step One—Create a Rule
click on “create role” in CloudWatch Events Rules
Step Two—Schedule a Cron inside Rule
Set Cron expression to ” 30 15 * * ? * ” , In this use case we have set it to 15:30 GMT which is 9:00 PM IST . Link about CloudWatch Events schedules.
Step Three – Add Targets
Click “Add target”
Select “Built-in target”
Set Action as “Stop an EC2 instance”
Select the instance ID of machine to stop
Add another Target “SNS topic” for email Notification
Set the Topic to which you wish to send notification, In this use-case we select “infra”
Step 4 —Configure Details
Enter name and description and set State to Enabled as shown.
Give AWS Permissions by either creating a new role or selecting the existing role as shown.
Click “create rule” to complete the configuration step.
You will see a new rule created “rundeck_stop” and schedule in Rules dashboard
Detail View of Rule created
“poc-rundeck” Instance gets stop automatically at 9 pm IST
If you plan to work late night in office on your POC instance you can disable the rule by editing it in order to avoid instance getting stopped by CloudWatch events.