AWS SAM Local: Test Serverless Application Locally

Table of contents
Reading Time: 2 minutes

AWS Lambda is a serverless framework where we can just create an application, make an artifact out of it and just upload it. Developers are free from configuring the infrastructure as it is handled by AWS.

Now the concern here is that when developers want to test the application after changing the code they have to, again and again, deploy the jar to AWS lambda console.

It’s really a Time-consuming…. !! task.

But don’t worry, AWS also provide AWS SAM Local were we can run the jar locally as it creates a local environment same as AWS creates on the console.

Now developers can focus on their development and deploy the jar to the actual AWS environment whenever needed.

Now let’s see how it’s done.

First, you need to set up SAM CLI on local.

Install SAM CLI

SAM CLI is a tool that allows faster, iterative development of your Lambda function code.

To use SAM CLI we need to install docker first as SAM CLI provides docker-lambda docker image where it runs the jar. Using docker-lambda, you can invoke your Lambda function locally. You can find how to install Docker here.

Now you can download the latest version of SAM CLI Debian file from here. Then,

sudo dpkg -i sam_0.2.11_linux_amd64.deb

Now let’s verify that the installation succeeded:

sam --version

Now we are done with the installation.

Let’s start with an example.

You can clone the project from here.
First, we need to create a template.yaml file in the project under root directory:



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Lambda Sample Project
Resources:
Products:
Type: AWS::Serverless::Function
Properties:
Handler: com.example.handler.LambdaHandler
CodeUri: ./target/lambda-project-1.0-SNAPSHOT.jar
Runtime: java8
Timeout: 300
Environment:
Variables:
ENVIRONMENT: "test"
Events:
ListProducts:
Type: Api
Properties:
Path: /lambda
Method: post
view raw

template.yaml

hosted with ❤ by GitHub

This file contains all the information that is needed for the application to run. We specify the handler class that will be executed in the lambda as well as the jar name that we have created earlier.

Then, we will create the jar to run the application:

mvn clean package

Now run the application by running the below command:

sam local start-api

You will get an URL from the above command and that URL will use to hit our lambda with the following json body:

{ “message” : “Hello Coders” }

You will get the required response:

{
“status”: “Success”,
“message”: “Got Hello Coders!!”
}

Yeah, we got the response.

Thanks for your patience…!!

Github Repo – AWS Lambda

Reference:

AWS SAM Local


knoldus-advt-sticker

Written by 

I am a Software Consultant at Knoldus Inc. I am a Scala Enthusiast. I am familiar with Object Oriented Programming Paradigms, and has also worked upon .NET based technologies. Aside from being a programmer, I am familiar with NoSQL database technologies such like Cassandra. I also worked on Lagom microservice architecture.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading