Using AWS CloudFormation to start/stop EC2/RDS instance

David Cheong
6 min readApr 7, 2020

AWS getting more popular nowadays where more and more company moving their workload on the AWS cloud platform including the development and UAT environment. Normally development and UAT environment only use by the programmer or staff during the working hour which around 8 to 12 hours per day excluding the public holiday and weekend. Let’s the instance up and running during this hours actually it’s a wasting of resources.

It is a pain if we may need to handle more than 1 instance manually on the start and stop of the resources.

Hereby I share an automated way using the Cloudformation to build to whole stack to complete this task.

Before we start the deployment, download the ready make AWS CloudFormation template on this link.

Login to AWS dashboard and go to CloudFormation console page, click on the create stack.

On the create stack page, select “Template is ready” -> Upload a template file -> Choose the template file that just downloaded and click Next

On the specify stack details page, enter the stack name and select service(s) to schedule, currently the stack support EC2 and RDS, if you want to schedule for both, than just select both, if not, just select either EC2 or RDS from the drop down

Summary of the parameters:

  1. Instance Scheduler tag, it will be used to tag EC2 or RDS instances.
  2. Service(s) to schedule, pick EC2, or RDS, or Both to apply what instances will be affected.
  3. Region(s), leave blank for your current region.
  4. Default time zone, default value is UTC, pick your current time zone.
  5. Cross-account role, leave blank to set stack for current account.
  6. This account, pick Yes for current account.
  7. Frequency, scheduler running frequency in minutes.
  8. Memory size, size of Lambda function run the scheduler.
  9. Started tags: SchedStart=”Scheduler started”
  10. Stopped tags: SchedStop=”Scheduler stopped”

*started and stopped tags is useful to identify the instance is start or stopped by the scheduler, the lambda function will attach this tag into your instance when it’s run

On the next page is just some more options for the scheduler, I skipped this because I didn’t make any change there. Next on the Review page, you will see the summary of CloudFormation stack that will be created.

Go to the bottom of the page. It is important for your current user to have [AWS::IAM::Role] capabilities. If you already have the required permission check the “I acknowledge…” check box and click Create.

Wait until the creation process is completed. It will deploy Amazon CloudWatch, AWS Lambda, and Amazon DynamoDB. Amazon DynamoDB will be used to store scheduler config and all affected instances state.

Now we need to create some config. You can use Amazon DynamoDB console, the scheduler CLI, or AWS CloudFormation custom resource. In here I will use DynamoDB console because it is easier.

Go to your Amazon DynamoDB console and click Tables. There will be 2 tables, <stack-name>-ConfigTable and <stack-name>-StateTable. We will make some changes only on ConfigTable. You can find a lot of sample config inside.

First step, we may need to create the period where it’s suitable for your used case, either you can edit the existing period of duplicated it from the existing and create your own one. To duplicate the item, just select on the existing sample and click on duplicate

A popup will appear and you can change everything you need like (more options in the docs):

  1. begintime, instance start time (24-hour format),
  2. description,
  3. endtime, instance stop time (24-hour format),
  4. name, period name (make it unique),
  5. weekdays, days of the week the instance will run.
  6. then click Save.

Then create a Schedule, that specify when instances should run. Pick one of the sample schedule and click Action > Duplicate.

A popup will appear and you can change everything you need like (more options in the docs):

  1. description,
  2. name, must unique because it will be used as tag value on the tagged resource,
  3. periods, must at least 1 period to be used to defines time(s) the instance should run,
  4. timezone,
  5. then click Save.

After that, now you need to tag all of your instances that will be affected by the scheduler. I will use Amazon EC2 as example. Choose an instance, click Action > Instance Setting > Add/Edit Tags

A popup will appear. Put key the same as your Instance Scheduler tag when creating the CloudFormation stack, and the schedule name (your DynamoDB config) as the value. Click save and you are done!

To check whether it is working or not, you can just check the affected instances tag if you set the Started Tags and Stopped Tags when creating the CloudFormation stack.

Same thing if you want to schedule your RDS instances as well, navigate to RDS console page and select the database which you wish to schedule and click into.

On the following page, select the tags and click on Add, just add the tags

On the pop up page, put key the same as your Instance Scheduler tag when creating the CloudFormation stack, and the schedule name (your DynamoDB config) as the value. Click save and you are done!

Check back your tags if it’s trigger by the scheduler, it will added the StoppedBy tag with the value of Scheduler Stack and also check your database is that in the stopped stage

This is very simplify way of scheduling the instance start/stop using the AWS CloudFormation, if you wish to know more in-deep about the every single step and how’s the schedule actually work behind the scene, please feel free to go through the AWS documentation.

Originally published at https://tech.david-cheong.com on April 7, 2020.

--

--