AWS Lambda Router for NodeJS
A collection of tools to handle ApiGateway requests and direct function invocation calls on AWS Lambda. Use it as a stand-alone lib in a Lambda function or plugged-in to Apex or any other serverless framework on NodeJS. I'd recommend deploying and managing your Lambda functions with Apex and LambdaReq since they play nicely together.
Installation
npm install lambda-req --save
Deploy it with Apex
const lambda = // set APIGateway handlerslambdalambda // set direct invocation handlerslambda invoke
Use it stand-alone on Lambda
// initialize Lambda with no params, pass them later from handlerconst lambda = // set handlerslambdalambda // export the handler// pass the event params on invocation time
Invoke other Lambdas
const lambda = // set APIGateway handlerslambda invoke
See more examples.
API
LambdaReq
This is the Lambda router object. It binds routes and proxies events for a Lambda handler.
Object
], [context = Object
], [callback = function
])
.constructor([event = Pass in the lambda handler event
, context
and callback
on object initialization. However, this is optional since they can be sent diectly on hanlder invocation.
Node 4.3:
const LambdaReq = default { const lambda = event context callback lambda} moduleexports = handler
With Babel:
{ const lambda = event context callback lambda}
String
, handler = function
)
.get|.post|.put|.delete|.options(path = Shorthand methods for binding APIGateway route handlers. ALL APIGateway params (path, query, body) are passed through the params
key on the req
argument. A router instance is passed as the second handler arg. router._event
and router._context
are mapped to the original handler params.
const lambda = event context callback lambdalambdalambdalambda lambda { const params headers = req return {}}
A request to https://api-gateway-id.execute-api.eu-west-1.amazonaws.com/prod/test
will fire up the GET
handler on the /test
route.
String
, handler = function
)
.proxy(name = Shorthand method for binding Lambda direct invocation handlers. Used for maintenance tasks (i.e. db migrations, cronjobs) and internal Lambda calls. Each proxy handler has a unique name, passed as a command
key on the Lambda event. A direct call, proxies all the event data to the handler.
const lambda = event context callback lambda lambda { const params = req return {}}
Invoke this functions from command line with aws-cli:
#!/usr/bin/env bash PAYLOAD='{"command":"db_migrate","params":{"name":"users"}}' aws lambda invoke \ --function-name myFunction \ --payload $PAYLOAD \ out.log
or through aws-sdk:
const config = FunctionName: 'myFunction' Payload: JSONconst lambda = lambda
Object
], [context = Object
], [callback = function
])
.invoke([event = Invokes the handlers that match the current Lambda route from APIGateway or by a direct invocation call. Optionally, Lambda handler params will be passed along. It's recommended to export this method as a Lambda handler.
const lambda = lambdalambda
or with Apex
and WebPack
const lambda = lambdalambda invoke
Bool
.isApiGateway It returns true
if the request was an APIGateway invocation. Internally, checks if an HTTP method is set on the Lambda event.
const lambda = event context callback if lambdaisApiGateway // it's a HTTP call
Bool
.isProxy It return true
if the request was a direct Lambda invocation. Internally, checks if a property command
is set on the Lambda event.
const lambda = event context callback if lambdaisProxy // it's a direct invocation
Object
.params Has all the params coming from event.queryStringParameters
, event.pathParameters
and event.body
for an APIGateway call.
Stores all the params coming from event.params
for a direct invocation.
Referenced through req.params
on a handler.
const lambda = event context callback lambdalambda if lambdaparams // process data
Object
.headers Stores the HTTP headers from an APIGateway call.
const lambda = event context callback lambda
String
.currentRoute Returns the current invocation route.
LambdaProxy
A wrapper for direct Lambda invocation calls. Sets the command
and parameters
on internal calls.
Object
= AWS.Lambda
])
.constructor([client = String
, command = String
, [params = Object
])
.invoke(functionName = Call a Lambda internally, pass in the command and params.
const proxy = proxy
LambdaReqError
A tiny wrapper for Lambda request errors.
.constructor({ message, status })
The error has a message and a status that will be passed along to the APIGateway.
lambda
See more examples.