
Your First Terraform Project on AWS: Deploy a Static Website to S3 in 15 Minutes
A beginner-friendly, step-by-step guide to Infrastructure as Code: create an S3 bucket, host a static website, and tear it all down with one command using Terraform.
If you've only ever created AWS resources by clicking around the console, Infrastructure as Code (IaC) can feel intimidating. In this guide, we'll use Terraform to deploy a simple static website to Amazon S3 — a small project that teaches you the core Terraform workflow.
By the end you'll know how to:
- Configure the AWS provider
- Create and configure an S3 bucket with code
- Upload files with Terraform
- Tear everything down with one command
Prerequisites
- An AWS account (cost for this project is a few cents at most)
- AWS CLI installed and configured (
aws configure)
- Terraform installed (
terraform -version)
Step 1: Create the project structure
terraform-s3-website/├── main.tf├── variables.tf├── outputs.tf└── site/ └── index.htmlStep 2: Write a simple page
Create
site/index.html :<!DOCTYPE html><html> <head><title>Hello from Terraform</title></head> <body> <h1>Deployed with Terraform 🚀</h1> <p>This page lives in an S3 bucket created entirely with code.</p> </body></html>Step 3: Define variables
Create
variables.tf :variable "region" { description = "AWS region" type = string default = "eu-central-1"}
variable "bucket_name" { description = "Globally unique S3 bucket name" type = string}S3 bucket names must be globally unique, so pick something like
yourname-tf-website-2026 .Step 4: Write the main configuration
Create
main.tf :terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }}
provider "aws" { region = var.region}
resource "aws_s3_bucket" "site" { bucket = var.bucket_name}
resource "aws_s3_bucket_website_configuration" "site" { bucket = aws_s3_bucket.site.id
index_document { suffix = "index.html" }}
# Allow public bucket policies (blocked by default)resource "aws_s3_bucket_public_access_block" "site" { bucket = aws_s3_bucket.site.id
block_public_acls = true ignore_public_acls = true block_public_policy = false restrict_public_buckets = false}
resource "aws_s3_bucket_policy" "public_read" { bucket = aws_s3_bucket.site.id depends_on = [aws_s3_bucket_public_access_block.site]
policy = jsonencode({ Version = "2012-10-17" Statement = [{ Sid = "PublicReadGetObject" Effect = "Allow" Principal = "*" Action = "s3:GetObject" Resource = "${aws_s3_bucket.site.arn}/*" }] })}
resource "aws_s3_object" "index" { bucket = aws_s3_bucket.site.id key = "index.html" source = "${path.module}/site/index.html" content_type = "text/html" etag = filemd5("${path.module}/site/index.html")}A few things worth noticing:
- Each S3 feature is its own resource. Since AWS provider v4, website config, policies, and access blocks are separate resources instead of one giant block.
-
depends_onmakes sure the public access block is updated before the policy is applied — otherwise AWS rejects the public policy.
-
etagmakes Terraform re-upload the file whenever its content changes.
Step 5: Add an output
Create
outputs.tf :output "website_url" { value = "http://${aws_s3_bucket_website_configuration.site.website_endpoint}"}Step 6: Deploy
terraform initterraform plan -var="bucket_name=yourname-tf-website-2026"terraform apply -var="bucket_name=yourname-tf-website-2026"Type
yes , wait a few seconds, and open the website_url from the output. Your site is live! 🎉Try editing
index.html and running terraform apply again — Terraform detects the change and re-uploads only that file.Step 7: Clean up
terraform destroy -var="bucket_name=yourname-tf-website-2026"This is the real power of IaC: everything you created is removed in one command — no forgotten resources quietly costing you money.
What's next?
Once this works, try extending it:
- Put CloudFront in front of the bucket for HTTPS and caching (and make the bucket private with Origin Access Control)
- Store Terraform state remotely in S3 with state locking
- Deploy automatically with GitHub Actions on every push
Key takeaways
- Terraform's core loop is init → plan → apply → destroy
-
planshows exactly what will change before anything happens
- Small projects like this are the best way to learn IaC before moving on to VPCs, ECS, or full environments
What was your first Terraform project? Let me know in the comments 👇
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article