🔍 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:

  1. Initializes the backend — where Terraform stores the state file.
  2. 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?

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:

  • aws is a provider for Amazon Web Services.
  • azurerm is for Microsoft Azure.
  • local is used for local file system operations like creating files, directories, or passwords.
Press enter or click to view image in full size

🧱 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_instance is the resource type
  • aws is the provider

Similarly, to create a VM in Azure, you would write:

resource "azurerm_virtual_machine" "example" {
...
}

Again:

  • azurerm_virtual_machine is the resource type
  • azurerm is the provider

💡 Real-Time Lab: Create an S3 Bucket using IAM User & Access Keys

👣 Step-by-Step

  1. Create IAM User in AWS Console
  • Go to AWS → IAM → Users → Create a new user
  • Attach AdministratorAccess policy
  • Download Access Key and Secret Key
  1. Install AWS CLI
  • sudo apt-get install
  • unzip curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
  • unzip awscliv2.zip sudo ./aws/install
  1. Configure AWS CLI with IAM credentials
  • aws configure # Enter access key, secret key, region, and output format
  1. Define AWS provider in providers.tf
  • provider "aws" { region = "us-east-2" }
  1. 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" } }
  1. Run Terraform Commands
  • terraform init # Initialize Terraform
  • terraform validate # Check if code is valid
  • terraform plan # See what will be created
  • terraform 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

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator