Stop EC2 instances whenever you need, through AWS CloudFormation!

In this article, I have articulated how we can STOP EC2 instances as and when we need. There may be times when we actually don't need EC2 instances running but terminating them may result in loosing the instance completely. So many prefer to stop the instances instead and automating this to schedule daily at specific time periods like weekends or after work hours would be a real cost optimizing step.

 

Pre-cursor


Before we look into the cloudformation script directly, lets look how we will be stopping the instances using CFN. When we actually look into the steps involved in stopping EC2 using CFN, some of you may think that stopping instances manually instead would be simple (like just clicking a button). But we need someone to always manually stop the instances at odd times and here comes the benefit of automating it.

 

What the script does?


Stopping EC2 instances using cloudformation involves the following steps. The cloudformation script would do all the below mentioned steps.

  1. Creating a role for lambda and attach a policy with permission to stop the EC2 instances.