How to Test AWS Lambda locally

Running LocalStack using Docker

# docker-compose.yml
version: '3.8'
services:
aws:
image: localstack/localstack:0.12.5
environment:
# Out put error messaging for local development
DEBUG: 1
# (optional) Docker network that lambdas are reachable by.
# This makes it easier for Lambads to be invoked by other
# external Docker containers.
LAMBDA_DOCKER_NETWORK: my-local-aws-network
# Use our local files via Docker volumes to define our Lambdas
LAMBDA_REMOTE_DOCKER: 0
# Spin up the Lambda module
SERVICES: lambda
ports:
- 4566:4566
volumes:
# Let LocalStack create new docker containers by giving
# LocalStack access to your computer's Docker daemon
- /var/run/docker.sock:/var/run/docker.sock
networks:
default:
name: my-local-aws-network

Create and Invoke Lambda locally

// index.js
const perform = async (event, context) => {
console.log("EVENT: \n" + JSON.stringify(event, null, 2))
return context.logStreamName
}
exports.handler = perform
# ./nodejs-example.bash# Point aws-cli to LocalStack instead of AWS
ENDPOINT=http://localhost:4566/
# The name of our lambda in LocalStack
FUNCTION_NAME=nodejs-example
# What code do we want LocalStack to execute on. In this case the
# handler() function defined in index.js
HANDLER=index.handler
# We want to run our index.js file using the node v12 runtime
RUNTIME=nodejs12.x
# Our local working directory where index.js is located
S3_KEY=$(pwd)
# Delete Lambda (if it already exists) to start with clean slate
aws --endpoint-url ${ENDPOINT} lambda delete-function \
--function-name ${FUNCTION_NAME}
# Create Lambda
aws --endpoint-url ${ENDPOINT} lambda create-function \
--code S3Bucket="__local__",S3Key="${S3_KEY}" \
--function-name ${FUNCTION_NAME} \
--handler ${HANDLER} \
--role value-does-not-matter \
--runtime ${RUNTIME}
# ./invoke.bashENDPOINT=http://localstack:4566
FUNCTION_NAME=nodejs-example
PAYLOAD='{ "myJsonData": "true" }'
aws lambda invoke \
--cli-binary-format raw-in-base64-out \
--function-name ${FUNCTION_NAME} \
--invocation-type RequestResponse \
--no-sign-request \
--payload ${PAYLOAD}\
--endpoint ${ENDPOINT} \
output.json

--

--

--

Software engineer with a background in human psychology and data analytics who affords both customer and engineer delight through Agile software architectures.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Arduino RC Boat that can be Controlled Wirelessly

Arduino Controlled RC Air Boat using 433Mhz RF Modules

Excel Consolidation using vba

Excel Consolidation using vba | multiple work book copy paste into single workbook

Reference Commission

Let's not call it Artificial Intelligence anymore.

SwiftUI: Data-Driven UI

Governing API Resource Modelling

Top 3 Ways To Center In CSS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erik A. Ekberg

Erik A. Ekberg

Software engineer with a background in human psychology and data analytics who affords both customer and engineer delight through Agile software architectures.

More from Medium

Can You Mix Multiple Programming Languages in the Same AWS SAM Application?

A whisk and mixing bowl, a play off of “mixing languages”

Simplify Your AWS Lambda View

What is Serverless Architecture? And Why Businesses Are Going Serverless

AWS Elastic Load Balancing from a Serverless perspective