Automate resources on AWS to terminate at the end of day

Huy Le
3 min readOct 21, 2022

--

This will be a simple tutorial.

We will be setting AWS Backup to save resources, creating an auto scaling group to automate test environment and I will have a part 2 of this tutorial for our Lambda function.

Step 1: Set up AWS Backup to save resources.

On your AWS Resources, hover to:

AWS Backup and “Create Backup Plan”.

Build a new plan and give it a name.

Enter the name for Backup Rule Name.

Create new Backup Vault with Default Encryption Key.

Retention period will be Weekly.

I will be adding new tags to assign them into each environment.

Hit Create Plan.

After that, It will take you to assign resources.

Give it a name.

Choose an IAM role and I chose my IAM with full admin access for this demo.

the Key will be Backup Equals the Value will be Web-Application-Prod. For anyone confuse about the name, Web-Application-Prod is my EC2.

hit Assign resources.

Step 2: Create Auto Scaling group to automate an environment.

The test environment will be created and terminated automatically.

Go to EC2 on your AWS Console and on the left side, under Auto Scaling, click on Launch Configurations.

Give it a name.

the AMI going to be ami-0002b3d83ef8bf621.

T2.micro

Under Advanced Details => User Data input the text as

#!/bin/bash
sed -i ‘s/Production/Test/g’ /var/www/html/index.php

Select your Security Group and Proceed without a key pair.

Hit Create Launch Configuration.

The screen will prompted that the launch configuration successfully created

Check mark the configuration => Hit Actions => Create Auto Scaling group

After that, It will take you to another page for auto scaling group creation.

Give it a name and then choose your VPC and Subnet.

Hit next till Step 6: Add tags

Choose Add tag and we will starting creating tags for each environment.

Create your auto scaling group after that.

Step 3: Schedule test environment to be provisioned when needed.

Check mark the newly created Auto Scaling group.

Go to Automatic scaling tab and scroll all the way down.

Hit Create scheduled action

We will create one for turning on that will be running from Monday to Friday, 9 AM EST

Give it a name

Desired Capacity 1— Min 1— Max — 1

Recurrence — Cron and enter 0 9* * MON-FRI

We will create another one to turn it off which will terminate.

Give it a name

Desired Capacity 0— Min 0— Max — 0

Recurrence — Cron and enter 0 17* * MON-FRI

Hit create after that.

Feel free to test it out and see if it works for you.

Thank you so much for reading this!

--

--

Huy Le

DevOps/Cloud engineer passionate about technologies. Looking to collaborate on anything Cloud related. Connect with me on https://www.linkedin.com/in/huy-le-110