Capital One Tech
Published in

Capital One Tech

Using a Custom Runtime for Go-Based Lambda Functions

Despite Go having native support in AWS Lambda, switching to a custom runtime has its advantages

man running across patio

Lambda runtimes: native vs custom

The native runtime for Go

package mainimport (
"fmt"
"context"
"github.com/aws/aws-lambda-go/lambda"
)
type Event struct {
Name string `json:"name"`
}
func main() {
h := func HandleRequest(ctx context.Context, e Event) (string, error) {
return fmt.Sprintf("Invoked by: %s", e.Name ), nil
}
lambda.Start(h)
}

Three advantages to using a custom runtime for Go

1. Support for Lambda Extensions

2. Provides an Amazon Linux 2 execution environment

3. Unifies the runtime and handler code

Migrating from the Go native runtime to a custom runtime

Change #1 — Build bootstrap binary

GOOS=linux go build -o ./bootstrap ./...

Change #2 — Remove RPC for faster cold-start

GOOS=linux go build -o ./bootstrap ./... -tags lambda.norpc

Change #3 — Update Lambda configuration

Resources:
Function:
Type: AWS::Serverless::Function
Properties:
Handler: bootstrap
Runtime: provided.al2
... # Other required properties

Putting it all together

--

--

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Travis Bale

Software Engineer at Capital One and AWS enthusiast who loves deep-diving into all things DevOps, Serverless, and Security