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

Run AWS Lambda Functions Locally on a Windows Machine

DZone's Guide to

Run AWS Lambda Functions Locally on a Windows Machine

Take a look at how you can run your AWS Lambda functions closer to home with the benefit of SAM Local.

· Cloud Zone ·
Free Resource

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

 


I was playing with AWS Lambda recently and found it pretty exciting. It's also cheap as serverless applications don’t require provisioning, maintaining, and administering servers, and AWS, in particular, is simple, easy-to-learn, and powerful. AWS Lambda supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby languages. The biggest problem with using Lambda is that it does not allow inline editing for Java. In Java, you need to write the code in an editor and build a .jar or .zip deploy on the console.

As deployment is a complex, painful, and time-consuming process, 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 the AWS lambda console.

After spending a few hours with Lambda, I found SAM Local in AWS docs.

SAM Local


SAM Local takes all the good parts of SAM and brings them to your local machine.

AWS SAM Local is a “CLI tool for local development and testing of Serverless applications.” It uses Docker to simulate a Lambda-like experience. The docs explain well how to get started, and the GitHub repo has lots of samples as well.

This tutorial shows you how to set up SAM Local on local Windows machine.

Prerequisites:

  • Docker
  • AWS Command Line Interface (AWS CLI)
  • (Pip only) Python 2.7 or Python 3.6
  • Java 8



Step 1: Download SAM local Windows Install SAM CLI using an MSI in either the 64 bit or 32 bit versions.

Step 2: Verify that the installation succeeded and version with the command below.

sam --version



Step 3: Write your lambda function or clone it from Github to run locally, making sure to add Template.yaml on the root level.

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


Step 4: Run Maven with the commands below.

mvn clean
mvn install


Step 5: The  sam local start-api command 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.

sam local start-api



Run AWS Lambda Functions With AWS Toolkit Eclipse:

Prerequisites:

  • Java 8 and Python 2.7 and 3.6 and Docker should be installed in local.

You can also use Eclipse or STS 4 and add the Eclipse AWS toolkit.


Step 1: In the configured path where we have sam.exe, find the installation folder


Step 2: Write your lambda function or clone from Github and run locally.


First published on Linkedin.

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.

Topics:
serverless ,cloud ,java ,sam local ,tutorial ,aws lambda

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}