Skip to main content

Command Palette

Search for a command to run...

Infrastructure as Code 101: Provisioning an AWS EC2 Instance with Terraform

Updated
•5 min read
A
Hi, I'm Atul! I am a software engineer with a strong focus on DevOps, Cloud Infrastructure, and automation. I spend my time building scalable CI/CD pipelines, mastering tools like Docker, Terraform, and AWS, and breaking down complex cloud concepts into accessible, hands-on tutorials. I believe the best way to learn is to build, break, and document the process.

If you are currently managing your AWS infrastructure by logging into the console, clicking through menus, checking boxes, and hitting "Create," you are practicing what DevOps engineers jokingly call "ClickOps."

While clicking around is fine for learning, it is a nightmare for production. Manual clicks are impossible to track, prone to human error, and incredibly slow to replicate. If your server goes down, you have to remember exactly which boxes you checked six months ago to rebuild it.

The solution is Infrastructure as Code (IaC).

Today, we are diving into Terraform, the industry standard for IaC. I am going to show you how to stop clicking and start coding by provisioning a basic Linux AWS EC2 instance using just a few lines of code.

What is Terraform?

Created by HashiCorp, Terraform is a tool that allows you to define both cloud and on-premise resources in human-readable configuration files. It uses a declarative language called HCL (HashiCorp Configuration Language).

"Declarative" means you just tell Terraform what you want (e.g., "I want one t2.micro EC2 instance"), and Terraform figures out how to communicate with AWS APIs to make it happen.

Prerequisites

Before we write our code, you need three things:

  1. An AWS Account (the Free Tier is perfect).

  2. The AWS CLI installed and configured on your machine with your IAM credentials (so Terraform has permission to talk to your account).

  3. Terraform installed on your local machine.


Step 1: Define the Provider

Terraform can manage infrastructure on AWS, Azure, Google Cloud, and even GitHub! To tell Terraform we want to talk specifically to AWS, we need to configure a "Provider."

Create a new folder on your computer, open it in your terminal, and create a file called main.tf.

Add this code:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

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

Step 2: Write the EC2 Resource Block

Now, we tell Terraform exactly what resource we want to build. Below your provider block in main.tf, add the following code:

resource "aws_instance" "my_first_server" {
  ami           = "ami-0c7217cdde317cfec" # Ubuntu 22.04 LTS in us-east-1
  instance_type = "t2.micro"

  tags = {
    Name = "AtulCodes-Terraform-Server"
  }
}

Breaking down the code:

  • resource: Tells Terraform we are creating a new piece of infrastructure.

  • "aws_instance": The specific type of resource (an EC2 instance).

  • "my_first_server": This is just a local name we use to reference this server within our Terraform code. AWS never sees this name.

  • ami: The Amazon Machine Image ID. Note: AMIs are region-specific! The one above is for Ubuntu in us-east-1. If you are in ap-south-1 (Mumbai), you will need to grab a different AMI ID from the AWS console.

  • instance_type: We are using t2.micro because it qualifies for the AWS Free Tier.

  • tags: This is how we actually name the server inside the AWS Console.

Step 3: The Core Terraform Workflow

Terraform relies on three magical commands to get your infrastructure into the cloud: init, plan, and apply.

1. Initialize the Directory

Open your terminal in your project folder and run:

terraform init

This command looks at your main.tf file, sees you want to use AWS, and downloads the necessary AWS plugins in the background.

2. Plan the Deployment

Next, we want to see what Terraform is going to do before it actually does it.

terraform plan

You will get a large output showing exactly what resources will be created. Look for the green + signs. At the bottom, it should say: Plan: 1 to add, 0 to change, 0 to destroy.

3. Apply the Code

If the plan looks good, it is time to deploy!

terraform apply

Terraform will show you the plan one more time and ask you to type yes to confirm. Type yes and hit enter. Within a few seconds, Terraform will communicate with AWS and spin up your server!

Step 4: Verify in the AWS Console

Don't just take my word for it. Log into your AWS Management Console, navigate to the EC2 Dashboard, and click on Instances.

You will see a brand-new server named AtulCodes-Terraform-Server sitting right there, in a "Running" state. You provisioned that without clicking a single button in the UI!

Step 5: Destroy the Evidence (Crucial!)

One of the best features of IaC is how easy it is to clean up. If you leave this server running, AWS might eventually charge you for it.

To tear down everything we just built, run:

terraform destroy

Type yes when prompted. Terraform will gracefully delete the EC2 instance it created.

The Takeaway

You have officially taken your first step into Infrastructure as Code. By writing your infrastructure as code, you can version control it in Git, review it in Pull Requests, and ensure your staging environments are exact replicas of your production environments.

Next week, I am dropping my Ultimate Terraform Cheatsheet! It will cover all the essential commands, state management, and variable syntax you need for your daily DevOps workflows. Make sure you hit the subscribe button so you don't miss it.


If this tutorial helped you deploy your first Terraform resource, you can ā˜• buy me a coffee here to support my work!

More from this blog

A

Atul Codes | DevOps & Cloud Engineering

5 posts

Welcome to Atul Codes! This blog is dedicated to helping developers master DevOps, Cloud Computing, and infrastructure automation. Expect weekly, hands-on tutorials covering CI/CD pipelines, Docker, AWS, and Terraform. Whether you are deploying your first container or looking to optimize your cloud architecture, you will find practical, step-by-step guides and real-world solutions here.