Using Terraform for Static S3 Site Infrastructure Configuration in AWS

Using Terraform for automated, reproducable infrasctructure configuration in AWS

What is Terraform?

Terraform

Terraform is a tool for building, changing, and versioning infrastructure. Terraform uses configuration files to describe the components needed to configure your infrastructure to host a single site or application or even an entire data centre. Terraform generates an execution plan describing what it will do to reach the desired states, and then executes it to build the described infrastructure.

Using Terraform to automate the setup of hosting a static site over S3

In some past posts, I detailed the manual steps for setting up the infrastructure configurations necessary to host a static web site via AWS S3. However, the steps and long and complex to follow should you ever have the need to set up another static site again.

Having something repeatable that is able to complete the infrastructure setup as needed can be beneficial and Terraform is a great tool to use to do so.

Terraform Syntax

Terraform uses a proprietary Hashicorp Configuration Language (HCL) syntax to create .tf files that detail the configurations that will be applied to your cloud service provider of choice. They toute this syntax as “…meant to strike a balance between human readable and editable as well as being machine-friendly”.

Terraform Syntax

Amazon Web Services is the most popular and most widely used with Terraform currently.

Terraform AWS Documentation

Following the Terraform configuration examples in their documentation, you can construct a series of .tf files to specifically configure AWS services to host whatever your applications may require.

The Core Items for a Static S3 Site

Refering to the past posts for the manual configurations, we utilize the following services:

  • S3 (Bucket for static site object storage)
  • Route 53 (Hosted Zone and DNS records)
  • CloudFront (Distribution Cache and HTTPS routing)
  • Certificate Manager (SSL Certificate)

So our Terraform configurations will need to configure each of these services.

(Note that creating a custom domain requires some external interventions and validations that are not automated via Terraform, so any cusotm domains being used should be purchased and configured via Route 53 or any domain provider of your choice beforehand.)

Running Terraform

Once you have your .tf files created, you can run:

$ terraform plan

to output the created plan to be sent to AWS to execute the infrastructure creation.

You can review the output of the plan to ensure your configuration files are creating the necessary values for AWS to use to configure the infrastructure services and adjust your values accordingly if something seems wrong.

Once things are ready to go, you can run:

$ terraform apply

The Inputs

Terraform will require access to your AWS account, so you will need to provide to the configurations your access key and secret key as well as any other defined variables you have created.

I created a set of Terraform configuration files hosted on GitHub to create a base static S3 site taking in the following inputs:

  • Primary (Root) domain
  • Secondary (Wildcard) domain
  • Access Key
  • Secret Key

terraform-s3-static-site-config

If you do not wish to input these values, you can create a terraform.tfvars file and input the variable values you wish to be passed in to Terraform to use.

Example:

primary_domain = "example.com"
secondary_domain = "*.example.com"
access_key_val = "21354ewq54rq564r"
secret_key_val = "8795142780951jkb512bhkj51kjhb51"

The Site

The static S3 site will only host one index.html file in the S3 bucket created, but this should be available for access once the scripts are run to completion successfully using your custom domain.

The Outputs

Once Terraform has completed the infrastructure setup, it should output the list of DNS servers for your domain. You can use these to populate the DNS records for your custom domain host service should you not be hosting your domain via AWS Route 53.

Built with Hugo