Skip to main content

Command Palette

Search for a command to run...

Terraform Hands-On: Provisioning an EC2 Instance on AWS

Published
4 min read

Introduction

In today’s cloud‑driven world, managing infrastructure manually through cloud consoles does not scale. As applications grow, teams need automation, consistency, and repeatability. This is where Terraform comes in.

Terraform is one of the most widely used Infrastructure as Code (IaC) tools that helps DevOps teams provision and manage infrastructure reliably using code. Instead of clicking through cloud consoles, you describe your infrastructure once and Terraform takes care of creating, updating, and maintaining it.

This blog is written in a clear and structured way, starting from Terraform basics and gradually moving to practical concepts like EC2 creation, state files, and workspaces. It is suitable for beginners, DevOps aspirants, and engineers preparing for interviews.


What is Terraform?

Terraform is an open‑source Infrastructure as Code tool developed by HashiCorp. It allows you to define infrastructure using declarative configuration files and provision it across multiple cloud providers.

With Terraform, infrastructure is treated just like application code:

  • Written once

  • Version controlled

  • Reviewed and reused

Instead of manually creating resources like EC2 instances or VPCs, Terraform provisions them automatically based on your configuration.


Why Infrastructure as Code (IaC)?

Before IaC, infrastructure was managed manually:

  • Clicking through cloud consoles

  • No version control

  • Difficult to recreate environments

  • High chance of human error

With IaC:

  • Infrastructure is version‑controlled

  • Environments (dev, QA, prod) can be recreated easily

  • Changes are automated and consistent

  • Team collaboration becomes easier


Why Terraform?

Terraform stands out among IaC tools because:

  • It works with multiple cloud providers (AWS, Azure, GCP)

  • Uses declarative syntax (define what you want, not how)

  • Has a strong community and ecosystem

  • Integrates easily with CI/CD pipelines


Installing Terraform on Ubuntu

Step 1: Add HashiCorp GPG Key

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

Step 2: Add HashiCorp Repository

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list

Step 3: Install Terraform

sudo apt update
sudo apt install terraform -y

Step 4: Verify Installation

terraform version

Creating an EC2 Instance Using Terraform

Terraform Configuration Files

Terraform uses files with the .tf extension written in HCL (HashiCorp Configuration Language).

Provider

A provider tells Terraform which cloud platform to interact with.
provider.tf

provider "aws" {
  region = "us-east-1"
}

Resource

A resource represents a cloud component such as EC2, S3, or VPC.
main.tf

resource "aws_instance" "example" {
  ami           = "ami-0abcdef12345"
  instance_type = "t2.micro"
}

Terraform Initialization

Before provisioning infrastructure, initialize the project:

terraform init

After running this command, Terraform creates two important files:

.terraform/ Directory

This directory stores downloaded providers, modules, and backend files. It is machine‑specific and can be safely deleted and re‑created. Do not commit this directory to version control.

.terraform.lock.hcl File

This file locks the exact provider versions used in the project. It ensures consistent behavior across all machines and CI/CD pipelines. This file should be committed to Git.


Terraform Plan and Apply

Before applying changes, ensure the server has the required IAM role or AWS credentials.

Terraform Plan

terraform plan

This command compares your configuration with the current infrastructure and shows what will be created, updated, or destroyed.

Terraform Apply

terraform apply

This command provisions the infrastructure after confirmation.


Terraform State Files

After terraform apply, Terraform creates state files:

terraform.tfstate

This is the main state file that stores the current state of your infrastructure. It maps Terraform resources to real cloud resources and is used during plan and apply. In real projects, this file is stored in a remote backend.

terraform.tfstate.backup

This is an automatic backup of the previous state file. Terraform creates it before modifying the state during apply. It helps recover from accidental corruption or deletion.


Terraform Workspaces

Terraform workspaces allow you to manage multiple environments using the same configuration.

Common Workspace Commands

terraform workspace list
terraform workspace new dev
terraform workspace select prod

terraform.tfstate.d/ Directory

When using workspaces with a local backend, Terraform creates the terraform.tfstate.d/ directory. Each workspace gets its own subdirectory and state file, helping isolate environments like dev, QA, and prod.

Deleting a Workspace

  1. Destroy all infrastructure inside the workspace

  2. Switch to another workspace

  3. Delete the target workspace

terraform workspace delete prod

Conclusion

Terraform simplifies infrastructure management by bringing automation, consistency, and version control to cloud provisioning. From creating EC2 instances to managing multiple environments with workspaces, Terraform is an essential skill for any DevOps engineer.

Mastering Terraform fundamentals lays a strong foundation for advanced topics like remote backends, modules, and CI/CD automation.