How to test golang written lambda function locally

How to test golang written lambda function locally
Page content

For a while I was looking for an easy we to test my lambda functions written in golang locally. We have awesome tools like LocalStack but this is not what I was looking for. I wanted to test my lambda function locally, without any dependencies. Something lighter without Docker . I will show you how to do that.

Installation

You have to build the binary for your lambda function: go build -v -gcflags='all=-N -l' main.go. You have to export one variable export _LAMBDA_SERVER_PORT=3000 and run the binary. That’s it.

Payload

Second step will be to create payload which will be sent to your lambda functions. You can define multiple fields in the payload, but the most important one is body. It will be passed to your lambda function as an input. You can also define headers and queryStringParameters. You can find more information about the payload here .

{
  "Headers": {
    "Content-Type": "application/json"
  },
  "Body": "this body will be pushed to lambda function",
  "HTTPMethod": "POST"
}

If your body is more sophisticated, you can base64 encode it and pass it to the lambda function with 2 more parameters

    { "isBase64Encoded": false } 

Testing

We only need one more ingredient: awslambdarpc

Finally, you can send payload to your lambda function:

~/go/bin/awslambdarpc -a localhost:3000 -e payload.json