97

Creating a Deployment Package (Go) - AWS Lambda

 6 years ago
source link: https://docs.aws.amazon.com/lambda/latest/dg/lambda-go-how-to-create-deployment-package.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Deploy Go Lambda functions with .zip file archives

To create a container image to deploy your function code, see Using container images with Lambda.

A .zip file archive is a deployment package for your Lambda function that consists of your code (a Go executable) and any dependencies.

After you create the deployment package, you can upload it directly to an Amazon Simple Storage Service (Amazon S3) bucket in the AWS Region where you want to create the Lambda function. Or, you can upload the .zip file archive first and then specify the S3 bucket name and object key name when you create the function using the Lambda console or the AWS Command Line Interface (AWS CLI).

Download the Lambda library for Go with go get, and compile your executable.

Use aws-lambda-go version 1.18.0 or later.

~/my-function$ go get github.com/aws/aws-lambda-go/lambda
~/my-function$ GOOS=linux go build main.go

Setting GOOS to linux ensures that the compiled executable is compatible with the Go runtime, even if you compile it in a non-Linux environment.

Create a deployment package by packaging the executable in a .zip file, and use the AWS CLI to create a function. The handler parameter must match the name of the executable containing your handler.

~/my-function$ zip function.zip main
~/my-function$ aws lambda create-function --function-name my-function --runtime go1.x \
  --zip-file fileb://function.zip --handler main \
  --role arn:aws:iam::123456789012:role/execution_role

Creating a deployment package on Windows

To create a .zip file archive that works on Lambda using Windows, we recommend installing the build-lambda-zip tool.

If you have not already done so, you must install git and then add the git executable to your Windows %PATH% environment variable.

To download the build-lambda-zip tool, run the following command:

go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip

Use the tool from your GOPATH. 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:

In cmd.exe, run the following:

set GOOS=linux
go build -o main main.go
%USERPROFILE%\Go\bin\build-lambda-zip.exe -output main.zip main

In PowerShell, run the following:

$env:GOOS = "linux"
$env:CGO_ENABLED = "0"
$env:GOARCH = "amd64"
go build -o main main.go
~\Go\Bin\build-lambda-zip.exe -output main.zip main

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK