Skip to main content

Command Palette

Search for a command to run...

Terraform on AWS: Automating Cloud Infrastructure

Published
β€’4 min read
Terraform on AWS: Automating Cloud Infrastructure
S

πŸš€ Software Geek | DevOps Engineer πŸ› οΈ Hi, I'm Sahil Patil, a passionate DevOps wizard dedicated to transforming code into cash by building scalable, high-performing, and reliable systems. With a knack for solving complex problems, I thrive on turning chaos into cloud-based efficiency through the seamless integration of DevOps practices and cloud solutions.My toolkit includes Kubernetes 🐳, Docker πŸ‹, and Terraform βš™οΈ, which I use to design robust, secure, and efficient infrastructure. Linux 🐧 is my playground, where I excel in troubleshooting and optimizing environments. AWS ☁️ serves as my canvas for crafting innovative cloud architectures.πŸ† Achievements: πŸŽ“ Awarded with Prime Minister Scholarship with All India Rank 2032.πŸ’Ό Selected for an internship at LRDE DRDO, Bengaluru.πŸ… Received Gaurav Puraskar from Defence Welfare, India.πŸ“œ Received KSB Scholarships from Kendriya Sainik Board, New Delhi.🌱 What Drives Me: I'm committed to continuous learning and staying ahead in the ever-evolving tech landscape. I actively participate in DevOps and cloud community meetups 🀝 to network with industry experts and exchange insights, helping me refine my skills and broaden my perspective.Let’s connect and collaborate to build something remarkable! πŸš€

In today's cloud-driven world, automating infrastructure deployment is crucial for efficiency, scalability, and cost savings. Terraform, an open-source Infrastructure as Code (IaC) tool, helps developers and DevOps engineers automate cloud infrastructure on AWS and other platforms.

This article will explain Terraform on AWS, how it works, and how to set up an automated cloud infrastructure step by step. Let’s dive in! πŸŠβ€β™‚οΈ


What is Terraform? πŸ€”

Terraform is a powerful tool that lets you define your cloud infrastructure using code (HCL – HashiCorp Configuration Language). Instead of manually creating resources in AWS (like EC2, S3, RDS), Terraform allows you to:

βœ… Write infrastructure as code
βœ… Plan changes before applying
βœ… Deploy infrastructure efficiently
βœ… Track infrastructure state

Why Use Terraform on AWS?

Terraform provides several advantages when working with AWS:

πŸ”₯ Automation – No need for manual AWS configurations
πŸ›  Consistency – Infrastructure remains the same across different environments
πŸ”„ Version Control – Code can be stored in Git for tracking changes
πŸ“‰ Cost Optimization – Avoid human errors that could lead to high costs
πŸ”— Multi-Cloud Support – Works across AWS, Azure, GCP, etc.


How Terraform Works on AWS? πŸ—

Terraform follows a simple workflow:

1️⃣ Write Configuration – Define resources in .tf files
2️⃣ Initialize Terraform – Prepare Terraform to use AWS
3️⃣ Plan Deployment – Check what changes will be made
4️⃣ Apply Changes – Deploy infrastructure on AWS
5️⃣ Manage & Destroy – Update or remove resources


Getting Started with Terraform on AWS 🏁

Step 1: Install Terraform πŸ–₯

First, install Terraform on your machine.

For Ubuntu/Linux:

sudo apt update && sudo apt install -y terraform

For Mac (Homebrew):

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

For Windows: Download from Terraform Website.

Verify installation:

terraform -version

Step 2: Configure AWS Credentials πŸ”‘

Terraform needs access to AWS. You can configure it using the AWS CLI:

aws configure

Enter:
πŸ”Ή AWS Access Key
πŸ”Ή AWS Secret Key
πŸ”Ή Default AWS Region


Step 3: Create a Terraform Project Folder πŸ“‚

mkdir terraform-aws-demo && cd terraform-aws-demo

Create a new file main.tf inside this folder.


Step 4: Define AWS Infrastructure in Terraform πŸ—

Let’s create a simple AWS EC2 instance using Terraform.

πŸ“„ main.tf

provider "aws" {
  region = "us-east-1"  # Change this to your preferred AWS region
}

resource "aws_instance" "my_server" {
  ami           = "ami-0c55b159cbfafe1f0"  # Amazon Linux AMI (Check AWS for latest AMI ID)
  instance_type = "t2.micro"

  tags = {
    Name = "Terraform-Instance"
  }
}

πŸ”Ή Provider Block – Specifies AWS as the cloud provider.
πŸ”Ή Resource Block – Defines an EC2 instance with a specific AMI and type.
πŸ”Ή Tags – Helps in identifying the instance.


Step 5: Initialize Terraform πŸš€

Run:

terraform init

This downloads AWS provider plugins.


Step 6: Plan the Deployment πŸ”

Before deploying, check what Terraform will create:

terraform plan

It will list all resources that will be created.


Step 7: Apply Terraform Configuration ⚑

Run:

terraform apply

Type yes to confirm. Terraform will create the EC2 instance! πŸŽ‰


Step 8: Verify in AWS Console πŸ“‘

Go to AWS Console β†’ EC2 Dashboard β†’ Instances. You should see the new instance running!


Modifying Infrastructure with Terraform πŸ”„

Want to change the instance type? Update main.tf:

instance_type = "t3.micro"

Run:

terraform apply

Terraform will update the instance.


Destroying Infrastructure ❌

To remove all resources:

terraform destroy

Type yes to confirm. This deletes the EC2 instance.


Best Practices for Using Terraform on AWS πŸ†

βœ… Use Version Control (Git) – Store Terraform code in Git repositories.
βœ… Use Remote Backend – Store Terraform state in S3 for better team collaboration.
βœ… Follow Module Structure – Break large infrastructure into reusable Terraform modules.
βœ… Use Variables & Outputs – Keep your code flexible using input variables.

Example variables.tf:

variable "instance_type" {
  default = "t2.micro"
}

Modify main.tf:

instance_type = var.instance_type

Now you can change the instance type without modifying the code!


Conclusion 🎯

Terraform makes AWS infrastructure automation simple and powerful. By using Infrastructure as Code (IaC), you can create, manage, and scale AWS resources efficiently.

βœ… No manual AWS configurations
βœ… Consistent, repeatable deployments
βœ… Fast, automated infrastructure changes

Start using Terraform today and level up your DevOps skills! πŸš€


Do you want to deploy a full AWS architecture with Terraform? Let me know, and I’ll guide you through it! 😊

More from this blog

S

Sahil's Blogs

132 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer, and this blog simplifies Cloud DevOps concepts. Get easy-to-understand articles to help you master DevOps and Cloud Technologies! πŸš€