lambda-request-handler
Advanced tools
Comparing version 0.0.1-beta.1 to 0.0.1
{ | ||
"name": "lambda-request-handler", | ||
"version": "0.0.1-beta.1", | ||
"version": "0.0.1", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -9,2 +9,10 @@ # lambda-request-handler | ||
It supports `nodejs8.10` and `nodejs10.x` execution environments. | ||
The main differences between this module and `aws-serverless-express` are | ||
* It's using [in-process-request](https://github.com/janaz/in-process-request) module to execute app handlers in-process without having to start background http server | ||
* Simpler setup as it doesn't require managing the internal http server | ||
* It's faster, because it doesn't need to pass the request to the internal server through the unix socket | ||
* It's free from issues caused by limits in Node.js http module such as header size limit | ||
## Usage | ||
@@ -15,3 +23,3 @@ | ||
```sh | ||
npm install lambda-request-handler | ||
$ npm install lambda-request-handler | ||
``` | ||
@@ -32,3 +40,3 @@ | ||
const handler = lambdaRequestHandler(app); | ||
const handler = lambdaRequestHandler(app) | ||
@@ -39,1 +47,49 @@ module.exports = { handler } | ||
If the above file in your Lambda source was called `index.js` then the name of the handler in the Lambda configuration is `index.handler` | ||
### Advanced example | ||
Sometimes the application needs to read configuration from remote source before it can start processing requests. For example it may need to decrypt some secrets managed by KMS. | ||
```javascript | ||
const lambdaRequestHandler = require('lambda-request-handler') | ||
const AWS = require('aws-sdk') | ||
const express = require('express') | ||
const kms = new AWS.KMS() | ||
const myKmsPromise = () => | ||
kms.decrypt({ | ||
CiphertextBlob: Buffer.from(process.env.ENCRYPTED_SECRET, 'base64') | ||
}) | ||
.promise() | ||
.then((data) => { | ||
process.env.SECRET = data.Plaintext.toString('ascii') | ||
}); | ||
cont app = express() | ||
app.get('/secret', (req, res) => { | ||
res.json({ | ||
secret: process.env.SECRET, | ||
}) | ||
}) | ||
const myAppHandler = lambdaRequestHandler(app) | ||
let _myKmsPromise; | ||
const handler = (event) => { | ||
if (!_myKmsPromise) { | ||
// _myKmsPromise is in global scope so that only one request to KMS is made during this Lambda lifecycle | ||
_myKmsPromise = myKmsPromise(); | ||
} | ||
return _myKmsPromise | ||
.then(() => { | ||
// at this point the secret is decrypted and available as process.env.SECRET to the app | ||
return myAppHandler(event); | ||
}) | ||
} | ||
module.exports = { handler } | ||
``` |
8559
92