Running Go AWS Lambdas locally with SLS framework and SAM

In my previous post, I described an example of manually creating an AWS Lambda function implementing a GraphQL endpoint. Since I’m a big fan of the Serverless Framework, I wanted to write an example of using it to create a project using Go. Also, one of the comments on my previous post by Brian Bates asked about using SAM with Go. He wrote a post about this integration and I wanted to try the same with a SLS project.

SAM (Serverless Application Model)

SAM is a way to define serverless applications created by AWS, similar to CloudFormation. It too follows a very similar format to the file provided by SLS where you declare all your functions and resources they use, like they endpoints for API Gateway.

One of the big features provided by SAM is that you can run your serverless application locally, similar to what offers for Node projects. Being officially supported by AWS, SAM is a great way to have a developer environment if you use AWS offering of Serverless.


In order to follow this example, you will need to install SAM in your computer, and have a working installation of Docker. You also need to have the Serverless framework and the Go SKD installed in your machine.

Also, since the SLS project for go uses for dependency management, you will need to install it too.

If you have NPM, you can install SAM with the following command:

Creating the project

Let’s generate a Go-based project with SLS. Navigate to your GOPATH (the project needs to live inside Go’s path) and run the following command:

The name of my project is . This will generate a project inside GOPATH. Please notice that we used as the template for our project, which means that this project will only work with AWS, just like SAM.

Image for post
Image for post

The project contains two function handlers: and .

Since this is a Go project, you can build it running the make command:

This will generate a folder in the root of your project, equivalent to the folder generated for Node SLS applications.

Technically, you could use the SLS framework to deploy the application. However, we want to run it locally for development. So let’s enable SAM.

Changing our sample function for API Gateway

As generated by SLS, your is only a Lambda function. If we want to serve it through the API Gateway, we need to modify it to have it return the appropriate type of data. Add the following code to :

Configuring SAM

Since we already installed SAM CLI, all we need is define a file at the root of your project. SAM will use this file to find the executables of your application, and define their required resources.

Create in your project’s root with the following content:

The important parts here are the and properties.

Handler must point to the compiled binary of your function handler. You can use the same property defined in . In our case, this is as that’s were our binary was created. Don’t point it to the file.

Runtime must be so SAM can interpret that our functions being written in Go.

Running the functions locally

We are now ready to run locally our functions. Just run the following command:

If this is the first time you execute this command, it will take some time while it downloads the appropriate Docker images that SAM local needs to serve the application in your computer. At the end, you should see in your console an output similar to this:

Now, navigate to and you should see the following message:

If you decided to use AWS for your serverless app, SAM is a great option for having a local development environment.


After playing around with SAM for a while, I could find a small weakness: It doesn’t support creating resources like DynamoDB. Only Lambdas will be emulated. You can still start local servers for databases or other services, but this is a big disadvantage against other services like which integrate out of the box with services like .

A man with no server

The history of my journey creating Serverless applications

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store