Home > Categories > Aws cdk > Build Golang AWS Lambda Function in Docker with CDK

Home > Categories > Lambda > Build Golang AWS Lambda Function in Docker with CDK

Home > Build Golang AWS Lambda Function in Docker with CDK

Build Golang AWS Lambda Function in Docker with CDK

Updated:

25 Jan 2021

Published:

25 Jan 2021

When building golang lambda function on windows or macOS we try to emulate linux with GOOS=linux and set x-86 intel based 64 bit architecture with GOARCH=amd64.

So we are emulating the production environment. And this will work fine most of the time.

But there are always edge cases where your lambda will crash because it was not built in the native production environment.

And maybe its just me but I spend hours debugging lambda function just to find out I forgot to zip the lambda function.

Something simple as remember to update zip file with newly built lambda function can drive you crazy.

So the solution is to build lambda function inside Docker container that mimics very closely the production aws lambda environment. So all of your edge cases will be resolved.

And use CDK to keep track of if the code has changed then build and deploy updated function to the cloud.

To read rest of the article please Purchase Subscription

View Source code on github

Before we get started I would recommend to develop CDK project inside development docker container.

So we no longer have the issue of "this works on my computer". And it will not mess with your existing workflow.

So checkout my blog post for detailed video explanation.

Then checkout CDK development container settings on github.

Pro Tip: Enable Subtitle

View Source code on github

So first create new CDK project, create lambda-fns/hello-world directory and add following golang lambda

We are not focusing on learning to write golang lambda function but on building and deploying the function.

So this is very basic function that returns simple string and nil error on every invocation.

Then add following code in your cdk project stack.

We have created lambda function as always except we have added bundling options.

The bundling allows us to specify docker image for building golang lambda function.

Also in bundling we can specify the build command.

Notice we don't need to specify operating system to linux and architecture to x86-64bit.

Docker image takes care of all of these settings. You can modify the commands to suite your specific needs.

The main advantage of this is you don't need to remember if you have built and zipped the code after you have changed it.

CDK will automatically run build process if any file inside specified directory has changed.

View complete source code on github

Conclusion

With serverless architecture you will break your monolithic architecture into 700+ lambda function for single app.

But thankfully CDK makes it super easy to build them and keep them updated.

Hence you should definitely use CDK for golang lambda functions.

Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.

If you like this article please consider purchasing paid