Terraform Tutorial for Beginners: Build Your First AWS Project

This Terraform tutorial for beginners takes you from an empty folder to real cloud infrastructure that you can create, change and remove with a few commands. Instead of clicking through a cloud console and hoping you remember every setting, you describe what you want in plain text files, review a preview of the changes, and let Terraform make them in the right order.

We will build a small but realistic project on AWS: a private, versioned Amazon S3 bucket with input variables, outputs and tags. Along the way you will learn the ideas interviewers and teams expect you to understand, including providers, state, plans and modules. If you want a trainer to guide you through larger multi-cloud projects, our live Terraform training builds on everything covered here.

Key takeaways

  • Terraform is an infrastructure as code tool that manages cloud and on-premises resources through human-readable configuration files.
  • The core workflow is write, plan and apply, with terraform init preparing the folder first.
  • Providers are plugins that talk to platform APIs, and you pin their versions in a required_providers block.
  • State (terraform.tfstate) maps your configuration to real resources; teams keep it in a remote backend with locking.
  • Variables, outputs and modules turn a one-off script into reusable infrastructure.

What is Terraform?

Terraform, from HashiCorp, is an infrastructure as code (IaC) tool. You define resources such as networks, virtual machines, storage buckets, DNS records or Kubernetes objects in configuration files written in the Terraform language (often called HCL). Terraform compares those files with what already exists and works out the exact create, update or delete actions needed.

Advertisement

It works across many platforms through providers published in the Terraform Registry, including AWS, Azure, Google Cloud, Kubernetes and GitHub. That breadth is what makes Terraform such a useful skill for DevOps, cloud engineering and platform engineering work in the US, the UK and beyond.

Why use infrastructure as code?

  • Repeatable: the same files build the same environment for dev, test and production.
  • Reviewable: changes go through Git and pull requests like application code.
  • Predictable: you see a plan before anything changes.
  • Documented: the configuration itself describes what is running.

Terraform tutorial for beginners: core concepts in plain English

Concept What it means
Provider A plugin that calls a platform’s API, for example hashicorp/aws. Providers are versioned separately from Terraform itself.
Resource Something Terraform creates and manages, such as an S3 bucket.
Data source Read-only information Terraform looks up, such as the current AWS account ID.
Variable An input that changes behavior without editing the code.
Output A value Terraform prints or shares after an apply.
State Terraform’s record of which real objects belong to your configuration.
Module A reusable folder of configuration. Your working folder is the root module; modules it calls are child modules.

Step 1: Install Terraform

HashiCorp publishes packages for macOS, Windows and Linux; always take the current release from the official install page. On macOS with Homebrew:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

On Ubuntu or Debian, add the HashiCorp repository and install the package:

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

On Windows, download the binary from the official install page and add its folder to your PATH. Confirm the install with terraform -version.

You also need an AWS account and credentials that the AWS CLI can use (for example through aws configure or single sign-on). Use a sandbox account, never production, while you learn.

Step 2: Create the project files

Make a new folder called tf-s3-starter and add four files. Splitting configuration this way is a common convention that keeps projects easy to read.

terraform.tf: pin Terraform and the provider

terraform {
  required_version = ">= 1.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

The ~> 6.0 constraint accepts any 6.x release of the AWS provider but not a future 7.0, which protects you from breaking changes.

variables.tf: inputs

variable "aws_region" {
  type        = string
  description = "AWS Region for all resources"
  default     = "us-east-1"
}

variable "environment" {
  type        = string
  description = "Deployment environment"
  default     = "dev"

  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging or prod."
  }
}

variable "project" {
  type        = string
  description = "Short project name used in bucket names and tags"
  default     = "reports"
}

The validation block stops a typo like “prdo” before Terraform talks to AWS at all.

main.tf: the infrastructure

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = {
      Project     = var.project
      Environment = var.environment
      ManagedBy   = "terraform"
    }
  }
}

resource "aws_s3_bucket" "reports" {
  bucket_prefix = "${var.project}-${var.environment}-"
}

