AWS, Cloud Computing, DevOps

4 Mins Read

A Guide to Automate Amazon EC2 Instance Start and Stop with Terraform

Voiced by Amazon Polly


In the dynamic landscape of modern IT infrastructure, where agility and scalability are paramount, the need for efficient and reproducible management of resources has given rise to Infrastructure as Code (IaC) tools. Terraform redefines how infrastructure is provisioned and managed, offering a declarative and version-controlled approach to safely and efficiently building, modifying, and versioning infrastructure safely and efficiently.

This blog post aims to unravel the core concepts of Terraform, exploring how it enables users to define and automate infrastructure deployment across various cloud providers, on-premises data centers, and other service providers.

Terraform allows you to describe your complete infrastructure in the form of code. Even if your servers come from different providers such as AWS, Azure, or GCP, Terraform helps you build and manage these resources in parallel across providers. It can automate the creation of the infrastructure with a single click and delete the infrastructure with a single click.


Terraform Installed:

Make sure that Terraform is installed on your local machine. You can download the latest version of Terraform from the official HashiCorp website. Having Terraform installed lets you define and provision your infrastructure using Terraform configuration files.

AWS CLI (Command Line Interface):

The AWS CLI should be installed and configured with the necessary credentials on your local machine. Terraform uses these credentials to authenticate and interact with your AWS account. Ensure the AWS CLI is set up correctly with the required access keys.

Terraform Configuration Files:

Create Terraform configuration files (typically with a .tf extension) that define the infrastructure you want to manage. These files will specify the AWS provider, resources (such as Amazon EC2 instances), and any additional configurations needed for your automation.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Steps to Automate the Amazon EC2 instance

Step 1: Go to the AWS IAM section, create a user with administrator access, and copy the access and secret keys used to help create the infrastructure.


Step 2: Note the access key and secret key of the user.


Step 3: Install the terraform in local systems, open the visual studio, and create one directory. Inside that, create a file name as and update the below script.

  • In the place of access key and secret, update the details that are copied in step 2, and also mention the region where you want to create the infrastructure.
  • Update the timings in the cron job when you want to start and stop the Amazon EC2 Instance in the Amazon CloudWatch event rule.
  • The lines that are marked in yellow need to be updated.

Step 4:  Create another file called and update the below script inside that file. Update the Amazon EC2 instance ID once the infrastructure is created in your account. You can get the instance ID from the output of the terraform script.

Step 5: Create the file inside the same directory after updating the instance ID in the file and run the following commands to create the infrastructure in your account.

Terraform Commands to Run:

If you want to delete the infrastructure, you can run.


Automating the start and stop of Amazon EC2 instances through Terraform unveils a powerful paradigm shift in infrastructure management. Addressing the prerequisites outlined in this guide lays the groundwork for a streamlined and efficient automation process. Terraform’s declarative approach to Infrastructure as Code (IaC) empowers you to define, version, and automate your AWS infrastructure effortlessly, enhancing agility and reducing operational costs. Harnessing the potential of Terraform, you not only simplify the complexities of infrastructure provisioning but also embrace a scalable and reproducible model that aligns seamlessly with the demands of modern cloud computing.

Drop a query if you have any questions regarding Amazon EC2 and Terraform, and we will get back to you quickly.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, Microsoft Gold Partner, and many more, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.


1. What is Terraform?

ANS: – Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It enables users to define and provision infrastructure using a declarative configuration language. Terraform supports various cloud providers, on-premises data centers, and other infrastructure platforms.

2. Why automate the start and stop of Amazon EC2 instances?

ANS: – Automating the start and stop of Amazon EC2 instances helps optimize costs by ensuring that resources are active only when necessary. This is especially beneficial for non-production environments, or instances used intermittently, allowing users to scale their infrastructure based on demand and reduce overall operational expenses.

3. Can I schedule the start and stop of Amazon EC2 instances at specific times?

ANS: – Yes, Terraform provides flexibility through variables and external tools to schedule the start and stop of Amazon EC2 instances at specific times. Users can integrate Terraform with external schedulers or leverage native cloud provider features for scheduled actions.

WRITTEN BY Yamini Reddy



    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!