Skip to content

Instantly share code, notes, and snippets.

@lusentis
Last active January 17, 2017 13:06
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 lusentis/20f4f50f48158eb2e8708ac96c2a69ab to your computer and use it in GitHub Desktop.
Save lusentis/20f4f50f48158eb2e8708ac96c2a69ab to your computer and use it in GitHub Desktop.
// Usage:
// $ babel-node keepalive.js > keepalive-code.js && \
// zip keepalive-code.zip keepalive-code.js && \
// aws lambda update-function-code \
// --function-name DawsonKeepAliveProd \
// --zip-file fileb://./keepalive-code.zip && \
// rm keepalive-code*
import execa from 'execa';
import assert from 'assert';
const DAWSON_STAGE = process.env.DAWSON_STAGE;
assert(DAWSON_STAGE, 'DAWSON_STAGE env is required');
const SNS_FAILURE_TOPIC_ARN = 'arn:aws:sns:eu-west-1:XXX:GePodKeepAliveFailed';
const createInvokeCode = name => {
return `
lambda.invoke({
FunctionName: '${name}',
InvocationType: 'RequestResponse',
LogType: 'None',
Payload: JSON.stringify({ __ping: true })
}).promise()`;
};
const createFunctionCode = functionNames => {
return `
module.exports.handler = function (event, context, callback) {
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({});
Promise.all([
${functionNames.map(name => {
return createInvokeCode(name);
}).join(',\n')}
])
.then(res => {
console.error(res.length, 'invocations completed');
console.error('Lambda status codes:', res.map(r => r.StatusCode));
})
.catch(err => {
console.error('One invocation failed (aborting whole execution)', err);
sns.publish({
TopicArn: '${SNS_FAILURE_TOPIC_ARN}',
Message: 'One or more keepalive invocations failed: \\n' + JSON.stringify(err, null, 2)
}).promise()
.then(() => callback(err.message))
.catch(snsError => callback(snsError.message));
})
};
`;
};
execa.shell(`cd .. && dawson describe --shell | grep -i '\\-lambda'`)
.then(result => {
assert(result.code === 0);
const functionNames = result.stdout.split('\n').map(v => v.split('=')[1]);
const functionCode = createFunctionCode(functionNames);
process.stdout.write(functionCode);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment