This repository provides a bootstrap setup to deploy and serve a static website using AWS services, Terraform for infrastructure as code, and GitHub Actions for CI/CD. Mileage may vary for costs but unless you've got the next big thing, it should be around $0.60/month.
Follow these steps to get started with deploying your static website on AWS.
Purchase a domain name from your preferred domain registrar (e.g., GoDaddy, Namecheap, etc.).
If you don't have an AWS account, create one here.
- Log in to your AWS Management Console.
- Navigate to the S3 service.
- Create a new S3 bucket to store your Terraform state files. Note down the bucket name as you will need it in the Terraform configuration.
- Navigate to the IAM service in your AWS Management Console.
- Create a new IAM role with the following trust policy to allow GitHub Actions to assume the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::YOUR_ACCOUNT_ID:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:sub": "repo:YOUR_GITHUB_REPO:ref:refs/heads/main"
}
}
}
]
}- Attach the necessary policies to the role (e.g.,
AdministratorAccessif you're lazy and insecure or a custom policy with the required permissions to manage S3, Cloudfront, Route53, ACM ). - Note down the role ARN as you will need it in the GitHub Actions workflows.
- Navigate to your
.github/workflowsdirectory. - Open the workflow files and set the
AWS_REGIONenvironment variable to your desired AWS region (e.g.,us-east-1).
Example:
env:
AWS_REGION: us-east-16. Configure S3_WEBSITE_BUCKET Value in deploy-website.yaml
- Open the deploy-website.yaml workflow file located in .github/workflows/.
- Set the S3_WEBSITE_BUCKET value to the name of the S3 bucket where your static website files will be stored.
Example:
env:
S3_WEBSITE_BUCKET: your-website-bucket-name7. Configure Desired Values in locals.tf
- Navigate to the terraform/ directory.
- Open the locals.tf file.
- Set your desired configuration values such as domain_name, bucket_name, and other parameters.
Example:
locals {
aws_region = "eu-west-2"
tf_state_bucket_name = "tf-state-bucket"
s3_bucket_name = "super-awesome-website"
domain_name = "example.co.uk"
root_file = "index.html"
error_file = "error.html"
}Follow guidance from the domain registrar you bought your domain from to configure the domain nameservers to point to AWS Route 53.
Merge your changes to main and let the Github Actions workflow take care of the rest!
The Terraform: CD action terraform-apply.yaml is manually triggered by default, but you can change this in the workflow file by adding an event trigger like on push to main:
on:
push:
branches:
- mainWith all of your config setup, you can now deploy your resources using Terraform. You can do this by running the Terrafrom: CD Github Actions workflow manually from the Actions tab in your repository.
NOTE: The SSL certificate may require you to take some action first to validate the domain. This can be done by adding a CNAME record to your domain's DNS settings. The certificate will be created in the
us-east-1region, so you may need to switch to that region in the AWS Console to see the certificate. Your terraform may fail on the first run.
Once you have your resources deployed, you can deploy your web content to the S3 bucket. You can do this by running the Deploy Website Github Actions workflow manually from the Actions tab in your repository. This will run an S3 Sync command to copy the contents of the src directory to the S3 bucket.
Assuming all went well (This whole process is (at time of writing) untested), you should now be able to access your website at the domain you configured.