Deploy Go Lambda functions with .zip file archives
Your Amazon Lambda function's code consists of scripts or compiled programs and their dependencies. You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and .zip file archives.
This page describes how to create a .zip file as your deployment package for the Go runtime, and then use the .zip file to deploy your function code to Amazon Lambda using the Amazon Command Line Interface (Amazon CLI).
Sections
Prerequisites
The Amazon CLI is an open-source tool that enables you to interact with Amazon services using commands in your command line shell. To complete the steps in this section, you must have the following:
Tools and libraries
Lambda provides the following tools and libraries for the Go runtime:
Tools and libraries for Go
-
Amazon SDK for Go
: the official Amazon SDK for the Go programming language. -
github.com/aws/aws-lambda-go/lambda
: The implementation of the Lambda programming model for Go. This package is used by Amazon Lambda to invoke your handler. -
github.com/aws/aws-lambda-go/lambdacontext
: Helpers for accessing context information from the context object. -
github.com/aws/aws-lambda-go/events
: This library provides type definitions for common event source integrations. -
github.com/aws/aws-lambda-go/cmd/build-lambda-zip
: This tool can be used to create a .zip file archive on Windows.
For more information, see aws-lambda-go
Sample applications
Lambda provides the following sample applications for the Go runtime:
Sample Lambda applications in Go
-
blank-go
– A Go function that shows the use of Lambda's Go libraries, logging, environment variables, and the Amazon SDK.
Creating a .zip file on macOS and Linux
The following steps demonstrate how to download the lambdago get
, and compile your executable with go build
-
Download the lambda library from GitHub.
go get github.com/aws/aws-lambda-go/lambda
-
Compile your executable.
GOOS=linux GOARCH=amd64 go build -o main main.go
Setting
GOOS
tolinux
ensures that the compiled executable is compatible with the Go runtime, even if you compile it in a non-Linux environment. -
(Optional) If your
main
package consists of multiple files, use the following go buildcommand to compile the package: GOOS=linux GOARCH=amd64 go build main
-
(Optional) You may need to compile packages with
CGO_ENABLED=0
set on Linux:GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go
This command creates a stable binary package for standard C library (
libc
) versions, which may be different on Lambda and other devices. -
Lambda uses POSIX file permissions, so you may need to set permissions for the deployment package folder
before you create the .zip file archive. -
Create a deployment package by packaging the executable in a .zip file.
zip main.zip main
Creating a .zip file on Windows
The following steps demonstrate how to download the lambdago get
, download the build-lambda-zipgo install
, and compile your executable with go build
Note
If you have not already done so, you must install gitgit
executable to your Windows %PATH%
environment variable.
-
Download the lambda library from GitHub.
go get github.com/aws/aws-lambda-go/lambda
-
Download the build-lambda-zip tool from GitHub.
go.exe install github.com/aws/aws-lambda-go/cmd/build-lambda-zip@latest
-
Use the tool from your
GOPATH
to create a .zip file. If you have a default installation of Go, the tool is typically in%USERPROFILE%\Go\bin
. Otherwise, navigate to where you installed the Go runtime and do one of the following:
Creating a Lambda function using a .zip archive
In addition to the main.zip
deployment package you have created, to create a Lambda function you also need
an execution role. The execution role grants the function permission to use Amazon services such as Amazon CloudWatch Logs for log streaming
and Amazon X-Ray for request tracing. You can create an execution role for your function in the IAM console or using the
Amazon Command Line Interface (Amazon CLI).
The following steps demonstrate how to create the execution role using the Amazon CLI and then create a Lambda function using your .zip deployment package.
-
Create a trust policy giving the Lambda service permission to assume the execution role. Copy the following JSON and create a file named
trust-policy.json
in the current directory.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
-
Create the execution role.
aws iam create-role --role-name lambda-ex --assume-role-policy-document file://trust-policy.json
You should see the following output. Make a note of your role's ARN. You will need this to create your function.
{ "Role": { "Path": "/", "RoleName": "lambda-ex", "RoleId": "AROAQFOXMPL6TZ6ITKWND", "Arn": "arn:aws-cn:iam::123456789012:role/lambda-ex", "CreateDate": "2020-01-17T23:19:12Z", "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } } }
-
Add permissions to the role using the
attach-role-policy
command. In the example below, you add theAWSLambdaBasicExecutionRole
managed policy, which allows your Lambda function to upload logs to CloudWatch. If your Lambda function interacts with other Amazon services, such as Amazon S3 or DynamoDB, you will need to add policies allowing your Lambda function to access these services. See Amazon managed policies for Lambda features for more information.aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
-
Create the function.
aws lambda create-function --function-name
my-function
--runtime go1.x --rolearn:aws:iam::123456789012:role/lambda-ex
--handler main --zip-file fileb://main.zip
Note
When you create a Go Lambda function using the Amazon CLI, the value of the handler setting you define is the executable file name. For more information, see Amazon Lambda function handler in Go.
Build Go with the provided.al2 runtime
Go is implemented differently than other native runtimes. Lambda treats Go as a custom runtime, so you can create a Go function on the provided.al2 runtime. You can use the Amazon SAM build command to build the .zip file package.
Using Amazon SAM to build Go for AL2 function
Update the Amazon SAM template to use the provided.al2 runtime. Also set the BuildMethod to makefile.
Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello-world/ Handler: my.bootstrap.file Runtime: provided.al2 Architectures: [arm64] Metadata: BuildMethod: makefile
Remove the
Architectures
property to build the package for the x86_64 instruction set architecture.Add file makefile to the project folder, with the following contents:
GOOS=linux go build -o bootstrap cp ./bootstrap $(ARTIFACTS_DIR)/.
For an example application, download
Go on AL2