Created
March 6, 2019 11:34
-
-
Save rafenden/a31a663f918e3cd17dcb9d74078c90f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aws = require('aws-sdk') | |
const express = require('express') | |
const bodyParser = require('body-parser') | |
const config = require('./lib/config') | |
// API Gateway | |
const apiLambdas = [ | |
{name: 'GetListOfUsers', endpoint: '/users', handler: require('./users-lambda').handler}, | |
] | |
// Functions invoked manually | |
const invokedLambdas = [ | |
{name: 'ImageBucketTrigger', handler: require('./resize-image-lambda').handler}, | |
] | |
const app = express() | |
const port = process.env.PORT || 3001 | |
app.use(bodyParser.json()) | |
app.use(bodyParser.raw({type: 'binary/octet-stream', limit : '100kb'})) | |
function apiInvocation(req, res, handler) { | |
const context = {} | |
const body = req.body | |
const event = { | |
"resource": req.path, | |
"path": req.path, | |
"httpMethod": req.method, | |
"headers": null, | |
"queryStringParameters": req.query, | |
"pathParameters": null, | |
"stageVariables": null, | |
"requestContext": { | |
"path": req.path, | |
"accountId": "000000000000", | |
"resourceId": "aztkla", | |
"stage": "test-invoke-stage", | |
"requestId": "test-invoke-request", | |
"identity": { | |
"cognitoIdentityPoolId": null, | |
"cognitoIdentityId": null, | |
"apiKey": "test-invoke-api-key", | |
"cognitoAuthenticationType": null, | |
"userArn": "arn:aws:iam::000000000000:root", | |
"apiKeyId": "test-invoke-api-key-id", | |
"userAgent": "userAgent", | |
"accountId": "000000000000", | |
"caller": "000000000000", | |
"sourceIp": "sourceIp", | |
"accessKey": "AADGAFCSFCSBCSBFCS", | |
"cognitoAuthenticationProvider": null, | |
"user": "000000000000" | |
}, | |
"resourcePath": req.path, | |
"httpMethod": req.method, | |
"extendedRequestId": "test-invoke-extendedRequestId", | |
"apiId": "inahj2igtc" | |
}, | |
"body": JSON.stringify(body), | |
"isBase64Encoded": false | |
} | |
handler(event, context, (error, response) => { | |
res.status(response.statusCode).set(response.headers).send(response.body) | |
}) | |
} | |
function manualInvocation(req, res, handler) { | |
const event = req.body | |
const context = {} | |
handler(event, context, (error, response) => { | |
res.status(response.statusCode).set(response.headers).send(response.body) | |
}) | |
} | |
app.use((req, res, next) => { | |
if (Buffer.isBuffer(req.body)) { | |
req.body = JSON.parse(req.body) | |
} | |
next() | |
}) | |
// Register Gateway API lambdas | |
apiLambdas.map((lambda) => { | |
app.get(lambda.endpoint, (req, res) => { | |
apiInvocation(req, res, lambda.handler) | |
}) | |
}) | |
// Register manually invoked lambdas | |
invokedLambdas.map((lambda) => { | |
app.post(`/:version/functions/${lambda.name}/invocations`, (req, res) => { | |
manualInvocation(req, res, lambda.handler) | |
}) | |
}) | |
app.post('/invoke/:func', async (req, res) => { | |
try { | |
const lambda = new aws.Lambda({ | |
endpoint: config.AWS_LAMBDA_ENDPOINT, | |
region: config.AWS_REGION, | |
}) | |
const data = await lambda.invoke({ | |
FunctionName: req.params.func, | |
Payload: JSON.stringify(req.body), | |
InvocationType: 'RequestResponse', | |
}).promise() | |
console.log('INVOCATION RETURN:', data) | |
res.status(data.StatusCode).send(JSON.parse(data.Payload)) | |
} | |
catch (e) { | |
console.error('INVOCATION ERROR:', e) | |
res.status(e.StatusCode).send(data.stack) | |
} | |
}) | |
module.exports = app | |
app.listen(port, () => console.log(`app listening on port ${port}!`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment