Clicking through the AWS console to create servers works for a first experiment, but it does not scale. Nobody remembers every setting, two environments slowly drift apart, and rebuilding after a mistake takes hours. Terraform solves this by letting you describe your infrastructure in code, review it like any other code, and create the same setup again with one command.

In this beginner guide you will learn how Terraform works and write your first configuration that creates a real AWS resource.

What Is Infrastructure as Code?

Infrastructure as Code (IaC) means you define servers, networks, databases and storage in text files instead of creating them by hand. The files are stored in Git, so you get history, code review and the ability to roll back. Terraform, made by HashiCorp, is one of the most widely used IaC tools, and it works with AWS, Azure, Google Cloud and hundreds of other services through plugins called providers.

Key Terraform Concepts

Terraform is declarative: you describe the end result you want, and Terraform works out the steps to get there.

Before You Start

terraform -version aws sts get-caller-identity

Your First Terraform Project

Create an empty folder and add a file called main.tf. This configuration creates one S3 bucket:

terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" # use the current major version when you start a real project } } } provider "aws" { region = var.region } variable "region" { type = string default = "us-east-1" } variable "bucket_name" { type = string description = "S3 bucket names must be globally unique" } resource "aws_s3_bucket" "demo" { bucket = var.bucket_name tags = { Project = "terraform-demo" ManagedBy = "terraform" } } output "bucket_arn" { value = aws_s3_bucket.demo.arn }

S3 bucket names are shared by every AWS customer in the world, so choose something unique such as yourname-terraform-demo-2026.

The Core Terraform Workflow

Almost everything you do with Terraform follows the same four commands:

# 1. Download the AWS provider and prepare the folder terraform init # 2. Check the code for mistakes terraform validate # 3. Preview exactly what Terraform will create, change or destroy terraform plan -var="bucket_name=yourname-terraform-demo-2026" # 4. Create the resources (Terraform asks you to type "yes") terraform apply -var="bucket_name=yourname-terraform-demo-2026" # 5. Remove everything when you are done, so you are not charged terraform destroy -var="bucket_name=yourname-terraform-demo-2026"

Always read the plan

The plan is Terraform’s safety net. It lists every resource it will add (+), change (~) or destroy (-). Read it carefully before you type yes, especially in a shared or production account.

After apply, open the S3 service in the AWS console and you will see your bucket. Run terraform plan again and Terraform will report that nothing needs to change, because the real world already matches your code. Change a tag in the file, run apply again, and Terraform updates only that tag.

Understanding the State File

After the first apply you will see a terraform.tfstate file. It is Terraform’s memory of what it manages. Treat it carefully:

Using Variables Files

Typing -var every time is tedious. Create a file called terraform.tfvars:

region = "us-east-1" bucket_name = "yourname-terraform-demo-2026"

Terraform loads this file automatically. Keep separate variable files for each environment, such as dev.tfvars and prod.tfvars, and pass the one you want with -var-file.

Beginner Mistakes to Avoid

What to Learn Next

Once the basics feel comfortable, try creating an EC2 instance with a security group, then move on to modules, workspaces and remote state. Terraform pairs naturally with CI/CD: you can run plan on every pull request and apply after approval. See how pipelines work in our GitHub Actions tutorial, and follow the full DevOps roadmap for 2026 to see where Terraform fits.

Learn Terraform and AWS with Live Projects

Our DevOps program includes Terraform, AWS, Docker, Kubernetes and CI/CD, with mentor support.

Apply for the Program →