Amazon has announced Go support for AWS Lambda, which opens the way to writing Go serverless applications running on AWS.
Go is the latest language Amazon is adding support for in AWS Lambda, along with Node.js, Python, Java, and C#.
To make your Go code ready for AWS deployment, it must reside in a handler function which is then passed to lambda.Start()
. lambda.Start()
does not return after being called and communicates with an internal Lambda endpoint to pass Invoke requests to the handler. The handler function must take between 0 and 2 arguments, which include the request object, and may return between 0 and 2 values, including an error object, e.g.:
// main.go
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
func hello(request Request) (string, error) {
return "Hello!", nil
}
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
AWS requires to compile Go code to be used in AWS Lambda for Linux and then zip it to make it ready for AWS Lambda deployment. This can be done, for example, running:
GOOS=linux go build -o main main.go
zip main.zip main
The name used for the zip file must match the Handler configuration property of the deployed Lambda function.
Once the code is packaged as a zip file, it can be deployed using the AWS CLI:
$ aws lambda create-function \
--region us-west-1 \
--function-name HelloFunction \
--zip-file fileb://./main.zip \
--runtime go1.x \
--tracing-config Mode=Active
--role arn:aws:iam::<account-id>:role/<role> \
--handler main
Additionally, AWS provides AWS X-Ray for Go, a set of libraries that enable the generation of trace data for the X-Ray daemon to analyze and debug your Go functions.
To make it easier for developers to use Go to develop AWS Lambda functions, Amazon has published a GitHub repository containing libraries, samples, and tools. You can get more details about testing, building, and deploying Go code to AWS Lambda here.