What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It lets you define your cloud infrastructure โ servers, databases, networking, DNS, and more โ in human-readable configuration files, then provision and manage that infrastructure automatically.
Instead of clicking through cloud consoles, you write code. That code becomes your infrastructure. It's version-controlled, reviewed, tested, and deployed just like application code.
Infrastructure as Code means your cloud environment is reproducible, auditable, and consistent across every team and every environment.
How Terraform Works
Terraform uses a simple three-step workflow:
- Write โ Define resources in
.tffiles using HashiCorp Configuration Language (HCL) - Plan โ Run
terraform planto preview what will be created, changed, or destroyed - Apply โ Run
terraform applyto make the changes real
Terraform keeps track of what it has provisioned in a state file (terraform.tfstate). This file is the source of truth for what infrastructure currently exists.
Your First Terraform Configuration
Every Terraform project starts with a provider. Here's the minimal setup for AWS:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.5.0"
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region"
type = string
default = "us-east-1"
}
Core Terraform Concepts
Resources
Resources are the fundamental building blocks. Each resource block describes one infrastructure object:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.medium"
tags = {
Name = "web-server"
Environment = "production"
}
}
Variables and Outputs
Variables make your configurations reusable. Outputs expose values to other modules or for human inspection:
# variables.tf
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.medium"
}
# outputs.tf
output "instance_public_ip" {
description = "Public IP of the web server"
value = aws_instance.web.public_ip
}
Data Sources
Data sources let you read information from existing infrastructure without managing it:
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux.id
}
Modules
Modules are reusable packages of Terraform configurations. The Terraform Registry has thousands of community modules:
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", "us-east-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
}
Managing Terraform State
State is one of the most important โ and most misunderstood โ concepts in Terraform. By default, state is stored locally in terraform.tfstate. For teams, you need remote state:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/infrastructure.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
Terraform Workflow Commands
| Command | What it does |
|---|---|
terraform init | Download providers and initialize the working directory |
terraform validate | Check configuration for syntax errors |
terraform fmt | Format .tf files to canonical style |
terraform plan | Preview changes without applying them |
terraform apply | Apply the planned changes |
terraform destroy | Destroy all managed infrastructure |
terraform output | Show output values |
terraform state list | List all resources in state |
Best Practices for Beginners
- Pin provider versions โ use
~> 5.0syntax to avoid breaking changes - Use variables for everything that changes โ never hardcode region, account IDs, or AMI IDs
- Always run
terraform planbefore apply โ review every change before it happens - Store state remotely โ S3 + DynamoDB for AWS, Azure Blob, or GCS for other clouds
- Tag all resources โ add consistent tags for cost tracking and ownership
- Use workspaces or separate state files โ keep dev, staging, and prod completely separate
Designing Terraform Infrastructure Visually
Writing Terraform from scratch is powerful but time-consuming. CloudFormation lets you design your infrastructure visually on a drag-and-drop canvas, then generates all the Terraform HCL automatically โ including main.tf, variables.tf, outputs.tf, provider.tf, and deploy scripts for Shell, Python, TypeScript, and Go.
It supports 693 resources across 9 cloud providers including AWS, Azure, GCP, IBM Cloud, Oracle Cloud, VMware, Kubernetes, and Alibaba Cloud โ all from a single browser tab, no installation required.
Try CloudFormation Free
Design infrastructure visually and generate production-ready Terraform in seconds โ 693 resources across 9 cloud providers.
๐ Open the Designer โ