🔍 Understanding Terraform Step by Step
By Raees Qazi | DevOps Engineer | Learner | Mentor | Creator
Today, I spent time learning Terraform in detail. In this blog, I will explain the Terraform code, commands, and concepts in a very simple and easy way — especially for beginners who want to learn Infrastructure as Code (IaC) with Terraform.
🚀 Step 1: Terraform Initialization — terraform init
The first command we use in any Terraform project is:
terraform init❓ What does terraform init do?
This command does two main things:
- Initializes the backend — where Terraform stores the state file.
- Downloads and installs the providers (e.g., AWS, Azure, Local).
So, once you run terraform init, Terraform prepares your system to work with the required cloud or local infrastructure providers.
🌍 Understanding Providers in Terraform
🤔 What is a Provider?
A provider in Terraform is like a plugin that allows Terraform to interact with different platforms such as AWS, Azure, Google Cloud, or even your local system.
For example:
awsis a provider for Amazon Web Services.azurermis for Microsoft Azure.localis used for local file system operations like creating files, directories, or passwords.

🧱 Defining a Provider
We define providers in our Terraform configuration file like this:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.3.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "4.36.0"
}
}
}After saving this file, run terraform init again to download these providers.
📘 Example: Creating an EC2 Instance and Azure VM
Let’s say you want to create an EC2 instance on AWS, you will use:
resource "aws_instance" "example" {
...
}Here:
aws_instanceis the resource typeawsis the provider
Similarly, to create a VM in Azure, you would write:
resource "azurerm_virtual_machine" "example" {
...
}Again:
azurerm_virtual_machineis the resource typeazurermis the provider
💡 Real-Time Lab: Create an S3 Bucket using IAM User & Access Keys
👣 Step-by-Step
- Create IAM User in AWS Console
- Go to AWS → IAM → Users → Create a new user
- Attach AdministratorAccess policy
- Download Access Key and Secret Key
- Install AWS CLI
sudo apt-get installunzip curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"unzip awscliv2.zip sudo ./aws/install
- Configure AWS CLI with IAM credentials
aws configure # Enter access key, secret key, region, and output format
- Define AWS provider in
providers.tf
provider "aws" { region = "us-east-2" }
- Write S3 bucket code in
s3.tf
resource "aws_s3_bucket" "pc_bucket" {bucket = "bucket-name-demo-123"tags = { Name = "pc bucket demo cool"Environment = "dev" } }
- Run Terraform Commands
terraform init # Initialize Terraformterraform validate # Check if code is validterraform plan # See what will be createdterraform apply # Actually create the S3 bucket
📌 Summary
- terraform init → Prepares the environment and downloads providers
- Providers → Allow Terraform to connect to different platforms (AWS, Azure, Local)
- Resources → Define what you want to create (like EC2, S3, VM)
- IAM User & AWS CLI → Can be used to authenticate with AWS securely
🙌 Final Words
I hope this blog helped you understand how Terraform works in a simple way. If you learned something new today, please share this post with your friends or team members who are also learning DevOps.
Let’s grow together! 🚀
~ Raees Qazi
DevOps Engineer | Learner | Mentor | Creator
Comments
Post a Comment