AWS Lambda Function with Go

Ömer Burak Demirpolat
Delivery Hero Tech Hub
4 min readNov 24, 2021

Intro

This article will contain creating a super simple Lambda Function with Go. This Lambda Function will be triggered from AWS API Gateway and AWS SNS.

Prerequisites

  • Go 1.x
  • AWS Account and IAM User

How do I create AWS Account?

How do I create IAM User?

First of all, what is Lambda?

Lambda is AWS compute service and provides us running code without thinking servers, scaling, and others. Lambda functions run only when needed and you pay only for compute times.

Okay, what means “Lambda functions run only when needed”?

Lambda functions run when;

  • Incoming HTTP request
  • Incoming message from SNS or SQS
  • And some other AWS services

You can find the full list of triggers from here.

What we will do in this article?

  • Create a Lambda Function triggered from API Gateway.
  • Create a Lambda Function triggered from SNS.
  • Build and upload to AWS.

Let’s create a simple Go application suitable for Lambda. We enable Go modules for AWS Lambda package and get AWS Lambda package.

mkdir $GOPATH/lambdatesttouch main.gogo mod init

Get AWS Lambda package.

go get github.com/aws/aws-lambda-go

Let’s create Go a program that will trigger from API Gateway.

Let’s examine the code above.

lambda.Start function takes a handler and passes incoming data to a handler function.

You can find handler types and details here.

We need a handler to take events.APIGatewayProxyRequest and returns (*events.APIGatewayProxyResponse, error). When the HTTP request comes, our code will run and events.APIGatewayProxyRequest will pass to the handler function.

Very simple, isn’t it?

Now we should build our code in a Linux environment.

GOOS=linux go build main.gozip main.zip main.go

Now let’s login to AWS Console and click Lambda Functions page on the left menu.
Create a function with Go 1.x Runtime and x86_64 arch.

Upload main.zip

Now we will change a little tricky setting in Runtime Settings section.

We will upload an executable file named main. Lambda Functions tries to run hello executable file as a default but we uploaded an executable file named main. So we should change Handler name hello to main.

Before the test our Lambda Function we should add a trigger.

Click Add Trigger button and select API Gateway.

Create an API and choose REST API. I will select Open for Security mechanism for this endpoint, now every client can reach this endpoint. Click to Add button and wait to see API Endpoint.

Okay now we are ready to test our Lambda Function.

curl --location --request POST '$YOUR_API_ENDPOINT' \
--header 'Content-Type: application/json' \
--data-raw '{
"a":"b"
}'

Response should be:

{
"request_payload": {
"a": "b"
}
}

Let’s change our code and create a Go program that will trigger from SNS message.

Above the code will trigger when a new message income to selected SNS topic. We will create an SNS topic and select this created topic for Lambda Function.

Let’s create an SNS topic named GoLambdaTest, you can choose FIFO or Standart type, I will choose Standart type.

Return to created Lambda Function and click to Add Trigger. We should select SNS for a new trigger.

Select an SNS topic for this Lambda Function. When new message incomes to GoLambdaTest topic, our Lambda Function will trigger.

Let’s build the code and zip again.

GOOS=linux go build main.gozip main.zip main.go

Upload the zip file again.

How to try this process? We should publish a new message to GoLambdaTest topic.

Let’s go to an SNS page and click on our topic GoLambdaTest.

Now click to Publish message top right on the page and insert message.

{
"message":"example message"
}

Return Lambda Function page and click Monitor and then Logs.

Now you can see logs on this page. Our Go program successfully handled the incoming message.

Conclusion

We created Lambda Function with Go and we triggered this function with two other triggers.

When working with API Gateway, remember to select the security layer as “open” and set it in a production environment.

Resources:

--

--