Guide to Building AWS Lambda Functions with Go and Docker
golang docker aws lambdaIntroduction
AWS Lambda is a powerful serverless computing platform that allows developers to run their code without worrying about the underlying infrastructure. In this tutorial, we will explore how to build and deploy AWS Lambda functions using Go and Docker. We will start by creating a new Lambda function in the AWS Management Console, then write some Go code to implement our function. We will then use Docker to build a container image of our code and deploy it to AWS. Finally, we will test our Lambda function locally to make sure everything is working correctly.
Step 1: Create a New Lambda Function
First, we need to create a new Lambda function in the AWS Management Console:
- Open the AWS Management Console and navigate to the Lambda service.
- Click the “Create Function” button.
- Select “Author from scratch” and give your function a name.
- Choose “Go 1.x” as the runtime and click the “Create Function” button.
Step 2: Write Your Go Code
Next, we need to write some Go code to implement our Lambda function. Here’s an example function that takes a name as input and returns a greeting message:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type Request struct {
Name string `json:"name"`
}
type Response struct {
Message string `json:"message"`
}
func HandleRequest(ctx context.Context, request Request) (Response, error) {
message := fmt.Sprintf("Hello, %s!", request.Name)
return Response{Message: message}, nil
}
func main() {
lambda.Start(HandleRequest)
}
In this code, we define a Request
struct that represents the input to our function, and a Response
struct that represents the output. We define a HandleRequest
function that takes a Request
as input and returns a Response
and an error. Finally, we call the lambda.Start
function to start our Lambda function.
Step 3: Build a Docker Image
Now, we need to build a Docker image of our Go code:
- Create a new file named Dockerfile in the same directory as your Go code.
- Add the following code to your Dockerfile:
FROM golang:1.20 AS builder
COPY . /go/src/app
WORKDIR /go/src/app
RUN go build -o main .
FROM public.ecr.aws/lambda/provided:al2
COPY --from=builder /go/src/app/main /main
ENTRYPOINT [ "/main" ]
This Dockerfile starts with golang:1.20
base image, copies our Go code into the image, builds the binary, uses public.ecr.aws/lambda/provided:al2
to run the built binary and sets the entry point to run the binary.
- Build the Docker image by running the following command in your terminal:
docker build -t my-lambda-function .
This command will build a Docker image with the name my-lambda-function
.
Step 4: Deploy the Docker Image
Now that we have a Docker image of our code, we can deploy it to AWS:
- Push the Docker image to a container registry. For example, you can use Amazon Elastic Container Registry (ECR).
- In the AWS Management Console, navigate to your Lambda function.
- Click the “Edit” button in the “Function code” section.
- Select “Container image” as the “Code source”.
- Enter the URI of your Docker image in the “Image URI” field.
- Click the “Save” button to save your changes.
Step 5: Test the Lambda Function Locally
Before testing our Lambda function on AWS, it’s a good idea to test it locally to make sure everything is working correctly. Here’s how to do it:
- Install the AWS SAM CLI by following the instructions here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
- In your terminal, navigate to the directory where your Go code and Dockerfile are located.
- Create a new file named
template.yaml
with the following contents:
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: my-lambda-function:latest
Handler: main
Runtime: provided
This YAML file defines an AWS Serverless Application Model (SAM) template that describes our Lambda function. It tells SAM to use the docker image that we built to run the lambda function, set the handler to main, and use the provided runtime.
- Start the SAM local Lambda runtime by running the following command:
$ sam local start-lambda
This command will start the SAM local Lambda runtime on your machine.
In a new terminal window, test your Lambda function by running the following command:
$ echo '{"name": "Alice"}' | sam local invoke MyLambdaFunction --event -
This command will invoke your Lambda function with an input of {"name": "Alice"}
and print the output to the console.
If everything is working correctly, you should see the output {"message": "Hello, Alice!"}
printed to the console.
The entire output from above command looks like this:
Reading invoke payload from stdin (you can also pass it from file with --event)
Invoking Container created from my-lambda-function:latest
Using local image: my-lambda-function:rapid-x86_64.
START RequestId: 86e8cf72-92dc-4e58-a745-b786bd4aa2d8 Version: $LATEST
{"message":"Hello, Alice!"}
END RequestId: 86e8cf72-92dc-4e58-a745-b786bd4aa2d8
REPORT RequestId: 86e8cf72-92dc-4e58-a745-b786bd4aa2d8 Init Duration: 0.34 ms Duration: 10.80 ms Billed Duration: 11 ms Memory Size: 128 MB Max Memory Used: 128 MB
Step 6: Test the Lambda Function on AWS
Now that we have tested our Lambda function locally, we can test it on AWS:
- In the AWS Management Console, navigate to your Lambda function.
- Click the “Test” button in the top right corner.
- Enter a name in the “Event name” field, and then click the “Create” button.
- Click the “Test” button to test your Lambda function with the default input.
If everything is working correctly, you should see the output of your Lambda function in the “Details” section of the test results.
Conclusion
In this tutorial, we have explored how to build and deploy AWS Lambda functions using Go and Docker. We started by creating a new Lambda function in the AWS Management Console, then wrote some Go code to implement our function. We then used Docker to build a container image of our code and deployed it to AWS. Finally, we tested our Lambda function locally and on AWS to make sure everything was working correctly. With this knowledge, you should be able to build and deploy your own Lambda functions using Go and Docker. In case you would like to get notified about more articles like this, please subscribe to my blog.