This article kicks off a series on building a Serverless API in Go in AWS using Lambda, API Gateway, and AWS Cognito! In this one, we’ll start by creating a simple lambda function using AWS’s tooling for Go.

Before you can follow this, you should have a basic understanding of Go, as well as an AWS account. I’ll be using VSCode on a Windows workstation, but you can very easily replicate this on Mac or Linux. Lets dive in!

Your First Go Lambda Function

Lambda is an AWS Service that allows you to write and execute serverless functions. Along with Go, it supports most of the popular development languages out there. It is essentially the backbone of any serverless offering thats hosted on AWS.

Lambda Functions may seem daunting to build and write, but once you understand the basic requirements, they become very easy to put together and deploy! Let’s write a simple Hello World Lambda and deploy it.

In VSCode, open an empty folder on your machine. Then open the integrated terminal and create a new Go module with the following command.

go mod init hello-world-lambda

You should have a single file named go.mod.

https://cdn.brianmorrison.me/media/2022/2f624659-855d-4cd3-a298-80eef274e06d

Now create your entry main.go file and populate it with the main function as usual. For good measure, add a log.Println method as well just to make sure we’re setup properly.

package main

import "log"

func main() {
    log.Println("hello world!")
}

https://cdn.brianmorrison.me/media/2022/82b3f994-7e2d-4da7-a307-b16089804f1d

Before we continue, make sure you have the aws-go-sdk installed by running the following command in the terminal.

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

Now lets modify this to support Lambda, specifically the API Gateway Proxy Event which will be sent to our Lambda Function by API Gateway when we get to that portion of the series. Modify your code to match the following. Much of the code has been commented to describe what each section is doing. The vast majority of Lambda functions you find will generally start with this as a base.

package main

import (
    "log"

    // Importing our AWS Libraries
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func main() {
    // <code>lambda.Start(handler) is how Lambda handles passing various events into your code
    lambda.Start(handler)
}

// handler will change the inputs & outputs depending on the type of event you trying to handle
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    // Logging a simple message to the console, which will display in the AWS Console
    log.Println("hello world!")

    // Creating our response with a status code of 200
    response := events.APIGatewayProxyResponse{
        StatusCode: 200,
    }
    
    // Finally, return the response with a nil error object to satisfy the outputs
    return response, nil
}

Deploying the Lambda Function

To test, we’re actually going to package up our function and deploy it straight to AWS. From the AWS console, search for Lambda in the search bar and select it.

https://cdn.brianmorrison.me/media/2022/0df2ca50-dbe1-4508-9fe4-41b5eba1dafc

Once there, click on Create Function. Give your function a name and make sure to change the runtime to Go 1.x.

https://cdn.brianmorrison.me/media/2022/9753138b-200e-4c89-a9d8-64869ebc092e
https://cdn.brianmorrison.me/media/2022/6cac70fc-da1a-4345-bda6-604d18187a8d

Now since AWS runs these on Linux, we need to specify the system we are building for with Go instead of just accepting the default. Run the following in your terminal to build the function.

# Run this on Windows
$Env:GOOS = "linux"
go build main.go

# Run this on Mac
GOOS=linux go build main.go

You should get a single file added to your file system called main. This is the compiled version of our Lambda function. You’ll need to zip up the file before you can upload it. On Windows, you can right click the file, open the Send To menu, and select Compressed folder

https://cdn.brianmorrison.me/media/2022/947c6ffc-48c1-4d51-be87-330a49240ede
https://cdn.brianmorrison.me/media/2022/a5b4ba57-2dcb-4f69-8efa-560a38f93e60

You should have a main.zip that was created. Now back in AWS, click the Upload From button, select .zip file. Select the zip you just created.

https://cdn.brianmorrison.me/media/2022/40d6afba-85e8-420c-b924-b8cdd2581c95

Once its uploaded, you’ll need to change the Handler in the settings from hello to main. Click on Edit in the Runtime Settings section, then change the Handler to main in the next dialog and save it.

https://cdn.brianmorrison.me/media/2022/f009b426-68c5-48b7-a67b-75785b5b1a49
https://cdn.brianmorrison.me/media/2022/75e1bc6b-d0b2-4031-9e68-f82ec19a6c7a

Testing Our Work

Now we can test the function. Head to the Test tab and simply click Test. You should have a green alert that shows up. Expand that to see the details of the execution. You can see we’ve returned that 200 status code, as well as see our hello-world in the logs below!

https://cdn.brianmorrison.me/media/2022/dd25a7a8-4289-4a77-9453-339529b6a796
https://cdn.brianmorrison.me/media/2022/96505bfe-9aca-47ad-8a65-39e99c74a544

In the next entry, I’ll show you how to wire up an API Gateway to Lambda functions and hit our functions using Postman!