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 …


This content originally appeared on DEV Community and was authored by Thomas Poignant

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

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.





This content originally appeared on DEV Community and was authored by Thomas Poignant


Print Share Comment Cite Upload Translate Updates
APA

Thomas Poignant | Sciencx (2021-05-26T15:21:18+00:00) Golang: Build and Deploy an AWS Lambda using CDK ?. Retrieved from https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/

MLA
" » Golang: Build and Deploy an AWS Lambda using CDK ?." Thomas Poignant | Sciencx - Wednesday May 26, 2021, https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/
HARVARD
Thomas Poignant | Sciencx Wednesday May 26, 2021 » Golang: Build and Deploy an AWS Lambda using CDK ?., viewed ,<https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/>
VANCOUVER
Thomas Poignant | Sciencx - » Golang: Build and Deploy an AWS Lambda using CDK ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/
CHICAGO
" » Golang: Build and Deploy an AWS Lambda using CDK ?." Thomas Poignant | Sciencx - Accessed . https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/
IEEE
" » Golang: Build and Deploy an AWS Lambda using CDK ?." Thomas Poignant | Sciencx [Online]. Available: https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» Golang: Build and Deploy an AWS Lambda using CDK ? | Thomas Poignant | Sciencx | https://www.scien.cx/2021/05/26/golang-build-and-deploy-an-aws-lambda-using-cdk-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.