Golang: Build and Deploy an AWS Lambda using CDK ๐Ÿš€

thomaspoignant

Thomas Poignant

Posted on May 26, 2021

Golang: Build and Deploy an AWS Lambda using CDK ๐Ÿš€

If you read my previous posts you can see that I am working with Golang and AWS a lot, so in this one, I will present how I deploy a serverless API in AWS Lambda with an API Gateway to access it.

I will write the API using GO and go-chi/chi to handle the http routing layer.

TL;DR

All the sources are available in thomaspoignant/cdk-golang-lambda-deployment repository.

Create a serverless API in GO

So the first things we will do is creating an API using go-chi and the aws-lambda-go SDK, so we need to import these dependencies:

go get github.com/aws/aws-lambda-go
go get github.com/awslabs/aws-lambda-go-api-proxygo get github.com/go-chi/chi
go get github.com/go-chi/render
Enter fullscreen mode Exit fullscreen mode

You can see that weโ€™ve also imported github.com/awslabs/aws-lambda-go-api-proxy this framework allows using go-chi with a lambda handler.

Now we can create our main.go file that contains the logic of our API.

In this file, we have 2 important functions, main() and handler().

This is important to understand that the main() function is called only when the lambda is starting, so donโ€™t do anything in the main() if you want that action to happen for each API request.

The handler() function is where the lambda is called, so you will go on this function every time a request comes in, and since we are using a chiadapter the handler will use the go-chi router to handle the requests.

We are using this adapter to keep all the benefits from a framework to handle the http layer, it allows us to have better routing, but also to use some fancy helper to get your params, or write your responses (in the repo we are using go-chi-render for that).

๐Ÿ‘‡You can check the repo to see the complete code of the lambda.๐Ÿ‘‡

Build & Deploy our lambda with CDK

Now that our lambda function is ready we will have to build it and deploy it.

So our CDK stack will contains these steps :

  • Create the GO binary we will use in our lambda
  • Deploy the code as a lambda function
  • Deploy an AWS API Gateway to access this API on the internet.

Create GO binary and deploy it

Since we are using CDK it is to serve and build the lambda inside our infra as code.

So as you can see the lambda.Function object is taking 3 parameters:

  • runtime: information on the language of the lambda (here we are using GO)
  • handler: the name of your go binary
  • code: the code to run in the lambda, as you can see we are using lambda.Code.fromAsset this takes the code location and create the binary from the code. To execute the build, CDK is launching a docker image (you need docker on your machine) and use the makefile to build the binary with no dependency of the current machine running CDK.

Add an API Gateway to access the lambda

Since lambda functions are not accessible directly through HTTP, letโ€™s add an API Gateway in front of our lambda function.

arch schema

Doing this is super easy with CDK, this is only one object creation call to LambdaRestApi with the lambda you just created and a description.

It will do everything for you under the hood, by creating a /{proxy+} route on the API Gateway and the associated deployment, it means that all the routes will go directly to lambda function and you have to manage the routing on your code, and this is EXACTLY what we want to do!

API Gateway in the console.

After cdk deploy, you can get API Gateway URL

Conclusion

As you can see with not much code we built/deployed and exposed a serverless API.

You can check the full repo here, and use this as an example to start building a more complex logic in your API.

It is also super easy to manage more than 1 lambda function in your stack, you just have to call multiple time the CDK functions so enjoy โœŒ๏ธ

cdk-golang-lambda-deployment

This repo is an example on how to deploy a serverless API on AWS Lambda using aws CDK.

Associated article https://faun.pub/golang-build-and-deploy-an-aws-lambda-using-cdk-b484fe99304b

Lambda

The GO Lambda function is a simple API using go-chi/chi to handle http requests.
The code is located in the /lambda/api folder.

Deployment with CDK

The CDK stack is also simple, it build the binary of the lambda and deploy it with an API Gateway in front of this lambda to access it through HTTP.
The code is located in the /infra-cdk folder.




๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
thomaspoignant
Thomas Poignant

Posted on May 26, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About