AWS  

How to Use Terraform to Provision Infrastructure on AWS Step by Step?

Introduction

In modern cloud environments like AWS, managing infrastructure manually using the AWS Console becomes difficult as applications grow. Creating EC2 instances, configuring networks, and managing storage repeatedly can lead to errors, inconsistency, and wasted time.

This is where Terraform becomes extremely useful.

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define AWS infrastructure using simple code. Instead of manually clicking in the AWS Console, you write configuration files, and Terraform automatically provisions everything.

This approach is widely used in DevOps, cloud engineering, and automation workflows because it ensures consistency, scalability, and faster deployments.

What is Terraform?

Terraform is an open-source tool developed by HashiCorp that helps you build, change, and manage infrastructure safely and efficiently.

Simple explanation

Instead of doing this manually:

  • Create EC2 from AWS Console

  • Configure networking

  • Attach storage

You simply write code, and Terraform creates everything automatically.

Why Terraform is popular in AWS and DevOps

  • Automates infrastructure setup

  • Reduces human errors

  • Supports multi-cloud (AWS, Azure, GCP)

  • Works well with CI/CD pipelines

What is Infrastructure as Code (IaC)?

Infrastructure as Code means managing servers, networks, and cloud resources using code instead of manual processes.

Real-life analogy

Imagine setting up a new laptop manually every time vs using a script that installs everything automatically.

Before IaC:

  • Manual setup every time

  • High chance of mistakes

After IaC:

  • Write once, use everywhere

  • Same setup in all environments

This is why IaC is a core concept in modern cloud computing.

Prerequisites for Terraform AWS Setup

Before starting Terraform on AWS, make sure you have the following:

Required tools and setup

  • AWS account (Free Tier recommended for beginners)

  • Terraform installed on your system

  • AWS CLI installed and configured

  • Basic knowledge of command line and cloud concepts

Beginner tip

If you are starting from India or any region, use AWS region like ap-south-1 for better latency and cost optimization.

Step 1: Install Terraform

Download Terraform from the official HashiCorp website and install it on your system.

Verify installation using:

terraform -v

If installed correctly, it will show the Terraform version.

Why this step is important

Without Terraform installed, you cannot run infrastructure code.

Step 2: Configure AWS Credentials

Terraform needs access to your AWS account to create resources.

Run the command:

aws configure

Enter the following:

  • AWS Access Key

  • AWS Secret Key

  • Default region (e.g., ap-south-1)

What happens here

You are securely connecting Terraform with AWS so it can provision resources.

Step 3: Create Terraform Project Structure

Create a project folder:

mkdir terraform-aws
cd terraform-aws

Now create a main configuration file:

main.tf

Why this matters

This file will define your entire AWS infrastructure.

Step 4: Write Terraform Configuration for AWS

Now write your first Terraform code to create an EC2 instance.

provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "my_ec2" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Explanation in simple words

  • provider tells Terraform which cloud to use

  • resource defines what to create

  • ami is the OS image (Linux/Ubuntu)

  • instance_type defines server size

This is the core of AWS infrastructure provisioning using Terraform.

Step 5: Initialize Terraform

Run:

terraform init

What happens internally

  • Downloads AWS provider plugins

  • Prepares working directory

This step must be done before running any Terraform commands.

Step 6: Preview Infrastructure Changes

Run:

terraform plan

Why this step is important

  • Shows what Terraform will create

  • Helps detect mistakes before execution

Think of it like a “preview” before deployment.

Step 7: Apply Terraform Configuration

Run:

terraform apply

Type yes when prompted.

What happens now

  • Terraform connects to AWS

  • Creates EC2 instance

  • Allocates resources automatically

This is where actual infrastructure provisioning happens.

Step 8: Verify Resources in AWS Console

Go to AWS Console → EC2 Dashboard

You will see your instance running.

Why verification matters

  • Confirms successful deployment

  • Helps in debugging if something fails

Step 9: Modify Infrastructure (Real DevOps Scenario)

Suppose you want to upgrade your instance.

Before:

instance_type = "t2.micro"

After:

instance_type = "t2.small"

Run:

terraform apply

What Terraform does

  • Detects change

  • Updates only required resources

This is called incremental infrastructure update.

Step 10: Destroy Infrastructure to Save Cost

Run:

terraform destroy

Why this is critical in AWS

  • Prevents unnecessary billing

  • Cleans unused resources

Always destroy resources when not in use, especially in AWS Free Tier.

Advantages of Using Terraform on AWS

Automation and speed

Infrastructure can be created in minutes instead of hours.

Consistency across environments

Same code works for dev, test, and production.

Version control integration

You can track infrastructure changes using Git.

Scalability

Easily scale infrastructure by updating code.

Disadvantages of Terraform

Learning curve for beginners

Understanding syntax and workflow takes time.

State file management complexity

Terraform state file must be handled carefully.

Debugging issues

Error messages can sometimes be complex.

Common Mistakes to Avoid in Terraform AWS

Not using terraform plan

Skipping this can cause unexpected changes.

Hardcoding values

Always use variables for flexibility.

Ignoring state file security

Store state file securely (e.g., S3 backend).

Manual changes in AWS

Avoid changing resources directly in AWS Console.

Real-World Use Case of Terraform in AWS

A startup was manually creating EC2 instances and networking in AWS.

Problems they faced

  • Time-consuming setup

  • Inconsistent configurations

  • Difficult to scale

After using Terraform

  • Infrastructure created in minutes

  • Same setup across environments

  • Easy to manage and scale

This is why Terraform is widely used in DevOps and cloud engineering roles.

Summary

Using Terraform to provision infrastructure on AWS transforms manual cloud management into an automated, consistent, and scalable process. By writing simple configuration files, teams can create, update, and delete AWS resources efficiently while maintaining full control through versioning and automation. This approach reduces errors, saves time, and aligns perfectly with modern DevOps practices, making Terraform an essential tool for anyone working with AWS cloud infrastructure.