Tools: Terraform Has a Free API — Here's How to Manage Cloud Infrastructure as Code (2026)
Installation
Basic AWS Setup
Workflow
Variables
Modules
State Management Terraform by HashiCorp lets you define cloud infrastructure using declarative configuration files. It supports AWS, GCP, Azure, and 3,000+ providers — all free and open source. Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at [email protected] for custom solutions. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse
$ -weight: 500;">brew -weight: 500;">install terraform
# or download from terraform.io
-weight: 500;">brew -weight: 500;">install terraform
# or download from terraform.io
-weight: 500;">brew -weight: 500;">install terraform
# or download from terraform.io
# main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }
} provider "aws" { region = "us-east-1"
} resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" tags = { Name = "web-server" }
} output "public_ip" { value = aws_instance.web.public_ip
}
# main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }
} provider "aws" { region = "us-east-1"
} resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" tags = { Name = "web-server" }
} output "public_ip" { value = aws_instance.web.public_ip
}
# main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }
} provider "aws" { region = "us-east-1"
} resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" tags = { Name = "web-server" }
} output "public_ip" { value = aws_instance.web.public_ip
}
terraform init # Download providers
terraform plan # Preview changes
terraform apply # Apply changes
terraform destroy # Remove all resources
terraform init # Download providers
terraform plan # Preview changes
terraform apply # Apply changes
terraform destroy # Remove all resources
terraform init # Download providers
terraform plan # Preview changes
terraform apply # Apply changes
terraform destroy # Remove all resources
# variables.tf
variable "instance_type" { description = "EC2 instance type" type = string default = "t3.micro"
} variable "environment" { type = string validation { condition = contains(["dev", "staging", "prod"], var.environment) error_message = "Must be dev, staging, or prod." }
} # Use variables
resource "aws_instance" "web" { instance_type = var.instance_type tags = { Environment = var.environment }
}
# variables.tf
variable "instance_type" { description = "EC2 instance type" type = string default = "t3.micro"
} variable "environment" { type = string validation { condition = contains(["dev", "staging", "prod"], var.environment) error_message = "Must be dev, staging, or prod." }
} # Use variables
resource "aws_instance" "web" { instance_type = var.instance_type tags = { Environment = var.environment }
}
# variables.tf
variable "instance_type" { description = "EC2 instance type" type = string default = "t3.micro"
} variable "environment" { type = string validation { condition = contains(["dev", "staging", "prod"], var.environment) error_message = "Must be dev, staging, or prod." }
} # Use variables
resource "aws_instance" "web" { instance_type = var.instance_type tags = { Environment = var.environment }
}
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" name = "my-vpc" cidr = "10.0.0.0/16" azs = ["us-east-1a", "us-east-1b"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24"] public_subnets = ["10.0.101.0/24", "10.0.102.0/24"] enable_nat_gateway = true
}
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" name = "my-vpc" cidr = "10.0.0.0/16" azs = ["us-east-1a", "us-east-1b"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24"] public_subnets = ["10.0.101.0/24", "10.0.102.0/24"] enable_nat_gateway = true
}
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" name = "my-vpc" cidr = "10.0.0.0/16" azs = ["us-east-1a", "us-east-1b"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24"] public_subnets = ["10.0.101.0/24", "10.0.102.0/24"] enable_nat_gateway = true
}
terraform { backend "s3" { bucket = "my-terraform-state" key = "prod/terraform.tfstate" region = "us-east-1" encrypt = true }
}
terraform { backend "s3" { bucket = "my-terraform-state" key = "prod/terraform.tfstate" region = "us-east-1" encrypt = true }
}
terraform { backend "s3" { bucket = "my-terraform-state" key = "prod/terraform.tfstate" region = "us-east-1" encrypt = true }
}
data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] }
} resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id
}
data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] }
} resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id
}
data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] }
} resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id
}