0% found this document useful (0 votes)
2 views18 pages

Terraform Introduction With Example

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp that allows users to define and manage cloud infrastructure through code instead of manual configurations. It supports various cloud platforms and automates the creation, modification, and management of resources, leading to consistent and repeatable deployments. Terraform's workflow involves writing code, initializing, planning, applying changes, and tracking state, making it essential for modern DevOps practices.

Uploaded by

ssindhiya1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Terraform Introduction With Example

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp that allows users to define and manage cloud infrastructure through code instead of manual configurations. It supports various cloud platforms and automates the creation, modification, and management of resources, leading to consistent and repeatable deployments. Terraform's workflow involves writing code, initializing, planning, applying changes, and tracking state, making it essential for modern DevOps practices.

Uploaded by

ssindhiya1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Terraform – A Detailed Introduction (Simple Explanation

with Examples and Workflow)


Terraform is one of the most important Infrastructure as Code (IaC) tools used in Cloud
Computing, DevOps, Platform Engineering, and Cloud Architecture.

Terraform allows you to create, modify, and manage infrastructure using code instead of
manually clicking through cloud portals.

Think of Terraform as an architect's blueprint for cloud infrastructure.

1. What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp.

It allows you to define infrastructure such as:

Virtual Machines
Networks
Storage Accounts
Databases
Kubernetes Clusters
Load Balancers
DNS
Firewalls

using simple configuration files.

Instead of manually creating cloud resources, you write code that describes the desired
infrastructure.

2. Real-Life Example
Imagine you want to build a house.

Without a blueprint:

Build one room


Add another room
Move a wall
Change plumbing
Fix mistakes
This is slow and error-prone.

With a blueprint:

House Blueprint

Construction Team

Complete House

Terraform works exactly like the blueprint.

Instead of building houses, it builds cloud infrastructure.

3. Why Do We Need Terraform?


Without Terraform:
Manual cloud configuration
Human errors
Difficult to reproduce environments
Time-consuming deployments
Inconsistent infrastructure

With Terraform:
Infrastructure is created automatically
Same configuration every time
Easy to version with Git
Faster deployments
Repeatable and reliable environments

4. What is Infrastructure as Code (IaC)?


Infrastructure as Code means defining infrastructure in code files rather than creating it
manually.

Example:

Instead of:
Azure Portal

Click Create VM

Select Region

Choose Size

Create

You write:

hcl

resource "azurerm_linux_virtual_machine" "vm" {


name = "web-vm"
}

Terraform creates the VM automatically.

5. Real-World Uses of Terraform


Terraform can provision:
Azure Virtual Machines
AWS EC2 Instances
Google Cloud Compute Engine
Virtual Networks (VNets/VPCs)
Storage Accounts
SQL Databases
Kubernetes Clusters
DNS Zones
Load Balancers
Firewalls

6. Terraform vs Manual Deployment


Manual Deployment Terraform

Click through portal Write code

Time-consuming Fast

Easy to make mistakes Consistent

Difficult to repeat Repeatable

Hard to track changes Version controlled with Git

7. Terraform Workflow (Pathway)

Terraform Code

Terraform CLI

Cloud Provider API

Azure / AWS / GCP

Infrastructure Created

8. Complete Terraform Data Flow


Suppose you want to create a Virtual Machine.

Write Terraform Code

terraform init


terraform plan

terraform apply

Terraform Calls Azure API

Azure Creates VM

Terraform Updates State File

9. Installing Terraform
Download Terraform for your operating system.

Verify installation:

Bash

terraform version

Example:

Terraform v1.10.x

10. Terraform Project Structure


Example project:

AzureProject/

[Link]

[Link]

[Link]

[Link]
[Link]

.terraform/

[Link]

Each file has a specific purpose.

11. Main Terraform Files


[Link]
Contains resources.

Example:

hcl

resource "azurerm_resource_group" "rg" {


name = "DemoRG"
}

[Link]
Defines input variables.

hcl

variable "location" {
type = string
}

[Link]
Stores actual values.

hcl

location = "East US"


[Link]
Displays useful information.

hcl

output "resource_group_name" {
value = azurerm_resource_group.[Link]
}

12. What is a Provider?


A provider allows Terraform to communicate with a platform.

Examples:

Azure
AWS
Google Cloud
Kubernetes
VMware
GitHub

Example:

hcl

provider "azurerm" {
features {}
}

Flow:

Terraform

Azure Provider

Azure API

Azure Resources
13. What is a Resource?
A resource is any infrastructure component managed by Terraform.

Examples:

Resource Group
Virtual Machine
Storage Account
Network
SQL Database

Example:

hcl

