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.
Join the DZone community and get the full member experience.Join For Free
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 takes all the good parts of SAM and brings them to your local machine.
- It lets you develop and test your AWS Lambda functions locally with SAM Local and Docker.
- It lets you simulate function invocations from known event sources like Amazon Simple Storage Service (S3), Amazon DynamoDB, Amazon Kinesis, Amazon Simple Notification Service (SNS), and more.
- It lets you start a local Amazon API Gateway from a SAM template, and quickly iterate on your functions with hot-reloading.
- It lets you quickly validate your SAM template and even integrate that validation with linters or IDEs.
- It provides interactive debugging support for your Lambda functions.
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.
- AWS Command Line Interface (AWS CLI)
- (Pip only) Python 2.7 or Python 3.6
- Java 8
Step 2: Verify that the installation succeeded and version with the command below.
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:
Java 8 and Python 2.7 and 3.6 and Docker should be installed in local.
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.
Opinions expressed by DZone contributors are their own.