Many businesses are focused about lowering the costs of running AWS EC2 instances. Terraform can help you save money by automating EC2 instance management.This solution enables you to automate the start and stop off your instances based on your business requirements.
Non-production machines can be turned off after hours and on weekends. The machines can be started whenever working hours begin. This might result in more than 50% of your ec2 instances being stopped, saving you a lot.
- Install terraform video.
- Setup your AWS account video.
- Create a programmatic user with the permissions specified in the permission.json file.
- To schedule ec2 instances, we must have tagged them with the 'environment' tag.
This cron job is made up of several fields, each separated by a space:
[Minute] [Hour] [Day_of_Month] [Month_of_Year] [Day_of_Week]
- The first field is for minutes (0-59).
- The second field is for hours (0-23).
- The third field is for days of the month (1-31).
- The fourth field is for months (1-12).
- The fifth field is for days of the week (0-7, where both 0 and 7 represent Sunday).
- The sixth field is for year by default it take current year.
-
Clone this repository to your local machine by running the below command:
git clone https://github.com/madgicaltechdom/Schedule-Idle-EC2-Instances-with-Terraform-Automation.git -
Navigate to the repository directory by running the below command:
cd Schedule-Idle-EC2-Instances-with-Terraform-Automation -
Login to your AWS Account, search for the EC2, click on the "Tags", then the "Manage tags" button. Here you need to select the instances in which you need to make scheduling and add a tag, in the Key field select "environment", and in Value select "qa" or "prd" according to your need then click on the "Add Tag" button.
-
Optional, if you want to add another tag then first you need to add that tag in the workspace_to_environment_map variable in the varible.tf file and use that tag for scheduling.
variable "workspace_to_environment_map" { type = map(string) default = { qa = "qa" prd = "prd" } } -
To match your requirements, modify the stopping time value in the file variable.tf. In this case, "30 14" is UTC time, which corresponds to 8 p.m. IST. For timing, please refer to the chart we printed on the last of this file. Additionally, the machine is shut off at 8 p.m every Monday to Saturday. You can customise your days according to your need.
variable "cron_stop" { description = "Cron expression to define when to trigger a stop of the DB" default = "30 14 ? * MON-SAT *" } -
Change the starting time value in the file variable.tf to suit your needs. For timing, please refer to the chart in the last of this file. In this case, "30 03" denotes UTC time, which corresponds to 9 a.m. IST. Additionally, the machine is turned on at 9 a.m every Monday to Saturday. You can customise your days according to your need.
variable "cron_start" { description = "Cron expression to define when to trigger a start of the DB" default = "30 03 ? * MON-SAT *" } -
Change the AWS access key value in the file variable.tf to meet your requirements.
variable "access_key" { description = "value of access key" default = "" } -
Change the AWS secret key value in the file variable.tf to meet your requirements.
variable "secret_key" { description = "value of secret key" default = "" } -
Create a new workspace for each environment you want to deploy, for example for qa(testing):
terraform workspace new qa -
Initialize Terraform by running below command:
terraform init -
Run below command to preview the changes:
terraform plan -
Run below command to apply the changes:
terraform apply
-
Click on the link below to see status, also you can see the time slot by implementing 5 minute later and run the code to see it's running or stopped status as shown below:
https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#rules:
- Check The Execution Point Using System Manager by clicking on the link below:
https://us-east-2.console.aws.amazon.com/systems-manager/automation/executions?region=us-east-2
We took information from this article and this is the step by step User Guide video.
- Here is time Zone converter for IST to UTC:
We are very grateful for any contributions you are willing to make. Please have a look here to get started. If you aim to make a large change, it is helpful to discuss the change first in a new GitHub issue. Feel free to open one!
This project is licensed under the MIT License.





