Deploying This Static Website

How I deploy this static website generated by Hugo to an AWS S3 bucket and use CloudFront to serve it.

Preamble

If you read many tech blogs, you’ll probably have seen the CSS of my website before. It is the minimo theme for Hugo and it seems to be somewhat popular. I used to deploy this site to an EC2 instance and put Nginx infront of it, but I figured that it is probably a lot easier to manage a static website using a cloud provider’s CDN than to manage my own virtual machine. It means that I get to deploy my website with better performance, and I also don’t have to log into my instance and run yum update every time a scary looking CVE comes out. It’s also significantly cheaper to serve a website out of an S3 bucket.

Deployment instructions

The Cloudformation template that runs this site is (mostly) automated and you can find it on the repository for this website.

The site is split across two Cloudformation stacks. CloudFront only accepts TLS certificates from Amazon Certificate Manager in us-east-1, so the certificate lives in its own stack in that region, while everything else is deployed in ca-central-1 so the content is hosted in Canada.

Development environment

The repository has a Nix flake that provides the tools needed for deployment, starting with the AWS CLI. This requires Nix with flakes enabled.

  1. Clone the repository and change into it: git clone https://github.com/thaniri/personalwebsite.git && cd personalwebsite
  2. Start the development environment, either:
    • with direnv (ideally with nix-direnv for caching): run direnv allow once, and the environment loads every time you change into the directory, or
    • without direnv: run nix develop to start a shell with the environment.
  3. Configure AWS credentials outside of the repository, for example with aws configure sso (or aws configure) which stores them under ~/.aws. Credentials are never committed to the repository.
  4. Select the AWS profile for this repository: echo 'export AWS_PROFILE=<profile name>' > .envrc.local, then direnv reload. .envrc.local is gitignored. Without direnv, run export AWS_PROFILE=<profile name> in the nix develop shell instead.
  5. Check that the AWS CLI is authenticated: aws sts get-caller-identity

Deploying the stacks

This is the process I got it down to so far, with every command run from inside the development environment:

  1. Run an AWS CLI Command to deploy the certificate stack: aws cloudformation create-stack --stack-name thaniriCertificate --template-body file://certificate.yaml --region us-east-1 --parameters ParameterKey=DomainName,ParameterValue=thaniri.ca ParameterKey=HostedZoneId,ParameterValue=Z048506537ITRMM3F4T4D
  2. Wait for the certificate stack to finish. Cloudformation creates the DNS validation CNAME record in the Route 53 hosted zone for the domain, and the stack completes once Amazon Certificate Manager has issued the certificate.
  3. Get the certificate ARN from the certificate stack’s outputs: aws cloudformation describe-stacks --stack-name thaniriCertificate --region us-east-1 --query "Stacks[0].Outputs[?OutputKey=='CertificateArn'].OutputValue" --output text
  4. Run an AWS CLI Command to deploy the website stack: aws cloudformation create-stack --stack-name thaniriS3wesbite --template-body file://template.yaml --region ca-central-1 --parameters ParameterKey=DomainName,ParameterValue=thaniri.ca ParameterKey=CertificateArn,ParameterValue=<certificate ARN from step 3>
  5. Make a merge to my repository to get travis-ci to copy over the generated HTML files to the S3 bucket.

While the process has been simplified, the waiting for the certificate validation as well as the Cloudfront resource creation does end up taking several minutes.