For a side project, I was expecting to host a static website. I then realized that it wasn't that easy to get it cost effectively. I couldn't found an easy way to tick all the following features:
- Hosted: I don't want to self-host it at home.
- low cost: I want it to be cost effective - it's a personal project after all
- static files: I don't want to fire up a server instance for a couple of pages
- https support: wake up we're in 2022!
- custom domain: I owned a domain at gandi.net and I don't want to change
- no repo access: don't ask me why but I'm reluctant to share my repo access (bye cloudflare)
Then I wrote the following Terraform file to host on AWS using my gandi.net domain. The tricky part here is that as s3 hosting http only, you need to setup Cloudfront which can be quite painful:
- I need to declare the subdomain zone in Route53
- I get the NS from this new Route53 domain to create them in my domain hosted at gandi.net
- Aws Certificate Manager can then issue the certificate by using custom CNAME record check.
- Everything is then ready to setup the Cloudfront distribution.
- I secure the s3 access by using an OAI (Origin Access Identity) keep it private
Easy ? No The following module automate this task using Terraform
You need:
- an AWS account with API keys and check that the free tier cover the costs (I bet)
- a domain name managed on gandi.net and generate the API key
- Set up the AWS CLI
- Write your Gandi API KEY in a file called
~/gandi/API_KEY
. Remember to limit the access to this file (600) - [Optional] The script
./initialize-terraform.sh
automatically set the required env variables for you. It uses the values of the default AWS profile. whether you need to customize your aws config, you should to define them as follow- Rename the file
terraform.tfvars.example
asterraform.tfvars
- edit it with your custom values_
- Rename the file
- Edit the main.tf file following the template example
- [The first time only] Run
./initialize-terraform.sh init
to download the dependencies - Run
./initialize-terraform.sh validate
to check the file syntax - Run
./initialize-terraform.sh plan
to check planned actions - Run
./initialize-terraform.sh apply
to create the infra
Et voilà!