Serverless Continuous Delivery and Hosting for Hugo
If you're interested in hosting a Hugo generated website but don't want to worry about setting up a bunch of pesky infrastructure, look no further! This starter kit will get you up and running in Amazon Web Services in no time. It utilizes AWS CloudFormation to provision a continuous delivery pipeline that will update your website on every commit to your repository.
This is achieved by provisioning the following infrastructure in AWS:
- S3 Bucket to host your static files
- Cloudfront Content Delivery Network
- Lambda Function to run Hugo and publish output
- API Gateway that listens to your GitHub events and triggers the Lambda
How do I get started?
pip install -r requirements.txt ./deploy.py /path/to/my/.hugo-cd.yml
This script will:
- Replace some variables in the lambda function package
- Zip it up and upload it to s3
- Run the
You'll have to check your CloudFormation Console to check in on the status. Unfortunately, running the stack from scratch takes around 30 minutes (the majority of that time is waiting for CloudFront to propogate content to it's edge locations).
I'm assuming you've already set up the AWS CLI.
Once the stack is complete, you'll want to create a new WebHook on your GitHub repo. Point it to the "WebhookEndpoint" value (found in the "Stack Detail" view). No password is required.
If you're wondering where that configuration above came from and what it should contain, look no further! I personally keep the configuration file in the root of my Hugo site directory. Feel free to keep it wherever you want. Now, for the contents...
Working sample here
# used to drive AWS resource naming. enter all caveats associated with AWS resource naming here. # most notably, it must be lowercase alphanumeric. stack_name: thebbs # the final URL generated will take the form <subdomain><hosted_zone> hosted_zone: example.com # optional value. defaults to empty string. if specified, please include trailing . subdomain: "" # repo to pull your site from git_url: https://github.com/myuser/myrepo # path to the root Hugo sources folder inside your git archive # to test, download https://github.com/myuser/myrepo/master/archive.zip path_to_site: myrepo-master/ # this is an optional value. defaults to cloudfront's cert. iam_certificate_id: ASCAJE7VY5QEFGQNNK2EG
- Add support for Route53 Hosted Zones.
- Figure out better integration between AWS and GitHub.
- Is there a way to git clone? I want to avoid pulling the .zip into the lambda function.
- Secure API Gateway endpoint. We can wait for GitHub to add the functionality, or do some magic to utilize GitHub's secret parameter.
- Filter events in lambda function. Right now we build on every event in the repo. It'd be nice to only build on commits to master.
- Generate SSL Certificate for the stack. Perhaps integration with AWS Certificate Manager. Perhaps LetsEncrypt.
- (low priority) Add CloudFront Origin Access Identity to further lock down S3 resources
- Parameterize git branch
- Clean up lambda function
- Provide better feedback/logs to the caller
- Factor the JS more cleanly