Getting Started

Terraform for Beginners: Complete Guide to Infrastructure as Code

By CloudFormation Team ยท 2025-06-01 ยท 12 min read

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:

  1. Write โ€” Define resources in .tf files using HashiCorp Configuration Language (HCL)
  2. Plan โ€” Run terraform plan to preview what will be created, changed, or destroyed
  3. Apply โ€” Run terraform apply to 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
  }
}
Always use remote state for team environments. Local state leads to conflicts, accidental deletions, and inconsistent deployments.

Terraform Workflow Commands

CommandWhat it does
terraform initDownload providers and initialize the working directory
terraform validateCheck configuration for syntax errors
terraform fmtFormat .tf files to canonical style
terraform planPreview changes without applying them
terraform applyApply the planned changes
terraform destroyDestroy all managed infrastructure
terraform outputShow output values
terraform state listList all resources in state

Best Practices for Beginners

  • Pin provider versions โ€” use ~> 5.0 syntax to avoid breaking changes
  • Use variables for everything that changes โ€” never hardcode region, account IDs, or AMI IDs
  • Always run terraform plan before 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 โ†’