Last active
January 17, 2017 13:06
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
// 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