Just like Python or Javascript, AWS Lambda can run Go code effectively. As Go is compiled into machine code, you should see better Lambda performance than Python or JS in many cases. But we can’t directly copy-paste our Go code into Lambda. In this blog, we will learn how to deploy Go code to AWS lambda.
Table of contents
Open Table of contents
Create a simple Go function
Let us start by creating a simple hello world Go function. We will first create a directory and initialize the Go module.
mkdir lambda-example
cd lambda-example
go mod init binaryguy.tech/lambda-example
touch main.go
Inside main.go
file, put the following code.
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
You should see Hello World
as output when you run this code using the Go run command.
go run main.go
Preparing Go Code for Lambda Deployment
Updating code for Lambda Event
We can’t run this code directly in AWS Lambda. We need to ensure the Go code understands events coming from Lambda and handles them correctly. For this, let us start by adding the AWS Lambda Go module.
go get github.com/aws/aws-lambda-go/lambda
After this step, update the code in main.go
file.
package main
import (
"context"
"github.com/aws/aws-lambda-go/lambda"
)
type MyEvent struct {
Name string `json:"name"`
}
func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
return "Hello " + name.Name, nil
}
func main() {
lambda.Start(HandleRequest)
}
The HandleRequest
function handles events coming to AWS lambda. This function expects the event to be a JSON object with a name key.
The main function starts the Lambda function using lambda.Start
function call.
Building Go code for Lambda
Next, we need to build the code for deployment. For this, you need to run the following command.
go build -o main
This command builds the Go application and saves it with the name main
.
AWS Lambda runs on Linux. We need to make sure that we build Go code for Linux.
If you are on Windows, run the following command to build the Go app.
$env:GOOS="linux"
go build -o main
If you are on Mac, you can use the following command
GOOS=linux go build -o main
Finally, we need to create a zip file to upload to AWS Lambda.
zip lambda.zip main
Uploading Go code to AWS Lambda
For simplicity, we will directly upload a Zip file to the Lambda function from the AWS console. I have already created the Go function.
Make sure that handler is set to main (or name of your go application that you used when building app) in Runtime settings.
Let us test our lambda code with a custom event, as shown below.
Updating Go Code for API Gateway Proxy
We need to update the code to process the API Gateway proxy events with the Go Lambda function.
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (string, error) {
fmt.Println(request.HTTPMethod)
return fmt.Sprintf("Hello %s!", request.Body), nil
}
func main() {
lambda.Start(HandleRequest)
}
The above code has updated the HandleRequest
function to accept APIGatewayProxyRequest events.
As mentioned above, we can again build and deploy this code to Lambda and test using the APIGateway event instead of our custom event.
Hurry 🥳🥳🥳
I hope you have found this helpful. You can get code used in this blog at this repo. See you in the next blog.