Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Created September 3, 2018 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalinchernev/5afc5c536073c95b26eb0b72fece824d to your computer and use it in GitHub Desktop.
Save kalinchernev/5afc5c536073c95b26eb0b72fece824d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const path = require('path');
const https = require('https');
const AWS = require('aws-sdk');
const promisePipe = require('promisepipe');
const unzip = require('unzip');
const { argv } = require('yargs');
const runner = async () => {
const { REGION, AWS_LAMBDA_HANDLER_PATH } = process.env;
// Required as strings by the API: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_TaskOverride.html?shortFooter=true
const { event: e, context: c } = argv;
const context = JSON.parse(c);
const event = JSON.parse(e);
if (!context.functionName) {
return console.error('Required function name not found in context.');
}
const invoking = context.functionName;
// Assumes dead letter queue lambda functions are suffixed with Dlq
const originalInvoking = invoking.substring(0, invoking.indexOf('Dlq'));
try {
const lambda = new AWS.Lambda({ region: REGION });
const lambdaInfo = await lambda
.getFunction({ FunctionName: originalInvoking })
.promise();
const sourceCodeSignedUrl = lambdaInfo.Code.Location;
return https.get(sourceCodeSignedUrl, async res => {
// Download source from cloud and extract it at the current directory at the same time.
await promisePipe(res, unzip.Extract({ path: __dirname }));
const pathToHandler = path.resolve(
`${__dirname}/${AWS_LAMBDA_HANDLER_PATH}`
);
const handler = require(pathToHandler);
// Run the taget handler's code.
// handler.handler is the default export of webpack's bundle file
const result = await handler.handler(event, context);
return console.log(result);
});
} catch (err) {
return console.error(err.message);
}
};
runner();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment