
Infrastructure as Code (IaC) is a game-changer in modern cloud computing, allowing developers and DevOps engineers to define and manage cloud resources using code. Terraform, an open-source IaC tool by HashiCorp, is one of the most popular choices due to its simplicity and multi-cloud support.
In this beginner-friendly guide, we’ll walk through setting up Terraform, understanding its core files, and deploying your first AWS EC2 instance using Terraform. Let’s get started!
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool that lets you define cloud resources in a declarative way. Instead of manually provisioning servers, databases, and networking components via the AWS Console, you can write Terraform configurations and apply them to create infrastructure automatically.
Why Use Terraform?
✅ Supports multiple cloud providers (AWS, Azure, GCP, etc.)
✅ Ensures consistency and automation in infrastructure management
✅ Version control infrastructure using Git
✅ Easily destroy and recreate environments
GitHub Repository
You can find all the code examples used in this guide in my GitHub repository:
Step 1: Installing Terraform
Before we begin, ensure that Terraform is installed on your system.
For Windows
1. Download Terraform from the official website: Terraform Downloads
2. Extract the binary and add it to your system’s PATH.
3. Verify the installation:
terraform -v
For macOS (Using Homebrew)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
terraform -v
For Linux
sudo apt update && sudo apt install -y terraform
terraform -v
If Terraform is installed successfully, you should see its version printed in the terminal.
Step 2: Setting Up Your First Terraform Project
Now that Terraform is installed, let’s create a basic Terraform project that provisions an AWS EC2 instance.
- Create a New Directory for Your Project
mkdir terraform-aws-ec2
cd terraform-aws-ec2
2. Create the Terraform Configuration File (main.tf)
The main.tf file is the primary configuration file in Terraform. It defines the cloud provider and resources.
Here’s an example main.tf file that creates an AWS EC2 instance:
provider "aws" {
alias = "eu-north-1"
region = "eu-north-1" # Set your desired AWS region
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
variable "ami_id" {
description = "EC2 AMI ID"
type = string
}
resource "aws_instance" "example_instance" {
ami = var.ami_id
instance_type = var.instance_type
provider = aws.eu-north-1
tags = {
Name = "Terraform-instance"
}
}
output "public_ip" {
description = "Public IP address of the EC2 instance"
value = aws_instance.example_instance.public_ip
}Step 3: Understanding Terraform Files
When working with Terraform, you will encounter several key files:
1. main.tf (Configuration File)
Defines cloud providers and resources (like EC2 instances, S3 buckets, databases, etc.).
2. terraform.tfstate (State File)
This file keeps track of the resources Terraform manages.
⚠️ Never edit this file manually! It is automatically updated when running Terraform commands.
3. .terraform.lock.hcl (Dependency Lock File)
Locks the provider versions to ensure consistent deployments.
Step 4: Initializing Terraform
Before applying the configuration, we need to initialize Terraform:
terraform init
This downloads the necessary provider plugins and sets up the Terraform workspace.
Step 5: Applying the Configuration
To provision the EC2 instance, run:
terraform apply
Terraform will show a plan of the changes to be made. Type “yes” to confirm and apply the changes.
Once completed, Terraform will output the public IP of the created EC2 instance.
Step 6: Verifying Your Infrastructure
You can verify the instance creation in two ways:
1. Using AWS Console
Go to AWS Console > EC2 > Instances and look for the newly created instance.
2. Using AWS CLI
aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress"
Step 7: Destroying the Infrastructure
To remove the created AWS resources, use:
terraform destroy
Be careful when running this command, as it deletes all resources managed by Terraform.
🚀 Let’s Connect!
If you found this guide helpful, follow me for more DevOps and Cloud Engineering content:
🔗 GitHub → github.com/Dhanika-Kumarasiri
🔗 Medium → medium.com/@dhanika-kumarasiri
Have questions? Drop them in the comments! Let’s automate AWS the smart way! 🚀