Terraform tutorial for beginners with AWS

Terraform Tutorial for Beginners: Build Real AWS Infrastructure

Introduction

Terraform is the tool that turns infrastructure into code. Instead of clicking through the AWS console to create a server — and hoping you remember every setting when you need a second one — you describe the server in a file, and Terraform creates it, changes it, and destroys it exactly as described. This tutorial teaches Terraform from zero, with a complete AWS example you can run: a virtual private cloud, a subnet, and an EC2 instance, all defined in code.

You should be comfortable with the terminal and know roughly what a cloud server is. You do not need prior Terraform experience, and you do not need to have read our Terraform Interview Questions first — though that article makes more sense after this one, because it tests exactly the concepts taught here. If you are aiming for AWS certification alongside Terraform, see the AWS Certification Roadmap 2026.

Version note (26 Nov 2026): the latest Terraform is 1.17.2, released 11 November 2026 (1.17.0 was released 21 October 2026). Terraform 1.6 and later are licensed under the Business Source License 1.1, not an open-source licence; OpenTofu is the community fork if that matters to your employer. The commands in this tutorial work on any recent Terraform 1.x version. For the full command reference, see the official Terraform documentation. The AWS provider’s resources are listed in the Terraform Registry.

What Terraform Is, in Plain Language

Terraform describes desired state. You write down what infrastructure should exist — “one VPC, one subnet, one EC2 instance of this type, in this region” — and Terraform compares that description with reality, works out the difference, and makes only the changes needed. Run it again with nothing changed, and it does nothing. That property — describing the end state instead of the steps — is what makes infrastructure reproducible: the same files create the same infrastructure on a colleague’s machine, in staging, and in production.

Terraform’s language is called HCL (HashiCorp Configuration Language). It is declarative, not a programming language: you write blocks that describe resources, and Terraform decides the order to create them in. A provider is a plugin that talks to one platform — AWS, Azure, Google Cloud, Kubernetes, GitHub — so the same workflow works across clouds, with different resource types.

Installing Terraform and Setting Up AWS

Install Terraform from HashiCorp’s official repository for your operating system, or download the binary from developer.hashicorp.com. Check it works:

terraform version

You need an AWS account and credentials configured — the AWS CLI (aws configure) or the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. Use an IAM user with limited permissions, never the root account, and never commit credentials to Git. For this tutorial, the cheapest path is a t3.micro instance in the AWS free tier, in us-east-1. Watch the billing page: Terraform creates real resources that cost real money if you forget to destroy them, which is why the last section of this tutorial exists.

Your First Configuration: a VPC, a Subnet and a Server

Create a directory and a file called main.tf:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  required_version = ">= 1.5"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "terraform-tutorial-vpc"
  }
}

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true

  tags = {
    Name = "terraform-tutorial-subnet"
  }
}

resource "aws_instance" "web" {
  ami           = "ami-0c7217cdde2cfd6b" # Amazon Linux 2023, us-east-1 — check the current AMI before running
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.public.id

  tags = {
    Name = "terraform-tutorial-web"
  }
}

Three ideas are doing the work here. A resource block (resource "aws_instance" "web") declares one thing to create. References (aws_vpc.main.id) tell Terraform about dependencies — it knows the subnet needs the VPC first, so it creates them in the right order without you saying so. And tags are not decoration: they are how you find, cost and clean up resources later.

Now run the core workflow — the same four commands you will use for the rest of your Terraform career:

terraform init     # download the AWS provider
terraform fmt      # format the files consistently
terraform validate # check the syntax without touching AWS
terraform plan     # show exactly what will be created

Read the plan carefully. It should say “3 to add, 0 to change, 0 to destroy”. If it does, apply it:

terraform apply

Type yes when asked. Terraform creates the VPC, the subnet and the instance, and writes the result into a state file — the next section explains why that file is the most important thing in the project.

State: the File That Makes Terraform Work

Terraform keeps a record of everything it manages in a file called terraform.tfstate. That file is how it knows, on the next run, that the VPC already exists and does not need creating again. It also stores values Terraform needs later, such as the instance ID. Three rules follow from that:

  1. Never edit the state file by hand. Use terraform state commands if you must change it.
  2. Never commit it to Git if it contains secrets. State can contain passwords and keys in plain text.
  3. Do not keep it only on your laptop in a team. Use a remote backend — an S3 bucket with locking — so the team shares one state and two people cannot apply at the same time.

