Terraform tutorial: from install to destroy, step by step
This Terraform tutorial takes you through the whole beginner workflow on AWS in about 30 minutes: install Terraform, write a configuration with the AWS provider and one S3 bucket, run terraform init, plan and apply, change a tag (update in place), rename the bucket (replace), add an output and a variable, then terraform destroy. Every step has the commands and the output to expect.
What you need
- A terminal on macOS, Linux or Windows.
- An AWS account and credentials allowed to create and delete S3 buckets. A sandbox or personal account is best for learning.
- The AWS CLI (optional, but the easiest way to set up credentials).
- A text editor. VS Code with the HashiCorp Terraform extension gives syntax highlighting and completion.
Cost: an empty S3 bucket costs essentially nothing; S3 charges for stored data and requests. This tutorial stores nothing and ends with terraform destroy, so the cost should be zero. Still, set an AWS Budgets alert on any account you learn in, and always destroy what you create.
Don't have an AWS account? The same steps work with the local provider, which writes files instead of cloud resources; see Terraform labs for cloud-free exercises.
Step 1: Install Terraform
Terraform is a single binary. Install it with a package manager so updates are easy.
macOS (Homebrew, from HashiCorp's own tap):
brew tap hashicorp/tap
brew install hashicorp/tap/terraformUbuntu or Debian (HashiCorp's apt repository):
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 $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraformHashiCorp also publishes yum/dnf repositories for RHEL, Fedora and Amazon Linux. On Windows, download the zip from developer.hashicorp.com/terraform/install, extract terraform.exe and add its folder to your PATH (community packages exist for Chocolatey and winget too). The same page has binaries for every platform.
Check it worked:
$ terraform -version
Terraform v1.17.2
on darwin_arm64Any 1.12 or later release is fine for this tutorial and matches what the Terraform Associate 004 exam tests.
Step 2: Give Terraform AWS credentials
The AWS provider finds credentials the same way the AWS CLI does, so set them up there and Terraform picks them up. With IAM Identity Center (SSO):
aws configure sso # once: creates a profile, e.g. "learn"
aws sso login --profile learn
export AWS_PROFILE=learnWith access keys for an IAM user, run aws configure instead. Check which identity you are using:
aws sts get-caller-identityNever put access keys in a .tf file: configuration ends up in Git. The providers guide covers all the authentication options, including roles.
Step 3: Write your first configuration
Create an empty folder. Terraform reads every .tf file in it as one configuration; file names are up to you. Start with providers.tf:
terraform {
required_version = ">= 1.12"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "us-east-1"
}The terraform block says which Terraform and provider versions this code accepts. ~> 6.0 means any 6.x release. The provider block configures the AWS provider; here just the region.
Now main.tf with one resource:
resource "aws_s3_bucket" "logs" {
bucket = "acme-tutorial-logs-7f3a"
tags = {
env = "dev"
}
}A resource block has a type (aws_s3_bucket) and a name (logs). Together they form its address, aws_s3_bucket.logs, which is how Terraform refers to it. The name is only used inside Terraform; bucket is the real bucket name in AWS. S3 bucket names are global across all AWS accounts, so add your own random suffix.

Step 4: terraform init
Every new configuration starts with terraform init. It downloads the providers and sets up the backend (here the default local one):
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 6.0"...
- Installing hashicorp/aws v6.74.0...
- Installed hashicorp/aws v6.74.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!Two things appear in the folder: .terraform/, which holds the downloaded provider (don't commit it), and .terraform.lock.hcl, which records the exact provider version (do commit it). Run init again whenever you add a provider or module or change the backend. Details: terraform init.
Optional but a good habit: terraform fmt tidies the formatting and terraform validate catches typos before you talk to AWS (fmt and validate).
Step 5: terraform plan
terraform plan shows what Terraform would do, without doing it. Output trimmed to the interesting lines:
$ terraform plan
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_s3_bucket.logs will be created
+ resource "aws_s3_bucket" "logs" {
+ arn = (known after apply)
+ bucket = "acme-tutorial-logs-7f3a"
+ force_destroy = false
+ id = (known after apply)
+ tags = {
+ "env" = "dev"
}
+ tags_all = {
+ "env" = "dev"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.+ means create. (known after apply) marks values AWS assigns when the bucket exists, such as the ARN. tags_all is the AWS provider's view of your tags merged with any provider-level default tags. The last line is the summary to check every time. More in how to read a Terraform plan.
Step 6: terraform apply
terraform apply makes a fresh plan, shows it, and waits for you:
$ terraform apply
...
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_s3_bucket.logs: Creating...
aws_s3_bucket.logs: Creation complete after 2s [id=acme-tutorial-logs-7f3a]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.The bucket now exists, and a new file terraform.tfstate records it. That is Terraform's state: the link between aws_s3_bucket.logs and the real bucket. List what it tracks with terraform state list.
Run terraform plan again. It should say No changes. Your infrastructure matches the configuration. That is how you know an apply did what you meant.
Step 7: Change a tag (update in place)
Change env = "dev" to env = "prod" in main.tf and plan:
# aws_s3_bucket.logs will be updated in-place
~ resource "aws_s3_bucket" "logs" {
id = "acme-tutorial-logs-7f3a"
~ tags = {
~ "env" = "dev" -> "prod"
}
~ tags_all = {
~ "env" = "dev" -> "prod"
}
...
}
Plan: 0 to add, 1 to change, 0 to destroy.~ means update in place: the same bucket, with new tags. Apply it with terraform apply. You edit the file; Terraform works out the API call.
Step 8: Rename the bucket (replace)
Now change the bucket name to acme-tutorial-logs-7f3a-v2 and plan again:
# aws_s3_bucket.logs must be replaced
-/+ resource "aws_s3_bucket" "logs" {
~ arn = "arn:aws:s3:::acme-tutorial-logs-7f3a" -> (known after apply)
~ bucket = "acme-tutorial-logs-7f3a" -> "acme-tutorial-logs-7f3a-v2" # forces replacement
~ id = "acme-tutorial-logs-7f3a" -> (known after apply)
...
}
Plan: 1 to add, 0 to change, 1 to destroy.S3 can't rename a bucket, so Terraform must destroy the old one and create a new one. -/+ and # forces replacement tell you so. On an empty tutorial bucket that's harmless; on a bucket full of data or a database it is exactly the line to catch in review. Apply it to see the destroy and create happen.
If you only wanted to rename the resource in Terraform (the logs part), not the real bucket, you would use a moved block instead: see the moved block.

Step 9: Add an output
Outputs print values after apply and let other tools read them. Create outputs.tf:
output "bucket_arn" {
value = aws_s3_bucket.logs.arn
}Apply, and the value appears at the end:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
bucket_arn = "arn:aws:s3:::acme-tutorial-logs-7f3a-v2"Read it any time with terraform output, or terraform output -raw bucket_arn for just the string in a script.
Step 10: Add a variable
Hard-coded values make a configuration single-use. Add variables.tf:
variable "env" {
type = string
description = "Environment name"
default = "dev"
}Use it in main.tf with var.env, in the tags and in the name through string interpolation:
resource "aws_s3_bucket" "logs" {
bucket = "acme-tutorial-logs-7f3a-${var.env}"
tags = {
env = var.env
}
}Apply it. The bucket name changes to acme-tutorial-logs-7f3a-dev, so this is another replacement. Then plan with a different value:
terraform apply
terraform plan -var env=prodThe plan replaces the bucket again, because the name follows the variable: one configuration, a different bucket per environment. Values can also come from a terraform.tfvars file or a TF_VAR_env environment variable. See variables and outputs.
Step 11: terraform destroy
When you're done, remove everything this configuration manages:
$ terraform destroy
...
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
aws_s3_bucket.logs: Destroying... [id=acme-tutorial-logs-7f3a-dev]
aws_s3_bucket.logs: Destruction complete after 1s
Destroy complete! Resources: 1 destroyed.If you put objects in the bucket, the destroy fails with BucketNotEmpty: AWS won't delete a bucket with objects in it. Empty it first, or for throwaway buckets set force_destroy = true and apply before destroying. Preview a teardown safely with terraform plan -destroy. See apply and destroy.
Terraform basics: what you just used
| Concept | What it is | In this tutorial |
|---|---|---|
| Provider | Plugin that talks to one platform's API | hashicorp/aws |
| Resource | One object Terraform manages | aws_s3_bucket.logs |
| State | Record of what Terraform manages | terraform.tfstate |
| Lock file | Exact provider versions | .terraform.lock.hcl |
| Variable | Input to the configuration | var.env |
| Output | Value exported after apply | bucket_arn |
| Plan | Proposed changes: +, ~, -, -/+ | Steps 5, 7, 8 |
A .gitignore for a Terraform folder usually lists .terraform/, *.tfstate, *.tfstate.* and any .tfvars files that hold secrets. Real projects keep state in a remote backend rather than a local file.
More Terraform examples to try next
Two references wire resources together. Add a VPC and a subnet, and Terraform creates them in the right order:
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}One block, several buckets, with for_each:
resource "aws_s3_bucket" "team" {
for_each = toset(["web", "data", "ops"])
bucket = "acme-${each.key}-7f3a"
}From here, learn for_each and count, modules and remote state. Terraform commands is a cheat sheet for everything you ran today, and What is Terraform fills in the background.
Try it in a Lab
Planfile's first two Labs are this tutorial without an AWS account: Your first apply runs init, plan and apply on one S3 bucket, and Update or replace? changes a tag and then renames the bucket so you see ~ and -/+. They run on a simulated cloud on your iPhone, and both are in the free chapters.
Questions
How long does it take to learn Terraform basics?
Most engineers who know a little cloud can run the init, plan, apply workflow in an afternoon and be comfortable with variables, outputs, state and modules within a couple of weeks of regular practice.
Do I need an AWS account for a Terraform tutorial?
Not necessarily. The local, random and null providers need no cloud account, and simulators like Planfile's Labs run without credentials. For real AWS resources you need an account and credentials.
Will this Terraform AWS tutorial cost money?
An empty S3 bucket costs essentially nothing, and the tutorial ends with terraform destroy. Set an AWS Budgets alert and always destroy what you create when learning.
What is the difference between terraform plan and terraform apply?
plan shows the changes Terraform would make without making them. apply makes a plan, asks for approval and then performs the changes.
Is Terraform good for beginners?
Yes. The workflow has only a few commands, the plan shows every change before it happens, and destroy cleans up. The main things to learn carefully are state and reading plans for replacements.
Should I commit terraform.tfstate to Git?
No. State can contain secrets and goes stale when two people apply. Commit the .tf files and .terraform.lock.hcl, and keep state in a remote backend for shared work.