AWS, Cloud Computing, DevOps

4 Mins Read

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

Voiced by Amazon Polly

Introduction

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.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Pre-requisites

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.

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.

step1

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

step2

Step 3: Install the terraform in local systems, open the visual studio, and create one directory. Inside that, create a file name as main.tf 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 ec2_lambda_handler.py 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 ec2_lambda_handler.zip file inside the same directory after updating the instance ID in the ec2_lambda_handler.py 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.

Conclusion

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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

FAQs

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

Share

Comments

    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!