1. What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool used to create, update, and destroy infrastructure using code.
It is developed by HashiCorp and is used to manage infrastructure across many platforms such as AWS, Azure, Google Cloud, VMware, Kubernetes, and more.
With Terraform, instead of creating infrastructure manually from a console, you define it in files and let Terraform provision it for you.
Key points
- IaC tool
- Used for infrastructure automation
- Created by HashiCorp
- Written in Go
- Works on:
- Windows
- Linux
- macOS
2. Why Terraform?
Terraform is popular because it brings software engineering practices to infrastructure.
Benefits of Terraform
- Infrastructure is managed with code
- Easy to read and maintain
- Easy to debug and test
- Platform independent
- Supports many providers out of the box
- Uses a declarative approach
- Reusable through modules
- One common language for many platforms
Terraform uses HCL (HashiCorp Configuration Language), which is simple to learn and easy to read.
3. What can Terraform manage?
Terraform can manage many types of infrastructure, such as:
- Virtual machines
- Networks
- Storage
- Databases
- Kubernetes resources
- DNS
- Security groups
It supports thousands of providers and modules.
Examples:
- Providers: AWS, Azure, GCP, Kubernetes, GitHub, Docker
- Modules: Reusable code blocks for common infrastructure setups
4. How Terraform works
Terraform works in this general flow:
You write Terraform code → Terraform processes the code → Provider talks to platform API → Infrastructure is created → State file is updated
Flow
- Terraform reads your code
- Terraform uses a provider
- Provider communicates with the target platform API
- Terraform stores infrastructure details in a state file
Common actions
terraform init→ initialize project and download providersterraform plan→ preview changesterraform apply→ create or update infrastructureterraform destroy→ delete infrastructure
5. Terraform workflow
The basic Terraform workflow is:
terraform init
terraform plan
terraform apply
terraform destroy
What each command does
terraform init
- Run only the first time in a project, or when providers/modules change
- Downloads required providers
- Prepares the working directory
terraform plan
- Shows a dry run
- Displays what Terraform will create, change, or destroy
terraform apply
- Executes the changes
- Creates or updates infrastructure
terraform destroy
- Deletes all resources managed by that code
6. Project structure
Terraform code is stored in files with the .tf extension.
Example:
project/
├── main.tf
├── variables.tf
├── outputs.tf
You can keep all code in one file, or split it into multiple .tf files.
Examples:
main.tfnetwork.tfinstance.tf
Terraform automatically reads all .tf files in the same directory.
7. Installing Terraform
Install Terraform from the official HashiCorp installation page for your operating system.
After installation, verify it:
terraform version
Example output may show a version like:
Terraform v1.14.8
8. Installing a provider
Providers allow Terraform to interact with platforms like AWS.
Example AWS provider configuration:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.41.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-east-1"
}
When you run terraform init, Terraform downloads the provider automatically.
9. Writing Terraform code
Terraform code is written using resources.
A resource defines an infrastructure object like:
- EC2 instance
- VPC
- EBS volume
Generic structure
resource "resource_type" "resource_name" {
attribute1 = "value1"
attribute2 = "value2"
}
10. Example project: Create an EC2 instance on AWS
Below is a simple example to create one EC2 instance.
File: main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.41.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0ec10929233384c7f"
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
11. Steps to run the project
Step 1: Create a project directory
mkdir terraform-ec2-demo
cd terraform-ec2-demo
Step 2: Create main.tf
Paste the EC2 example code into main.tf.
Step 3: Initialize Terraform
terraform init
This downloads the AWS provider.
Step 4: Check the execution plan
terraform plan
Terraform shows what it is going to create.
Step 5: Apply the code
terraform apply
Type:
yes
Terraform creates the EC2 instance.
Step 6: Destroy the infrastructure when done
terraform destroy
This removes the EC2 instance.
12. Example of multiple resources
Terraform can manage more than one resource in the same codebase.
Example structure:
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_ebs_volume" "example" {
availability_zone = "us-east-1a"
size = 10
}
resource "aws_instance" "example" {
ami = "ami-0ec10929233384c7f"
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
This shows how Terraform can manage networking, storage, and compute together.
13. Important Terraform concepts
As you move beyond basics, you will use:
Variables
Used to make code flexible and reusable.
Conditions
Used to create logic in code.
Loops
Used to create multiple resources efficiently.
Functions
Used for string, math, and collection operations.
Modules
Reusable Terraform code blocks.
You can think of modules like reusable components.
Workspace
Used to manage multiple environments such as dev, test, and prod.
Backend
Used to store the Terraform state remotely, such as in S3.
Templates
Used to dynamically generate configuration content.
Terraform Cloud
Used for remote execution, state management, and collaboration.
14. Terraform vs Cloud-specific tools
Terraform is often compared with:
- AWS CloudFormation
- Azure Resource Manager (ARM)
- GCP Deployment Manager
Why many teams prefer Terraform
- One language for many platforms
- Multi-cloud support
- Large provider ecosystem
- Large module ecosystem
- Easier standardization across teams
15. Best practices
- Keep code in version control
- Use variables instead of hardcoding values
- Store state securely
- Review changes with
terraform plan - Use modules for reusable code
- Destroy unused resources to avoid cost
16. Summary
Terraform is a powerful IaC tool for automating infrastructure.
With Terraform, you can:
- Define infrastructure as code
- Reuse configurations
- Manage resources across many platforms
- Preview changes before applying them
- Create and destroy infrastructure in a controlled way
The basic lifecycle is:
Write code → Init → Plan → Apply → Destroy
If you want, I can also turn this into:
- a beginner-friendly classroom tutorial
- a step-by-step hands-on lab
- a professional documentation format