resource "aws_s3_bucket_versioning" "reports" {
  bucket = aws_s3_bucket.reports.id

  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_public_access_block" "reports" {
  bucket                  = aws_s3_bucket.reports.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

A few things are worth noticing. S3 bucket names must be globally unique, so bucket_prefix asks AWS to generate a unique name that starts with your prefix (the prefix must be lowercase and 37 characters or fewer). Versioning and public access settings are separate resources in current versions of the AWS provider. Each one references aws_s3_bucket.reports.id, and that reference is how Terraform knows to create the bucket first.

outputs.tf: useful results

output "bucket_name" {
  description = "Generated name of the reports bucket"
  value       = aws_s3_bucket.reports.bucket
}

output "bucket_arn" {
  description = "ARN of the reports bucket"
  value       = aws_s3_bucket.reports.arn
}

Step 3: Run the Terraform workflow

From inside the folder, run these commands in order.

terraform init       # download the AWS provider, create .terraform.lock.hcl
terraform fmt        # format files to the standard style
terraform validate   # check syntax and internal consistency
terraform plan       # preview the changes
terraform apply      # make the changes after you type yes

init downloads providers into a hidden .terraform folder and writes the dependency lock file, .terraform.lock.hcl. Commit the lock file to Git so everyone uses the same provider versions.

plan shows every resource Terraform will add, change or destroy. Read it carefully: this is your safety net. In automation, save the plan with terraform plan -out=tfplan and later run terraform apply tfplan so exactly the reviewed plan is applied.

apply prints the plan again and waits for you to type yes. When it finishes you will see the two outputs, including the generated bucket name.

Make a change and watch Terraform update it

Change the default environment to staging, then run terraform plan again. Because the bucket prefix is part of the name and S3 names cannot be changed in place, Terraform will plan to replace the bucket rather than edit it. Spotting “must be replaced” lines in a plan is one of the most important habits you can build, because replacement can mean data loss on real systems.

Pass variables without editing code

You can override defaults on the command line, through environment variables, or with a variable definitions file:

Advertisement
terraform plan -var="environment=staging"

export TF_VAR_environment=staging

# staging.tfvars
environment = "staging"
project     = "reports"

terraform plan -var-file="staging.tfvars"

Terraform also loads a file named terraform.tfvars and any *.auto.tfvars files automatically. Command line values take precedence over files, and a variable’s default has the lowest precedence.

Clean up

Run terraform destroy to delete everything this configuration manages. It shows a plan and asks for confirmation first. AWS will not delete a bucket that still holds objects or, with versioning on, older object versions, which is a helpful guard. For throwaway practice buckets only, the force_destroy argument removes all objects on destroy, and those objects cannot be recovered.

Understanding Terraform state

After the first apply you will see terraform.tfstate and, after later runs, terraform.tfstate.backup. State is how Terraform knows which real bucket corresponds to aws_s3_bucket.reports. Without it, Terraform cannot tell what it already created.

Local state is fine for learning but risky for teams: it can be lost, and two people running apply at once can conflict. It may also contain sensitive values, so never commit it to Git. HashiCorp recommends storing state in HCP Terraform or a remote backend. On AWS, the S3 backend now supports native state locking with use_lockfile, and the older DynamoDB locking arguments are deprecated:

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

Create the state bucket separately and enable versioning on it, as the backend documentation strongly advises, so you can recover from mistakes. Handy state commands include terraform state list to see managed resources and terraform show to inspect current values.

Next step: turn it into a module

Once the configuration works, move the bucket resources into a folder such as modules/secure-bucket and call it from your root module:

module "reports_bucket" {
  source      = "./modules/secure-bucket"
  project     = "reports"
  environment = "dev"
}

Now every team that needs a private, versioned bucket gets the same secure defaults with three lines of code. Modules are where Terraform starts paying off at scale, and they come up constantly in DevOps interviews.

Common beginner mistakes to avoid

  • Skipping terraform plan or not reading the replace and destroy lines.
  • Committing terraform.tfstate or credentials to Git. Add state files and .terraform/ to .gitignore, but do commit .terraform.lock.hcl.
  • Leaving provider versions unpinned, so a new major release changes behavior unexpectedly.
  • Editing resources by hand in the console after Terraform created them, which causes drift between state and reality.
  • Putting every environment in one folder and one state file instead of separating them.

Terraform usually sits next to configuration management and CI/CD tools. If you are mapping out a wider DevOps skill set, see our guide to applying machine learning to DevOps and our Puppet interview questions, and browse all IT training courses for cloud and DevOps paths.

Learn Terraform with live, instructor-led training

Our Terraform course teaches infrastructure as code in depth: HCL, providers, state, modules, workspaces, testing and CI/CD across AWS, Azure and Google Cloud. Classes are live and instructor-led, with hands-on labs and guidance for the HashiCorp certification exam.

Enroll Now Request a Free Demo

Frequently asked questions

Is Terraform easy to learn for beginners?

The basics are approachable, especially if you already know one cloud platform, and the small project in this tutorial is a realistic first goal. The harder parts are state management, module design and safe changes in shared environments, which come with practice.

Do I need to know programming to use Terraform?

No. The Terraform language is declarative, so you describe the result rather than writing step-by-step logic. Familiarity with variables, lists and maps helps, and expressions and functions become useful as projects grow.

What is the difference between Terraform and Ansible?

Terraform focuses on provisioning infrastructure and tracking it through state. Ansible focuses on configuring servers and applications through playbooks. Many teams use Terraform to create resources and a configuration tool to set up what runs on them.

Is Terraform free to use?

The Terraform CLI can be downloaded and used without charge. HashiCorp also offers HCP Terraform, a hosted service for remote state, runs and collaboration, with its own plans. Review the current license and plan terms on HashiCorp’s website for commercial use.

Which cloud should I learn Terraform on?

Pick the cloud you use at work or the one most common in the jobs you want. AWS is a common starting point with plenty of official examples, but the workflow you learn here is the same on Azure and Google Cloud.

Sources

  • HashiCorp Developer: What is Terraform? (developer.hashicorp.com/terraform/intro)
  • HashiCorp Developer: Install Terraform
  • HashiCorp Developer: Create infrastructure (AWS get started tutorial)
  • HashiCorp Developer: Input variables; Output values; Modules overview
  • HashiCorp Developer: State; Backend type s3; Dependency lock file
  • HashiCorp Developer: terraform plan and terraform destroy command references
  • Terraform Registry: hashicorp/aws provider, aws_s3_bucket and aws_s3_bucket_versioning resources
Advertisement

About Tutorials Mania Editorial Team

The Tutorials Mania Editorial Team writes and maintains the site's technology tutorials, interview guides and coverage of AI tools and platforms. Every article is researched against official documentation, product announcements and primary sources, written to explain complex technology in plain language, and updated when products change.
View all posts by Tutorials Mania Editorial Team →