AWS Lambda functions using Go

AWS Lambda functions using Go

Featured on Hashnode

Recently, I wanted to deploy a Go service into an AWS Lambda for the first time and found myself struggling a lot more than I expected.

Even though there are plenty of great and helpful posts with examples, there where minor aspects in which they differed (from each other and from the official documentation) given that the cloud seems to evolve so fast with constant improvement updates.

This is why I decided to create my own updated version using the official lambda library for go hoping it might help someone out.

What is AWS Lambda?

Lambda is a serverless, event-driven compute service that lets you run your code without provisioning and/or managing servers, only paying for what you use which is extremely convenient.

Lambdas can be triggered with any of the other AWS services for things like:

  • File processing (S3)

  • Data and analytics (DynamoDB)

What you'll need

Create a new lambda function

Log into your account, go to the search bar on top and type "lambda". Click on the first result.

Then click "Create function".

  • Type: Author from scratch

  • Function name: lambda-example-go

  • Runtime: Amazon Linux 2023

  • Architecture: arm64

According to the AWS documentation, "Functions that use arm64 architecture offer lower cost per Gb/s compared with the equivalent function running on an x86-based CPU." This is the one we'll be using for now.

Also, notice the note box that says "you must provide your own executable or script name 'bootstrap' as the entry point". We will be using this information in a few steps.

Click on "Create function" at the bottom and leave this page open.

Build the app

Let's now create our simple Hello World service in Go.

Create your project directory

mkdir lambda-example-go
cd lambda-example-go

Import dependencies

We will need the lambda handler function to run our code. For more information, check out the official Developer Guide.

go get github.com/aws/aws-lambda-go/lambda

Create your go module

go mod init example.com/lambda-go

Download dependencies and update go.mod

go mod tidy

Write the code

Create a main.go file with the following content:

package main

import (
    "log"

    "github.com/aws/aws-lambda-go/lambda"
)

func main() {
    lambda.Start(runService)
}

func runService() {
    log.Println("Hello from AWS")
    log.Println("exiting...")
}

We will now build our application, and for this, we need to remember a couple of things:

  1. We need to use "bootstrap" as the name for our executable. This is very important, otherwise the function will fail on execution

  2. We have to use arm64 architecture for a linux OS

Run the following build command inside your project directory:

GOOS=linux GOARCH=arm64 go build -v -o ./build/bootstrap main.go

Here, GOOS defines the operating system and GOARCH the architecture of the target system our code will run on.

Now let's zip our executable file. On macOS we can do that with zip:

zip -j lambda-example-go.zip ./build/bootstrap

Let's go back to our AWS page and click on "Upload from" on "Code Source" and choose ".zip file".

Click on "Upload" and choose your recently zipped file lambda-example-go.zip

Now go to the "Test" tab and just click "Test" on the right.

Execution should succeed and you will see a status box like this one:

Click on "logs" and open the log stream

You should be able to see our log messages

And that's about it.