📚 TLDR: I created a Github template repo golang-lambda-function-cdk-template that has all the things necessary to write a Golang lambda function and deploy it via CDK.

(April 2024 Update: The go1.x lambda runtime showed in this template was been deprecated by AWS. Anything you’ve built using this template should be migrated to use the provided.al2 runtime, following this migration guide . Going forward, this template will use that runtime (although the content of this blog post will stay the same).)

I’ve decided recently to take better advantage of Github’s template repository feature. Whenever I have an idea for a little project, I often feel like I’m re-inventing the wheel, trying to re-remember how to set this or that up in CDK or forgetting a “gotcha” in the implementation details.

This recently happened when I was writing a lambda function that would update my book reading project, Book & Page . I was previously updating those “Callout” blocks on the main page (the page count, currently reading, and just finished sections) manually after changing the status of a book. So I decided to write a quick Golang Lambda that would update those for me using Notion’s API, so all I’d have to do is mark my book as “Finished” and the service would take care of the rest!

After finishing that project, I abstracted it into a template repo, golang-lambda-function-cdk-template .

The Details #

The trickiest part of this is getting the CDK setup. It looks like AWS has some cool things in alpha to make Golang lambda deploys a little easier. And, compared to the mess of managing a package.json and any other dependencies, being able to build a Go program into a single-file executable is really a dream for Lambda. That being said, if you’re not familiar with Lambda or CDK, it can be a little tricky to get all the pieces to work together.

Basically, you need to:

  1. Build the executable
  2. Zip the executable
  3. Reference the zipped file in the CDK stack

All of these pieces sound like the perfect making for a Makefile! The one included in my template repo looks like this:

You’ll see that with a simple make deploy at the root of the repo, you can build, zip, and deploy your code!

In your CDK stack, the relevant code that is run when you call cdk deploy, and that references the zipped executable looks like this:

For more details, check out the full template repo . Happy coding!