run-local-aws-lambda
v0.0.3
Published
Run local aws lambda
Downloads
7
Readme
run-local-aws-lambda
! ORIGINAL version es created by sbarski
An npm module to help you run and test Lambda functions locally
Allows you to test asynchronous lambdas functions, does not use callback, it invokes the function only with the event and waits for promise. See AWS docs for more information.
exports.handler = async function (event) {
// does things
return {
statusCode: code,
body: JSON.stringify(response),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
}
};
- This module allows you to run and test AWS Lambda functions on your computer.
- You can pass in any event data JSON object to simulate a Lambda event.
Getting Started
This module is designed to be run by npm to facilitate testing of Lambda functions. To install it run:
npm install run-local-aws-lambda --save-dev
Your Lambda function should have a package.json which you can modify to add a test script like so:
"scripts": {
"local": "run-local-lambda --file index.js --event tests/event.json --timeout 3"
}
Finally, you can invoke your test by simply running:
npm run local
Overview
Parameters
This module accepts the following parameters which are all optional.
- --file [lambda file name] - Lambda function file name. Default: index.js
- --event [event file name] - Event data file name. Default: event.json
- --handler [handler name] - Lambda function handler. Default: handler
- --timeout [timeout seconds] - The timeout in seconds. Default: 30
Event
The event data file can be provided using the --event parameter. An event is just a JSON object such as:
{
"resource": "/",
"path": "/",
"httpMethod": "GET",
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"cookie": "s_fid=7AAB6XMPLAFD9BBF-0643XMPL09956DE2; regStatus=pre-register",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"upgrade-insecure-requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
"X-Forwarded-For": "52.255.255.12",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"multiValueHeaders": {},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"resourceId": "2gxmpl",
"resourcePath": "/",
"httpMethod": "GET",
"extendedRequestId": "JJbxmplHYosFVYQ=",
"requestTime": "10/Mar/2020:00:03:59 +0000",
"path": "/Prod/",
"accountId": "123456789012",
"protocol": "HTTP/1.1",
"stage": "Prod",
"domainPrefix": "70ixmpl4fl",
"requestTimeEpoch": 1583798639428,
"requestId": "77375676-xmpl-4b79-853a-f982474efe18",
},
"body": "{\"userId\":\" [email protected] \"}",
"isBase64Encoded": false
}