Blog:Hosting a Simple Static Website with Terraform and AWS S3

Blog:Hosting a Simple Static Website with Terraform and AWS S3

The easiest Terraform tutorial for beginners.

Introduction

In this blog post, we'll explore the process of creating a simple static website using Terraform and AWS S3. Terraform is an Infrastructure as Code (IaC) tool that allows us to define and provision infrastructure in a declarative manner. We'll go through each step of the process, from setting up the provider to hosting a basic website.

Step 1: Configuring Providers

To begin, we set up the necessary providers in a file named providers.tf. This file specifies the required Terraform providers and their versions. In our case, we are using the AWS provider:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.31.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

Step 2: Creating the S3 Bucket

Our next step involves creating the main Terraform file, main.tf, which defines the AWS S3 bucket. We utilize a variable for the bucket name, specified in a separate variables.tf file:

# main.tf
resource "aws_s3_bucket" "mybucket" {
  bucket = var.bucketname
}

# variables.tf
variable "bucketname" {
  default = "firstterraformsimpleproject"
}

Step 3: Controlling Ownership and Access

We take control of the S3 bucket's ownership and grant public access, allowing everyone to view the bucket contents. This is achieved through the use of ownership controls and public access block:

#main.tf
resource "aws_s3_bucket_ownership_controls" "example" {
  bucket = aws_s3_bucket.mybucket.id

  rule {
    object_ownership = "BucketOwnerPreferred"
  }
}

resource "aws_s3_bucket_public_access_block" "example" {
  bucket = aws_s3_bucket.mybucket.id

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

Step 4: Introducing Access Control List (ACL)

For additional access control, an Access Control List (ACL) is introduced to the bucket:

#main.tf
resource "aws_s3_bucket_acl" "example" {
  depends_on = [
    aws_s3_bucket_ownership_controls.example,
    aws_s3_bucket_public_access_block.example,
  ]

  bucket = aws_s3_bucket.mybucket.id
  acl    = "public-read"
}

Step 5: Adding Website Content

To make our website more interesting, we create index.html and error.html files. The content for these files can be generated using tools like ChatGPT.

#main.tf
resource "aws_s3_object" "index" {
  bucket = aws_s3_bucket.mybucket.id
  key    = "index.html"
  source = "index.html"
  acl    = "public-read"
  content_type = "text/html"
}

resource "aws_s3_object" "error" {
  bucket = aws_s3_bucket.mybucket.id
  key    = "error.html"
  source = "error.html"
  acl    = "public-read"
  content_type = "text/html"
}

Step 6: Enabling Static Website Hosting

We enable static website hosting for our S3 bucket using the aws_s3_bucket_website_configuration resource:

#main.tf
resource "aws_s3_bucket_website_configuration" "example" {
  bucket = aws_s3_bucket.mybucket.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

Step 7: Outputting Website Endpoint

To provide useful information after deployment, we create an outputs.tf file to display the website endpoint:

#outputs.tf
output "website_endpoint" {
  value = aws_s3_bucket.mybucket.website_endpoint
}

Conclusion

By following these steps, we've successfully created a simple static website on AWS S3 using Terraform. The entire process, from infrastructure setup to website hosting, is now defined as code, providing repeatability and consistency. This project serves as a foundation for further exploration into infrastructure as code and AWS provisioning with Terraform. Feel free to modify and extend it to meet your specific needs!

Like , share this article and follow for more such articles. Hoping to see you waving at me in my journey. Thanks.