What is Terraform?
Terraform by HashiCorp is an open-source Infrastructure as Code (IaC) tool that lets you define and provision infrastructure across multiple cloud providers using a single, consistent language (HCL).
Why Terraform? Write once, deploy anywhere. Same workflow (init, plan, apply) works across all cloud providers.
Official Docs: Terraform Documentation | Download & Install
Prerequisites
- Install Terraform CLI — Download here
- Create a free account on your cloud provider (AWS / GCP / Azure)
- Install the respective CLI tool for authentication
Verify installation:
terraform -version
Core Terraform Workflow
These 4 commands are all you need for every cloud provider:
# 1. Initialize the working directory & download providers
terraform init
# 2. Preview the changes Terraform will make
terraform plan
# 3. Apply the configuration to create resources
terraform apply
# 4. Destroy resources when no longer needed
terraform destroy
Cloud Provider Examples
Click a provider to see setup instructions and copy-ready sample code.
Amazon Web Services
Step 1: Configure credentials
aws configure
# Enter: Access Key, Secret Key, Region, Output format
Step 2: Create main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Terraform-Web-Server"
}
}
resource "aws_s3_bucket" "app_bucket" {
bucket = "my-terraform-demo-bucket-12345"
tags = { Environment = "Dev" }
}
output "instance_public_ip" {
value = aws_instance.web_server.public_ip
}
Step 3: Deploy
terraform init && terraform plan && terraform apply
AWS Resources: Provider Docs | Get Started | Free Tier
Google Cloud Platform
Step 1: Authenticate
gcloud auth application-default login
gcloud config set project YOUR_PROJECT_ID
Step 2: Create main.tf
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
provider "google" {
project = "your-gcp-project-id"
region = "us-central1"
zone = "us-central1-a"
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-vm"
machine_type = "e2-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {}
}
}
resource "google_storage_bucket" "app_bucket" {
name = "my-terraform-demo-bucket-12345"
location = "US"
force_destroy = true
}
output "vm_external_ip" {
value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
}
Step 3: Deploy
terraform init && terraform plan && terraform apply
GCP Resources: Provider Docs | Get Started | Free Tier
Microsoft Azure
Step 1: Login
az login
az account set --subscription "YOUR_SUBSCRIPTION_ID"
Step 2: Create main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "terraform-rg"
location = "East US"
}
resource "azurerm_storage_account" "storage" {
name = "tfdemostorage12345"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_virtual_network" "vnet" {
name = "terraform-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
Step 3: Deploy
terraform init && terraform plan && terraform apply
Azure Resources: Provider Docs | Get Started | Free Account
Deploying to All Three Clouds at Once
Combine all providers in a single Terraform configuration:
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
google = { source = "hashicorp/google", version = "~> 5.0" }
azurerm = { source = "hashicorp/azurerm", version = "~> 3.0" }
}
}
provider "aws" { region = "us-east-1" }
provider "google" { project = "your-gcp-project-id"; region = "us-central1" }
provider "azurerm" { features {} }
# AWS S3 Bucket
resource "aws_s3_bucket" "aws_store" {
bucket = "multicloud-aws-bucket-12345"
}
# GCP Storage Bucket
resource "google_storage_bucket" "gcp_store" {
name = "multicloud-gcp-bucket-12345"
location = "US"
}
# Azure Resource Group
resource "azurerm_resource_group" "azure_rg" {
name = "multicloud-rg"
location = "East US"
}
Run
terraform applyonce — Terraform provisions resources across all three clouds in parallel.
Quick Comparison
| Feature | AWS | GCP | Azure |
|---|---|---|---|
| Provider | hashicorp/aws | hashicorp/google | hashicorp/azurerm |
| Auth | aws configure | gcloud auth login | az login |
| Compute | aws_instance | google_compute_instance | azurerm_linux_virtual_machine |
| Storage | aws_s3_bucket | google_storage_bucket | azurerm_storage_account |
Video Tutorial
Best Practices
- Use remote state: Store terraform.tfstate in S3, GCS, or Azure Blob for team collaboration.
- Never commit secrets: Use environment variables or secret managers.
- Use variables & modules: Keep code DRY and reusable.
- Always run terraform plan before applying.
- Use version constraints for providers to avoid breaking changes.
Learn more: Terraform Modules | Remote State Backends