Building apps with AWS CDK (v1) for Golang, Api Gateway and Lambda

Harshana Abeyaratne
5 min readOct 17, 2021

--

If you rather look at code, repository for this blog is at https://github.com/harshq/test-api-go-aws-cdk

Infrastructure as code is a common practice in modern applications. AWS provides a CDK (Cloud Development Kit) to do exactly that. However if you were to build a Go application on AWS, you would have to use another language (like Typescript, origin language for CDK) to write your infrastructure code. Why ? Because AWS CDK does not support Go. That’s until now! Developer preview for AWS CDK with Go support landed couple of months ago (April 2021). In this early look at CDK (v1.118.0-devpreview), will see how we can build a simple HTTP api with AWS CDK for Go, Api Gateway v2 and Lambda.

Pre requisites: I’m going to assume that you already have the AWS CDK installed and configured. If you haven’t, you will have to follow this guide to install the CDK and configure it. It will be useful to understand the fundamentals of AWS CDK too. Read about it here.

1. Create a new directory 🛠

mkdir test-api && cd test-api

We will call it test-api. This directory name will be the CDK project name.

2. Initialize a CDK project with Go 🚧

cdk init app --language=go

This creates a new CDK project inside the directory. It also shows some useful commands that we will be using later.

If your code editor shows a squiggly line in go.mod file like mine, run :go mod tidy

3. Open test-api.go ✍️

This is where your infrastructure code (stack) lives. CDK already have added some boilerplate code for us to get started. You will notice there is a comment in NewTestApiStack function to define our stack. There is already an example that defines a new AWS SNS topic.

Let’s start by getting rid of that SNS topic.

4. Define your stack 😎

We are aiming to build a simple http api with lambda. So we need to define an API gateway, a lambda and an integration.

Let’s see what we have added.

First we create a new HTTP api on API Gateway v2. awsapigatewayv2 exposes a NewHttpApi factory function that takes a stack, a name string and a set of options. (You may notice the use of jsii and pointer types. We will discuss why in the end this blog)

We also define a Go Lambda function, that sits in the app directory (We don’t have one yet. Will add this in the next step). We set the memory size to 128MBs (that’s plenty for a simple hello world)

This is to add the lambda proxy integration. We pass the helloFunc we defined previously as the handler.

Finally, We add a route to the HTTP api we defined previously and link the integration. We also set the path to /hello.

5. Add your handler 🙌

mkdir app && touch app/main.go

Add your handler code in main.go file that you just created. Mine looks like below.

What’s left to do is to deploy it.

5. Deploy it 🚢

Let’s see if everything is in order. We can check the current stack by running below command in the root directory (make sure you aren’t inside app dir).

cdk ls

If that doesn’t say CdkLambdaStack, check your code again.

Next we can run below command if you want to see the CloudFormation template. This is an optional step. Deploy command that we are going to run next will automatically synthesize your stack before each deployment.

cdk synth

Finally, run below command to deploy.

cdk deploy

CDK will show a summary of IAM changes this deployment will make. Confirm once you check everything.

Voila, Everything is deployed now. Unfortunately deploy command isn’t going to print the api endpoint for http apis (surprisingly it did for rest apis 🤔). So Go to your api gateway and find the invoke URL.

6. Test it 😻

Now that we have the invoke URL, we can test it. We added a route /hello so:

Final thoughts

You might wonder why there are so many pointers. I’m not gonna lie, it felt bit strange at first. But this is to allow optional types. You may know that theres no native way to allow optionality in Go. Methods provided by JSII make it possible to pass pointer types little more ergonomically and removes the need for allocating a variable just to get a pointer.

Now, I don’t think we should use AWS CDK for Go on production apps just yet. But It’s pretty cool to know what’s coming. Also I would like to appreciate the amazing work AWS CDK team and contributors are doing to make this possible 👏 Hopefully a stable release will come soon.

Repository for this blog is at https://github.com/harshq/test-api-go-aws-cdk

Cheers!

--

--

Harshana Abeyaratne
Harshana Abeyaratne

Written by Harshana Abeyaratne

Software product engineer | UX enthusiast | Speaker

No responses yet