Terraform Project Static Website on S3

A printed flyer needs no kitchen, no staff, and no machinery. Anyone who walks by can read it. A static website works like that flyer. It holds ready-made HTML, images, and style files that need no server-side processing. Amazon S3 stores those files and serves them to visitors at a very low cost. This project builds a small website on S3 with Terraform.

What You Will Build

Diagram: Request Path

  Visitor types website address
              |
              v
   +----------------------------+
   |   S3 Website Endpoint      |
   +-------------+--------------+
                 |
   +-------------+--------------+
   |      S3 Bucket             |
   |   +--------------------+   |
   |   | index.html         |   |
   |   | error.html         |   |
   |   +--------------------+   |
   +----------------------------+
   Bucket policy: everyone may read files

Project Files

site-project/
  main.tf
  variables.tf
  outputs.tf
  www/
    index.html
    error.html

Step 1: Create the Web Pages

www/index.html

<h1>Welcome to my Terraform website</h1>
<p>Terraform built this page on S3.</p>

www/error.html

<h1>Page not found</h1>

Step 2: Variables

Bucket names must be unique across all of AWS. Add a personal suffix to avoid a clash.

variable "bucket_name" {
  type        = string
  description = "Globally unique bucket name, for example my-terraform-site-4821"
}

Step 3: Create the Bucket

resource "aws_s3_bucket" "site" {
  bucket = var.bucket_name
}

Step 4: Turn On Website Hosting

resource "aws_s3_bucket_website_configuration" "site" {
  bucket = aws_s3_bucket.site.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

Step 5: Allow Public Reading

New buckets block public access by default. A website needs visitors to read its files, so this project opens read access on purpose. Two resources work together: one relaxes the block, and one adds a policy that allows only the read action.

resource "aws_s3_bucket_public_access_block" "site" {
  bucket = aws_s3_bucket.site.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

resource "aws_s3_bucket_policy" "site" {
  bucket = aws_s3_bucket.site.id

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid       = "PublicRead"
        Effect    = "Allow"
        Principal = "*"
        Action    = "s3:GetObject"
        Resource  = "${aws_s3_bucket.site.arn}/*"
      }
    ]
  })

  depends_on = [aws_s3_bucket_public_access_block.site]
}

The depends_on line forces Terraform to relax the block before it adds the policy. Without it, AWS may reject the policy.

Step 6: Upload the Files

The for_each argument uploads every file in the www folder with a single block.

resource "aws_s3_object" "pages" {
  for_each     = fileset("${path.module}/www", "*.html")
  bucket       = aws_s3_bucket.site.id
  key          = each.value
  source       = "${path.module}/www/${each.value}"
  etag         = filemd5("${path.module}/www/${each.value}")
  content_type = "text/html"
}

The etag setting makes Terraform re-upload a file whenever its content changes.

How the File Upload Works

fileset("www", "*.html")  -->  ["index.html", "error.html"]
                                     |
                      for_each creates one object per name
                                     |
                +--------------------+--------------------+
                |                                         |
       aws_s3_object.pages["index.html"]       aws_s3_object.pages["error.html"]

Step 7: Output the Website Address

output "website_url" {
  value = "http://${aws_s3_bucket_website_configuration.site.website_endpoint}"
}

Step 8: Run the Project

terraform init
terraform plan -var="bucket_name=my-terraform-site-4821"
terraform apply -var="bucket_name=my-terraform-site-4821"

Open the website_url in a browser. The welcome page appears. Edit index.html, run terraform apply again, and refresh. Terraform uploads only the changed file.

Security Reminder

  • Upload only content meant for the public. Every file in this bucket is readable by anyone.
  • S3 website endpoints serve HTTP only. Production sites place CloudFront in front of the bucket to add HTTPS and keep the bucket private.
  • Delete the project after practice.

Clean Up

A bucket holding files cannot be deleted unless it is empty. Add force_destroy = true to the bucket resource during practice so terraform destroy removes the files too.

resource "aws_s3_bucket" "site" {
  bucket        = var.bucket_name
  force_destroy = true
}

Key Points

  • A static website serves ready-made files without any server code.
  • S3 bucket names must be unique across every AWS account.
  • Website hosting, public access settings, and a read-only policy work together.
  • depends_on orders the public access block before the policy.
  • fileset() with for_each uploads a whole folder of files.
  • Production sites add CloudFront for HTTPS and tighter access control.