Golang: Build and Deploy an AWS Lambda using CDK 🚀
Tags: aws, cdk, go, lambda
Published: 2021-05-26 15:21:18

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`](https://github.com/thomaspoignant/cdk-golang-lambda-deployment) repository.

{% github thomaspoignant/cdk-golang-lambda-deployment no-readme %}


# 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
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
```

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`](https://github.com/thomaspoignant/cdk-golang-lambda-deployment/blob/main/lambda/api/main.go) file that contains the logic of our API.
{% gist https://gist.github.com/thomaspoignant/7563fde92386e3af3b2dc9e42d00b3b1 %}

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.👇
{% github thomaspoignant/cdk-golang-lambda-deployment no-readme %}

# 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.

{% gist https://gist.github.com/thomaspoignant/d4ed6397f256ae087f7bddcd8f7fc7dc %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2m5r5iovm3yb34tqf2d.jpeg)

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.

{% gist https://gist.github.com/thomaspoignant/3e02beff6bd57dce77b6703557029095 %}

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ne17ochrdtopb2bfih2.png)

![After cdk deploy, you can get API Gateway URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sk82c5lobn7f8672ymo5.png)

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

You can check the full repo [here](https://github.com/thomaspoignant/cdk-golang-lambda-deployment), 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 ✌️

{% github thomaspoignant/cdk-golang-lambda-deployment %}