~/ricktimmis

$ cat aws-s3-website-hosting-static-development-and-deploy-with-gohugo-aws-localstack-and-terraform.md

AWS S3 Website Hosting, Static development and Deploy with GoHugo, AWS, Localstack, and Terraform

AWS S3 Website Hosting, Static development and Deploy with GoHugo, AWS, Localstack, and Terraform

Objectives

  • Develop a Static Website with GoHugo
  • Build S3 Static Webhosting Infrastructure using Terraform
  • Test the Infrastructure build out using Localstack
  • Deploy a test instance of the website to S3 on Localstack
  • Build S3 Static webhosting on a Production AWS
  • Deploy our Website to Production on AWS S3

Develop a Static Website with GoHugo

Developing a static website is straightforward and only requires that you write markdown files. The Quick Start Guide should have you up and running in just a few minutes

Build S3 Static Webhosting Infrastructure using Terraform

Using Terraform to define infrastructure requirements is a little more tricky. Our requirements are very small, all we need is a named S3 bucket, configured for WebHosting and a Route53 Domain record that points to the S3 host bucket. Simples!!

You will need to configure an Terraform AWS Provider, I put mine in providers.tf. Here is the main.tf Terraform file where the magic happens

# Create an S3 Bucket
resource "aws_s3_bucket" "ricktimmis_website" {
  bucket = "www.ricktimmis.com"
}


# Configure the S3 Bucket to Serve Website
resource "aws_s3_bucket_website_configuration" "ricktimmis_website_config" {
  bucket = "www.ricktimmis.com"

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }

  routing_rule {
    condition {
      key_prefix_equals = "docs/"
    }
    redirect {
      replace_key_prefix_with = "documents/"
    }
  }
}

resource "aws_route53_zone" "ricktimmis-com" {
  name = "ricktimmis.com"
}

resource "aws_route53_zone" "www-ricktimmis-com" {
  name = "www.ricktimmis.com"

  tags = {
    Environment = "web"
  }
}

# TF Create DNS Record to Serve from S3 bucket
resource "aws_route53_record" "ricktimmis_website_dns" {
  zone_id = aws_route53_zone.ricktimmis-com.zone_id
  name    = "www.ricktimmis.com"
  type    = "A"
  alias {
    name                   = "s3-website-us-east-1.amazonaws.com"
    zone_id                = aws_route53_zone.ricktimmis-com.zone_id
    evaluate_target_health = false
  }
}

Test the Infrastructure build out using Localstack

Deploy a test instance of the website to S3 on Localstack

Build S3 Static webhosting on a Production AWS

Deploy our Website to Production on AWS S3