Deploy Go Lambda functions with .zip file archives
Your AWS 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 files.
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 AWS Lambda using the AWS Command Line Interface (AWS CLI). To upload your .zip file on the Lambda console, see Deployment packages.
Sections
Prerequisites
The AWS CLI is an open-source tool that enables you to interact with AWS 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
-
AWS SDK for Go
: the official AWS 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 AWS Lambda to invoke your handler. -
github.com/aws/aws-lambda-go/lambdacontext
: Helpers for accessing execution 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 AWS 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 go build 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 go build main
-
(Optional) You may need to compile packages with
CGO_ENABLED=0
set on Linux:GOOS=linux CGO_ENABLED=0 go build main.go
This command creates a stable binary package for standard C library (
libc
) versions, which may be different on Lambda and other devices. -
Create a deployment package by packaging the executable in a .zip file.
zip function.zip main
Creating a .zip file on Windows
The following steps demonstrate how to download the build-lambda-zipgo get
, and compile your executable with go build
If you have not already done so, you must install gitgit
executable to your Windows %PATH%
environment variable.
-
Download the build-lambda-zip tool from GitHub:
go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
-
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 the Lambda function using the AWS CLI
Lambda needs to know the Lambda runtime environment to use for your function's code (in runtime
), a name for your Lambda function (in function-name
), the Lambda handler in your function code (in handler
), and the execution role it can use to invoke your function (in role
).
Use the create-function command to create the Lambda function.
aws lambda create-function --function-name
my-function
--runtime go1.x --zip-file fileb://function.zip
--handlermain
--role arn:aws:iam::your-account-id
:role/execution_role