Getting started with AWS SAM to validate your Serverless apps before deployment

Photo by Kelvin Ang on Unsplash

As organizations shy away from managing the hardware and infrastructure to run their applications, Serverless has gained spotlight. Many of us might be familiar with AWS Lambda or Azure functions which is based on serverless technology that powers the customers apps and business. However, Serverless doesn’t mean there are no server’s, behind the scenes the third-party infrastructure is taking care of your compute to make your application available.

In this article, we will go over AWS Lambda, A popular serverless compute service provided Amazon web services and learn how to validate the in-development application or business logic before its even deployed in AWS cloud.

Some per-requisites, we need to take care of before getting started with development and running the Lambda locally.

  1. Docker
  2. AWS CLI
  3. Sam CLI

Once you have aws configured on your machine, depending on your programming language choice you might need additional tools. You can also install extensions to your favorite IDE for convenience.

In this article, I will be using DotNet Core 2.1 for developing a SQS(Simple queue service) message triggered Lambda. This article is only intended to show how you can verify the business logic locally without actual deployment of your Lambda.

I have installed dotnet lambda templates from here and then created a new SQS.Lambda project.

For the purpose of this article, I have a handler function that just logs out all caps of the message body received from SQS. If this function is deployed this logged message can be viewed in CloudWatch service in AWS.

Function.cs

Also verify that, aws-lambda-tools-defaults.json is correctly created in boilerplate. Apart from this, we will need to create a event.json file and template.yaml file which is required for SAM local run.

template.yaml

Event.json

Once, you are ready to validate your business logic for your serverless app. You need to run sam cli command which basically runs your code in a container. Here we have used a queue service, but we can also simulate other services from aws.

This should run your app and return the output.

You can also debug your Lambda with breakpoints on your code, with some additional tools for e.g. Debugging of DotNet Lambda requires this Lambda test tool.

So, it’s pretty simple to validate your Serverless app logic dependent on other services(SQS) etc. without causing unnecessary charge from your cloud provider to deploy and then perform the trial-error of the app.

Software developer