Published on

Lambda

Authors
  • avatar
    Name
    Galuh Pradipta
    Twitter

AWS Lambda is a cloud service that allows you to run your code without having to provision or manage servers. AWS Lambda supports various programming languages, including Go. In this tutorial, we will walk you through the steps to test your Go AWS Lambda function locally and deploy it to AWS.

Prerequisites

Before we start, ensure that you have the following:

  • Go installed on your machine
  • AWS CLI installed on your machine
  • AWS IAM user with access keys

If you do not have the above prerequisites installed on your machine, you can follow the documentation provided by AWS to install them.

Setting Up

First, we need to create a new Go module. Open your terminal and run the following command:

$ go mod init example.com/myfunction

This command initializes a new Go module that we will use to test our AWS Lambda function locally.

Next, create a new Go file with the following contents:

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(ctx context.Context) (string, error) {
    return "Hello, World!", nil
}

func main() {
    lambda.Start(Handler)
}

This file defines a Lambda function that returns "Hello, World!".

Building the Binary

Next, we need to build the binary for our Lambda function. Run the following command in your terminal:

$ GOOS=linux GOARCH=amd64 go build -o main

This command builds the binary for the Lambda function in the correct format.

Creating the Lambda Function

Now, we need to create the Lambda function on AWS. Run the following command in your terminal:

$ aws lambda create-function --function-name myfunction --runtime go1.x --handler main --role arn:aws:iam::123456789012:role/lambda-execution-role --zip-file fileb://./main

Replace 123456789012 with your AWS account ID and lambda-execution-role with the name of your IAM role that has execution permissions for Lambda functions.

Invoking the Lambda Function

Finally, we can test the Lambda function locally. Run the following command in your terminal:

$ sam local invoke myfunction

This command invokes the Lambda function using the AWS SAM CLI and returns the output in your terminal.

Congratulations! You have successfully tested your Go AWS Lambda function locally.

Deploying to AWS

To deploy your Lambda function to AWS, you can use the AWS CLI. Run the following command in your terminal:

$ aws lambda update-function-code --function-name myfunction --zip-file fileb://./main

This command updates the code for the Lambda function on AWS with the binary that we built earlier.

If you want to automate the deployment process, you can use AWS CodePipeline and AWS CodeBuild. These services allow you to automate the building and deployment of your Lambda functions.

Conclusion

In this tutorial, we walked you through the steps to test your Go AWS Lambda function locally and deploy it to AWS. Testing your Lambda functions locally can save you time and money in the development process. It allows you to test your code quickly and efficiently, without the need to deploy your code to AWS. Automating the deployment process can help you streamline the development process and ensure that your code is always up-to-date on AWS.