For a team, the standard backend looks like this:

terraform {
  backend "s3" {
    bucket       = "my-terraform-state-bucket"
    key          = "tutorial/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
  }
}

The use_lockfile = true setting is the current way to lock state in S3 — it replaced the older DynamoDB locking table. Locking matters: without it, two applies at once can corrupt the state, and a corrupted state is the Terraform problem nobody wants at 2 a.m.

Variables, Outputs and Modules

Hard-coded values do not survive contact with a second environment. Replace them with variables:

variable "instance_type" {
  description = "EC2 instance type for the web server"
  type        = string
  default     = "t3.micro"
}

variable "region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

Use them as var.instance_type, and override them per environment with a terraform.tfvars file or -var flags. Outputs expose values you need after apply — the instance’s public IP, for example:

output "web_public_ip" {
  description = "Public IP of the web server"
  value       = aws_instance.web.public_ip
}

And modules are how Terraform scales: a module is a folder of .tf files with its own variables and outputs, reused across projects. You do not need modules for a first project — but when you find yourself copying the same VPC block into a third project, that is the moment to extract it into one.

Changing and Destroying Infrastructure

Change the instance type in main.tf from t3.micro to t3.small, run terraform plan, and read what it says: Terraform will stop the instance, change its type, and start it again — it tells you this before it does anything, which is the whole point of the plan step. Apply it, then check the AWS console: the change is exactly what the plan described, nothing more.

When you are done, destroy everything the tutorial created:

terraform destroy

Read the plan, type yes, and confirm in the AWS console that the instance, subnet and VPC are gone. Destroying is a skill, not an afterthought. The most expensive Terraform mistake a beginner makes is not a bad plan — it is a forgotten apply in a region nobody checks.

Common Beginner Mistakes

  • Applying without reading the plan. The plan is the safety net. If it says “200 to change” and you expected two, stop — our Terraform Interview Questions article opens with exactly that scenario.
  • Committing state or credentials to Git. Add *.tfstate, *.tfstate.backup and .terraform/ to .gitignore on day one.
  • One giant main.tf. Split files by concern (network.tf, compute.tf, variables.tf) once the project grows past a screenful.
  • No remote backend in a team. Local state on two laptops diverges, and diverging state is how infrastructure gets deleted by accident.
  • Hard-coding AMIs and regions. AMIs change by region and over time; look them up with a data source or a variable instead of copying an ID from a tutorial — including this one.
  • Forgetting terraform destroy on tutorial infrastructure. Set a calendar reminder if you must; the billing page does not forget.

FAQ

Is Terraform the same as AWS CloudFormation? No. CloudFormation only manages AWS; Terraform manages AWS, Azure, Google Cloud and hundreds of other platforms through providers, with the same workflow. CloudFormation is the right choice when a team is AWS-only and wants a fully AWS-native tool.

What is the difference between Terraform and OpenTofu? OpenTofu is the community fork of Terraform, created after Terraform’s licence changed to Business Source License 1.1 in version 1.6. The language and workflow are the same; the difference is licensing and governance, and it matters most to organisations with strict open-source policies.

Do I need to learn HCL as a programming language? No — HCL is declarative configuration, not a general-purpose language. You describe resources; Terraform decides how to create them. You will write more of it than code in most DevOps roles, and it is quicker to learn than a programming language.

How do I learn Terraform well enough for a job? Build something real, break it, and fix it with terraform plan — then read our Terraform Interview Questions and check you can answer them from experience, not memory.

Conclusion

You have now done the complete Terraform loop: written infrastructure as code, planned it, applied it, changed it, and destroyed it — with state stored safely and variables ready for a second environment. The next step is the one that separates a tutorial from a real project: a remote backend, a module for the VPC you will reuse, and a pipeline (see our Jenkins CI/CD Pipeline Tutorial or GitHub Actions Tutorial) that runs terraform plan on every pull request, so no change reaches production unreviewed. To plan the rest of your path, follow our DevOps Engineer Roadmap.

—

Scroll to Top