Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AWS SAM Local: Test Serverless Application Locally

DZone's Guide to

AWS SAM Local: Test Serverless Application Locally

Here we take a look at how you can avoid wasting time when retesting their application after having made changes with AWS SAM Local.

· Cloud Zone ·
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

AWS Lambda is a serverless framework where we can just create an application, make an artifact out of it and 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 provides AWS SAM Local were we can run the jar locally as it creates a local environment, the same as the one 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.

Install SAM CLI

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

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 a 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:

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


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!!


This article was first published on the Knoldus blog.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
cloud ,aws ,lambda function ,application testing ,aws sam local ,container ,container deployment

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}