Converting AWS Lambdas from Apex to native Go

Henry Blyth
Kudos Engineering
Published in
5 min readJul 13, 2018
“Black and white photo of potter molding clay with his hands on a spinning pottery wheel” by Quino Al on Unsplash

In this post, we assume the reader has the Go language already setup on their system. If not, see golang.org.

Go (golang) is fast becoming a popular programming language, and AWS Lambdas offer many benefits of serverless computing. It would be amazing if we could have both, right?

Back in January, Amazon announced native support for Go in AWS Lambda!

Before then, it was still possible to write your lambdas in Go, but you had to use a shim to run it from a different language that Amazon supported natively (Node.js, Java, C#, or Python).

Apex is one such example, which uses a Node.js shim. Thankfully — no disrespect to Apex! — we don’t have to make too many difficult changes to get a Go binary running our Lambda.

An Apex Lambda

Let’s start off with the Apex example, seen here in their readme. If you’re working on your own code, you can still follow along with the described changes.

If we run that as the Apex docs describe, we get the correct result: the input is uppercased.

Let’s make one change before we continue, to help us later on: uncouple the body of our lambda from the apex handler function. This is a useful first step in any refactoring process.

Refactoring

If we make a new function called handle and allow it to accept a byte array, then we can return the result/error and determine what to do with that in the apex handler. This means the main function can be replaced with whatever we want, whilst our handle function can accept and respond with raw data, uncoupled from Apex.

Let’s test it:

Using aws-lambda-go

Now that we have a main to handle incoming requests, and a handle function that receives data and returns a response, we can begin to replace apex with aws-lambda-go.

First we need two new packages, so please install these:

  1. go get github.com/aws/aws-lambda-go/events
  2. go get github.com/aws/aws-lambda-go/lambda

Now lets import them:

Next, swap apex.HandleFunc for lambda.Start:

Unfortunately, we are getting some type errors:

Firstly, the type of r.Body is string, so we need to change that to a byte array to work with our handle function.

Secondly, the return type of our handler is our own struct, whereas we can instead respond with an events.APIGatewayProxyResponse, including the value of our message struct as the Body and a nice StatusCode along with that:

Ok, let’s run it!

Hm. It’s not doing anything. Ah! That’s because the lambda has started a webserver and is listening for requests — it’s not quite working the same way as apex-go.

We need a way to test this. Fortunately, Amazon provide a way to test this lambda on AWS itself!

Testing in AWS Lambda

What we need to do is

  1. create a Go function in Lambda,
  2. upload a Linux-compatible build, and
  3. test it with a JSON request.

Create a Go function in Lambda

Go into the AWS Lambda Console and create a new function. Choose “Author from scratch” and fill out the form like so (the only important bit is choosing Go as the runtime).

Once you see “Congratulations! Your Lambda function “apextonative” has been successfully created”, scroll down to the “Function code” block and lets upload our lambda.

Upload a Linux-compatible build

First we need to build it into a binary (docs here):

Now make a zip out of just the binary, click “Upload”, and select that zip.

Finally, make sure apextonative is in the “Handler” field (see above), then click “Save” at the top of the page (it should be orange indicating unsaved changes). Once the zip has been uploaded, we can build a test!

Test with a JSON request

Select the drop-down before the “Test” button and choose “Configure test events”:

In the modal that pops up, create a new test event with the following JSON:

The format of the input JSON is slightly different to what we expected when using Apex: it’s now a string of JSON in the body parameter.

Name the event and create it: the modal will close and your new test event will be selected in the drop-down.

Click the “Test” button and we should see the following:

And that’s it!

If you like what you read and think you could contribute to our team at Kudos then take a look at our careers page to see what roles we currently have open.

--

--