DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. AWS SAM Local: Test Serverless Application Locally

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.

Akhil Vijayan user avatar by
Akhil Vijayan
·
Jul. 09, 18 · Tutorial
Like (2)
Save
Tweet
Share
16.01K Views

Join the DZone community and get the full member experience.

Join For Free

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.

AWS application Sam (text editor) Testing

Published at DZone with permission of Akhil Vijayan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Core Machine Learning Metrics
  • Enabling DB Migrations Using Kubernetes Init
  • Learning by Doing: An HTTP API With Rust
  • 9 Ways You Can Improve Security Posture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: