Golang: Build and Deploy an AWS Lambda using CDK ๐
Thomas Poignant
Posted on May 26, 2021
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.
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.๐
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.
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!
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 โ๏ธ
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.