Lambda @ Edge for Hugo site over S3/Cloudfront
While I did not intend to write any non-CL code in this blog, I thought this one might actually be useful for people like myself. I recently moved my Wordpress blog to a hugo static site. But instead of deploying to netlify or AWS amplify, I chose to go with S3 + cloudfront. Only after deploying, I figure that cloudfront cannot really pick up index files on its own. A little search left me with this link where they do this using a lambda function (using javascript and node runtime).
I rewrote it using the python runtime. After a bunch of debug sessions, I got it to work as expected. Here is the lambda code that I actually use for this now.
import json
import re
def lambda_handler(event, context):
record = event["Records"][0]
cf = record["cf"]
request = cf["request"]
uri = request["uri"]
request["uri"] = re.sub("\/$", "/index.html", uri)
return request
I hope it saves you all some time setting up the same thing. Now, no more python on this blog.