resource "azurerm_storage_account" "storage" {

14. Terraform Commands


Initialize

Bash

terraform init

Downloads providers.

Project

terraform init

Download Provider Plugins

Validate
Bash

terraform validate

Checks configuration syntax.

Plan

Bash

terraform plan

Shows what Terraform intends to create, modify, or destroy.

Example:

Plan:

+ Create VM

+ Create Storage

+ Create Network

Nothing is changed yet.

Apply

Bash

terraform apply

Creates or updates infrastructure.

Terraform Code

Azure API

Infrastructure Created
Destroy

Bash

terraform destroy

Deletes all managed resources.

Infrastructure

terraform destroy

Resources Deleted

15. Terraform Lifecycle

Write Code

terraform init

terraform validate

terraform plan

terraform apply

Infrastructure Running

Modify Code

terraform plan

terraform apply

Updated Infrastructure

16. What is Terraform State?


Terraform keeps track of deployed resources in a state file.

[Link]

The state file records:


Resource IDs
Current configuration
Resource attributes
Relationships

Flow:

Terraform

Cloud

[Link]

Future Updates

17. Why is the State File Important?


Without the state file:
Terraform doesn't know what already exists.
It may try to recreate resources.

With the state file:


Only necessary changes are applied.
Infrastructure stays synchronized with the code.

18. Variables
Variables make configurations reusable.

Example:

hcl

variable "vm_name" {
default = "webvm"
}

Use:

hcl

name = var.vm_name

19. Outputs
Outputs display information after deployment.

Example:

hcl

output "vm_public_ip" {
value = azurerm_public_ip.vm.ip_address
}

Result:

[Link]
20. Modules
Modules are reusable Terraform components.

Example:

Root Module

Network Module

Virtual Machine Module

Storage Module

Instead of rewriting code, you reuse modules.

21. Cloud Deployment Example (Azure)


Suppose you want to deploy:
Resource Group
VNet
Subnet
VM
Storage Account

Workflow:

Terraform Code

terraform apply

Azure Provider

Azure Resource Manager API


Resource Group

Virtual Network

Subnet

Storage Account

Virtual Machine

22. CI/CD with Terraform


Terraform is commonly used in CI/CD pipelines.

Developer

Git Push

CI/CD Pipeline

terraform init

terraform plan

Approval (Optional)

terraform apply

Azure Infrastructure Updated

This enables automated infrastructure deployments.

23. Typical Terraform Workflow

Write Infrastructure Code

Store in Git

Developer Pushes Code

Pipeline Starts

Terraform Plan

Review Changes

Terraform Apply

Cloud Infrastructure Updated

24. Benefits of Terraform


Infrastructure as Code
Multi-cloud support
Automation
Version control
Repeatable deployments
Easy rollback using Git history
Reduced human error
Scalable infrastructure management

25. Common Terraform Terminology


Term Meaning

Provider Connects Terraform to a platform (Azure, AWS, GCP, etc.)

Resource Infrastructure object managed by Terraform

Module Reusable Terraform configuration

Variable Input value

Output Value displayed after deployment

State File Tracks deployed infrastructure

Plan Preview of proposed changes

Apply Executes changes

Destroy Removes managed infrastructure

26. Complete End-to-End Example


Suppose you need a new Azure environment.

Write Terraform Code

git commit

git push


CI/CD Pipeline Triggered

terraform init

terraform validate

terraform plan

Approval (Optional)

terraform apply

Azure Creates:

• Resource Group
• Virtual Network
• Subnet
• Network Security Group
• Virtual Machine
• Storage Account

Terraform Updates State File

Infrastructure Ready for Use

27. Key Concepts to Learn Next


1. Infrastructure as Code (IaC)
2. HashiCorp Configuration Language (HCL)
3. Providers
4. Resources
5. Variables
6. Outputs
7. Terraform Commands ( init , validate , plan , apply , destroy )
8. State Files ( [Link] )
9. Remote State (Azure Storage, AWS S3, Terraform Cloud)
10. Modules
11. Expressions and Functions
12. Loops ( count , for_each )
13. Conditional Expressions
14. Data Sources
15. Provisioners (use sparingly)
16. Workspaces
17. Dependency Management ( depends_on )
18. CI/CD Integration (Azure DevOps, GitHub Actions, Jenkins)
19. Terraform Best Practices
20. Terraform Cloud and Enterprise

Mastering Terraform enables you to provision and manage cloud infrastructure consistently
across Azure, AWS, Google Cloud, VMware, Kubernetes, and many other platforms.
Combined with Git, CI/CD, Docker, Kubernetes, and cloud platforms, Terraform forms a
core part of modern DevOps and cloud engineering practices.

You might also like