An introduction to serverless Go applications using AWS

Serverless has always been a bit of a buzzword and seems to be the trend at the moment — probably justifiably. In this post we’ll discuss building serverless Go functions, testing, configuration and deployment to AWS.


What is serverless?

Serverless is essentially an on-demand execution model for computing workloads, which is supported by many cloud providers — such as AWS, Azure and Google Cloud.

Serverless doesn’t mean that there are no servers involved.

Serverless functions are exactly what they sound like — functions which run on-demand on a cloud provider’s platform.

Typically an interface is provided for developers to upload their code and the cloud provider takes care of the rest. The cloud provider manages hosting and execution of the code, resource allocation, auto-scaling, logging, metrics, security (somewhat) and more. You’re only billed for the time your application is actually running.

Often serverless functions are executed inside stateless Docker containers and invoked using triggers provided by cloud providers, like a database event, new queue entry or HTTP proxy event.

Serverless functions are usually used for workloads of unknown or unpredictable demand, since they are provisioned and executed only when they’re needed. Some example use-cases for this could be:

  • A Slack integration which responds to slash commands.
  • A mobile app which makes a backup of photographs you take.
  • An API for AJAX calls powering a SPA CloudFront distribution.

AWS Lambda

Lambda is Amazon’s FaaS (function as a service) platform and is part of their serverless application model (SAM).

On January 15th 2018 AWS announced support for Go Lambda functions. AWS provide a library for easily implementing Lambda functions in Go which we’ll be using for the example in this post.

There are many interface signatures supported by Lambda handlers and AWS provide types for many existing Lambda triggers (such as S3, SQS, API Gateway, DynamoDB, CloudWatch, Lex and many more).

An example

I’ve created an example function we’ll use for the purposes of this post. You can grab the code from GitHub at https://github.com/kaperys/blog/tree/master/serverless-go-intro.

The high level requirements for this function are simple. I want to build an API which I can provide a search string and then have one or more applicable emojis returned. I also don’t want to manage the emoji data (this introduces an obvious dependency, but that’s fine for this example).

Here’s the code:

The important part is the handler function defined on line 35.

func handler(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {}

Here we implemented one of the supported Lambda handler signatures. In this example we’re using the predefined APIGatewayProxy types since we’ll be using API Gateway to invoke our function.

The rest of the code is “business logic” and really could be anything.

Local development

Now we’ve written our Lambda function, how do we run it locally? There are a few options here.

Unit tests

Firstly, and possibly most obviously, we could run the unit tests for the function (since your application should have unit tests anyway!). Since Lambda functions are literally that, functions, it’s very easy to invoke your function in a unit test. You can check out the unit tests for the example application on GitHub.

Although unit tests are a great way to test that your business logic and handlers are doing what you expect, they can’t cover integrations with the AWS services we’ll be using.

AWS SAM

AWS SAM CLI is a tool provided by AWS for development and deployment (amongst other things) of serverless applications. It allows us to run Lambda functions locally, validate AWS SAM templates, retrieve logs, generate event payloads, deploy our applications and more.

AWS SAM CLI is available for Windows, Mac and Linux and requires Docker, the AWS CLI and Python Pip to be installed. There are installation instructions for SAM CLI available on AWS’ website.

There are two SAM CLI commands that we could use to test our function — the start-api command and the invoke command (there are more commands available if your Lambda function uses an alternative trigger, like an S3 or SQS event).

The start-api command, according to AWS’ documentation:

Allows you to run your serverless application locally for quick development and testing. When you run this command in a directory that contains your serverless functions and your AWS SAM template, it creates a local HTTP server that hosts all of your functions.
When it’s accessed (through a browser, CLI, and so on), it starts a Docker container locally to invoke the function. It reads the CodeUri property of the AWS::Serverless::Function resource to find the path in your file system that contains the Lambda function code.
- https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html

Essentially start-api allows us to spawn a local API Gateway with which we can invoke our Lambda function.

The invoke command, according to AWS’ documentation:

Invokes a local Lambda function once and quits after invocation completes.
This is useful for developing serverless functions that handle asynchronous events (such as Amazon S3 or Amazon Kinesis events). It can also be useful if you want to compose a script of test cases.
- https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-invoke.html

invoke allows us to manually invoke the function with a given payload.

Both the start-api and invoke commands require a template.yml file. The template file describes the architecture required by our serverless application. For our example application we’ll need to describe a serverless function resource.

Here’s the template.yml file we’ll be using for our emoji function:

The AWS::Serverless::Function resource describes the attributes of our function, such as the runtime, handler, memory requirements, timeout, etc. Within the function resource definition we can also describe the API we want to use to invoke the Lambda function.

In our example, we’re telling SAM that we want to create a serverless function called “SearchEmojis”. The template states that the “SearchEmojis” function should execute the “handler” binary using the go1.x runtime, and that we want to provide the SOURCE_URL environment variable to the function. It also states that we want to provision an API which will invoke the Lambda function when a GET request is made to the root handler.

We can now execute our Lambda function locally, using either the start-api or invoke commands. Since we’re going to be using API Gateway, I’ll use start-api (I’ve also provided an invoke example in the Makefile).

Deployment

AWS SAM CLI also provides functionality to package and deploy your serverless applications. We’ll use the sam package and sam deploy commands to do this. Both commands are aliases for their aws cloudformation [command] equivalents.

The package command uses the template.yml file to package the application into a release and uploads the resources to an AWS S3 bucket. Once the upload is complete a copy of the template.yml file is created and the local artifacts are replaced with the remote ones.

To package our application we need to pass a couple of flags to the sam package command. Firstly SAM needs to know the template filename. Secondly we’ll need to provide the name of the AWS S3 bucket where we want to store our release. Lastly the name of the packaged output file.

sam package — template-file template.yml — s3-bucket serverless-go-intro — output-template-file package.yml

Once our application has been packaged we can use the deploy command to roll out the application. By default, the deploy command creates and executes a CloudFormation change set.

To deploy our application we need to pass a couple of flags to the sam deploy command. Firstly the CloudFormation stack name. This name is used to uniquely identify the stack. If a stack with this name doesn’t already exist CloudFormation will provision a new one, otherwise it will update the existing one. Secondly we’ll need to pass the packaged template filename. And lastly we need to tell CloudFormation that our packaged template is allowed to create IAM roles (we’ll need to create an IAM role to allow API Gateway to invoke Lambda).

sam deploy — stack-name ServerlessGoIntroStack — template-file package.yml — capabilities CAPABILITY_IAM

Great! Our serverless application has been deployed. Since we added an endpoint output to our template.yml file we’re able to use the AWS CloudFormation CLI to discover the URL of the API Gateway endpoint.

aws cloudformation describe-stacks — stack-name ServerlessGoIntroStack

We can now invoke our Lambda function using the API Gateway endpoint we can see in the Outputs array. We can use curl to try it out.

Conclusion

In this post we’ve seen how we can use the AWS SAM CLI tool to easily build, test and deploy serverless Go applications with AWS. With a simple template.yml file and some tooling we can make these often challenging tasks as simple as executing a few commands.