(Day 40) Task : Infrastructure as Code with Terraform :-


In this article, we will explore the basics of Terraform, its key concepts, and how to get started writing your very first Terraform code to provision infrastructure. If you are following this series, make sure to keep a folder ready on your system and install Terraform (we’ll cover it below!).
Earlier in the series, we touched upon Helm basics. However, to build a stronger foundation in infrastructure provisioning, we’ll now pause the Helm series and dive deep into Terraform for the next few days.Once we’ve covered the core and advanced Terraform concepts, I’ll resume the Helm series with much more clarity, as the concepts from Terraform will help us better understand Helm Charts and Kubernetes deployments.
What is Terraform?
Terraform is an open-source tool developed by HashiCorp that allows you to define and provision infrastructure using a high-level declarative configuration language called HCL (HashiCorp Configuration Language).
Unlike manual setup using AWS Console or CLI, Terraform lets you declare infrastructure as code (IaC), making deployments automated, versioned, and reproducible.
Why Use Terraform?
Let’s understand with real-world analogies:
Without Terraform: You log into AWS and manually launch EC2 instances, set up networking, and manage configurations.
With Terraform: You write a .tf file to define everything. Just run terraform apply, and your infrastructure is automatically created.
Installing Terraform :
Windows/macOS/Linux:
Visit : https://developer.hashicorp.com/terraform/downloads
Debian/Ubuntu:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
Check Version:
terraform -version
Terraform Folder Structure (Best Practice):
Here's how a simple project structure looks:
terraform-ec2/
├── main.tf # Core infrastructure code
├── variables.tf # Variables (inputs)
├── outputs.tf # Output definitions
├── provider.tf # Provider setup (e.g., AWS)
└── terraform.tfvars # Actual variable values
Core Terraform Concepts :
Let’s break down the main building blocks:
1. Provider :
Specifies the cloud or platform you are working with.
provider "aws" {
region = "us-west-2"
}
2. Resource :
A block that defines what you want to create.
Example: An EC2 instance
resource "aws_instance" "demo" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
3. Variable :
Used to parameterize values.
variable "region" {
default = "us-east-1"
}
4. Output :
Used to print or export values after deployment.
5. State :
Terraform keeps track of infrastructure using a .tfstate
file. It records what has been created and ensures the next deployment is consistent.
Day 1 Hands-on: Launching an EC2 Instance on AWS :-
Step 1: Create a main.tf file :
Let’s create a basic EC2 instance in AWS:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Day40TerraformInstance"
}
}
Step 2: Initialize Terraform :
terraform init
This will download the AWS provider plugin.
Step 3: Validate the Code :
terraform validate
Step 4: See What Will Be Created :
terraform plan
This gives a preview of what Terraform will do.
Step 5: Apply the Configuration :
terraform apply
Type yes to confirm. Terraform will now create the EC2 instance.
Step 6: Destroy the Infrastructure :
To avoid charges in AWS:
terraform destroy
Type yes to destroy all resources created by Terraform.
Pro Tips for Beginners :
Use Visual Studio Code with the Terraform plugin for syntax highlighting.
Always keep your .tfstate file safe — treat it like your infrastructure "database."
Use terraform fmt to format your code automatically.
Prefer terraform plan before apply to avoid surprises.
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS