Website on CloudFront
This page serves a static website over HTTPS, from edge locations close to your visitors, out of an S3 bucket that nobody can read directly. It is the production version of the site in Static Website, which answers on S3's own endpoint over plain HTTP.
CloudFront bills per request and per gigabyte served, and S3 per gigabyte stored, so a small site with little traffic costs cents a month.
The definition file
Start a project, and put the site's pages in a website/ folder beside octo.yaml, with an
index.html and an error.html:
npx @quadnix/octo-build create-app -n shop-website
cd shop-website
mkdir website
Then add a AWS_S3_WEBSITE_BUCKET_NAME entry to the env of octo.yaml, holding a bucket name of
your own, and set AWS_ACCOUNT_ID to your account number. Replace the imports and modules with
these, keeping the rest of the file as create-app wrote it:
imports:
- className: SimpleAppModule
importPath: "@quadnix/octo-aws-cdk/modules/app/simple-app"
- className: AwsIniAccountModule
importPath: "@quadnix/octo-aws-cdk/modules/account/aws-ini-account"
- className: AwsServerlessRegionModule
importPath: "@quadnix/octo-aws-cdk/modules/region/aws-serverless-region"
- className: AwsS3StorageServiceModule
importPath: "@quadnix/octo-aws-cdk/modules/service/aws-s3-storage-service"
- className: AwsCloudFrontServiceModule
importPath: "@quadnix/octo-aws-cdk/modules/service/aws-cloudfront-service"
- className: AwsCloudFrontInvalidationServiceModule
importPath: "@quadnix/octo-aws-cdk/modules/service/aws-cloudfront-invalidation-service"
modules:
- moduleId: app-module
moduleClass: SimpleAppModule
moduleInputs:
name: shop-website
- moduleId: account-module
moduleClass: AwsIniAccountModule
moduleInputs:
accountId: "${env.AWS_ACCOUNT_ID}"
app: "${{app-module.model.app}}"
- moduleId: region-module
moduleClass: AwsServerlessRegionModule
moduleInputs:
account: "${{account-module.model.account}}"
name: website-region
regionId: aws-us-east-1
# The files, in a closed bucket.
- moduleId: website-storage-module
moduleClass: AwsS3StorageServiceModule
moduleInputs:
bucketName: "${env.AWS_S3_WEBSITE_BUCKET_NAME}"
directoryPath: website
region: "${{region-module.model.region}}"
# The distribution serving them.
- moduleId: website-cloudfront-module
moduleClass: AwsCloudFrontServiceModule
moduleInputs:
account: "${{account-module.model.account}}"
distributionName: shop-website
defaultRootObject: index.html
priceClass: PriceClass_100
errorResponses:
- errorCode: 403
responseCode: 404
responsePagePath: /error.html
- errorCode: 404
responseCode: 404
responsePagePath: /error.html
origins:
- originId: website
source: "${{website-storage-module.model.service}}"
routes:
- originId: website
# A fresh copy at the edge every time the files change.
- moduleId: website-invalidation-module
moduleClass: AwsCloudFrontInvalidationServiceModule
moduleInputs:
cloudFront: "${{website-cloudfront-module.model.service}}"
source: "${{website-storage-module.model.service}}"
The bucket is a plain storage bucket rather than a website bucket, and that choice is what keeps it closed. Serving it through the distribution adds a statement to the bucket's policy naming that one distribution, so the files are reachable through CloudFront and nowhere else. Removing the distribution later can never reopen the bucket.
The bucket name cannot contain a dot. CloudFront reaches the bucket over HTTPS at
<bucket>.s3.<region>.amazonaws.com, and the certificate AWS serves there covers a single label, so
a dotted name is refused before anything is created.
errorResponses turns a missing page into a real 404 carrying your error page. A closed bucket
answers 403 for a file it does not have, which is why both codes are mapped. priceClass decides
which edge locations serve the site, and PriceClass_100 keeps to the cheapest ones.
Deploy it
npm run octo:generate
npm run octo:validate
npm run octo:lock
npm run octo:apply
npm run octo:commit
The apply takes several minutes, because CloudFront spreads a new distribution to its edge locations before it reports it ready. When it finishes, the site answers on the distribution's domain:
aws cloudfront list-distributions --query "DistributionList.Items[].DomainName" --output text
Change a page
Edit a file under website/ and run the five commands again. The apply uploads the changed file,
and then the invalidation module tells CloudFront to drop what its edge locations were holding, so
the next visitor gets the new version.
Without the invalidation, the upload would succeed and the site would keep serving the old file for as long as the cache policy allows, which is a day. The invalidation runs after the upload, so it never drops the cache before the new file is there to replace it.
Your own domain
Add your domain to the distribution, with a certificate that covers it:
- moduleId: website-cloudfront-module
moduleClass: AwsCloudFrontServiceModule
moduleInputs:
aliases:
- www.shop.example.com
certificateArn: arn:aws:acm:us-east-1:123456789012:certificate/your-certificate-id
# ...the rest as before
CloudFront reads certificates from us-east-1 only, wherever the bucket lives, so the certificate
is issued there. The certificate and the DNS record pointing your domain at the distribution are
yours to create; Octo creates neither.
A site of folders
Documentation generators and most static site builders write every page as a folder holding an
index.html, so /guide lives at /guide/index.html. CloudFront serves exact paths, and asks the
bucket for /guide, which does not exist.
Give the origin an indexDocument, and both /guide and /guide/ serve the folder's
index.html:
origins:
- originId: website
source: "${{website-storage-module.model.service}}"
indexDocument: index.html
Octo creates a small CloudFront Function that rewrites those paths on every request to that origin. You write none of it.
Tear it down
Comment out the storage, distribution and invalidation modules, then run the lifecycle with
--auto-approve on the apply. Deleting a distribution takes longer than creating one, because
CloudFront disables it at every edge location first.