Testing Go Serverless Functions Locally

Posted on March 12, 2019

GO And Serverless

I’ve been using the Serverless Framework for developing many lambda’s over the last month. And there is no way to currently run Go lambda’s on your local.

You can run Nodejs and Python lambda’s fairly easily with a few plugins. Or, you could use AWS’s SAM tool for local Go development. I have also developed a Continous Delivery pipeline, custom DNS/domain setup, and multiple languages (nodejs/go) with the Serverless framework and really don’t want to rewrite all of this.

Also, I rather like the Serverless framework and it does give some semblance of not being completely locked into a vendor.

Testing

Solution

So, I came up with a relatively simple solution for this issue. This will work for at least until there is a Go plugin for local development.

It looks like this:

- serverless.yml
- App
  - FOO
    - FooFunctionOne
    - test_FooFunctionOne
    - http_invoke_FooFunctionOne
    - FooFunctionTwo
    - test_FooFunctionTwo
  - BAR
    - BarFunctionOne
- Routes
  - SQS
  - GET_ENDPOINT
  - POST_ENDPOINT

Basically, it’s really easy to catch a get or post request. And similarly easy to catch an SQS/SNS message. This is really ~10 lines of code where you are largely just Unmarshalling JSON.

You then call your logical functions from the App folder.

You can now both invoke these functions locally through a Postman Request, with hard coded values, or setup tests.

It is a very good seperation of concerns. And, it also sets up your app to be easily portable.

The FooFunctionOne simply accpets parameters and returns values. You then can invoke it from a test file in the same folder. You can also write a http invocation method as well.

This keeps your logic is relatively stateless and doesn’t care where the request is coming from.