Build serverless functions with Go

AWS Lambda supports compiled Go binaries to run as serverless functions. You can precompile them yourself, or Netlify can compile them for you as part of our continuous deployment.

Format

The official AWS repository has very good examples to learn how to create serverless functions with Go. This section walks you through the format of a serverless function that Netlify can deploy and serve with a dedicated endpoint.

Each Go file to be deployed as a serverless Lambda function must include a handler function to receive events from AWS API Gateway:

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
  # Your server-side functionality
}

The ctx parameter includes information about the context in which the function was called, like certain Identity user information, for example.

The response must be compatible with the AWS API Gateway response:

return &events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       "Hello, World",
	}, nil

Here’s a complete example function hello.go:

package main

import (
	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func handler(request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
	return &events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       "Hello, World",
	}, nil
}

func main() {
	// Make the handler available for Remote Procedure Call by AWS Lambda
	lambda.Start(handler)
}

This function would be called from your site at /.netlify/functions/hello, and on success, would return the 200 status code and the string, “Hello, World”.

Access the ClientContext

Netlify can add extra request information to a serverless function call using AWS Lambda's ClientContext. To access this information, you need to use a handler definition that includes a Go Context struct. This object can be transformed into a LambdaContext to access the ClientContext:

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
	lc, ok := lambdacontext.FromContext(ctx)
	if !ok {
		return &events.APIGatewayProxyResponse{
			StatusCode: 503,
			Body:       "Something went wrong :(",
		}, nil
	}

	cc := lc.ClientContext

	return &events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       "Hello, " + cc.Client.AppTitle,
	}, nil
}

Build settings and dependency management

Netlify can build your source Go functions into AWS Lambda compatible binaries. The Go version used in the deployment pipeline is determined by your site's selected build image.

Before building your Go source, Netlify needs to know the expected Go import path for your project. Use the GO_IMPORT_PATH environment variable to set the right import path. You can do this in the Netlify UI at Settings > Build & deploy > Environment > Environment variables, or in your netlify.toml file. The path value should point to your source repository on your Git provider, for example github.com/netlify/aws-lambda-go-example.

Once the Go import path is set, go get will work as expected, and you can also use any dependency management tool to download dependencies for your project.

You can see a full example in the netlify/aws-lambda-go-example repository.