Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. // Usage:
  2. // $ babel-node keepalive.js > keepalive-code.js && \
  3. // zip keepalive-code.zip keepalive-code.js && \
  4. // aws lambda update-function-code \
  5. // --function-name DawsonKeepAliveProd \
  6. // --zip-file fileb://./keepalive-code.zip && \
  7. // rm keepalive-code*
  8.  
  9. import execa from 'execa';
  10. import assert from 'assert';
  11.  
  12. const DAWSON_STAGE = process.env.DAWSON_STAGE;
  13. assert(DAWSON_STAGE, 'DAWSON_STAGE env is required');
  14.  
  15. const SNS_FAILURE_TOPIC_ARN = 'arn:aws:sns:eu-west-1:XXX:GePodKeepAliveFailed';
  16.  
  17. const createInvokeCode = name => {
  18. return `
  19. lambda.invoke({
  20. FunctionName: '${name}',
  21. InvocationType: 'RequestResponse',
  22. LogType: 'None',
  23. Payload: JSON.stringify({ __ping: true })
  24. }).promise()`;
  25. };
  26.  
  27. const createFunctionCode = functionNames => {
  28. return `
  29. module.exports.handler = function (event, context, callback) {
  30. const AWS = require('aws-sdk');
  31. const lambda = new AWS.Lambda({});
  32.  
  33. Promise.all([
  34. ${functionNames.map(name => {
  35. return createInvokeCode(name);
  36. }).join(',\n')}
  37. ])
  38. .then(res => {
  39. console.error(res.length, 'invocations completed');
  40. console.error('Lambda status codes:', res.map(r => r.StatusCode));
  41. })
  42. .catch(err => {
  43. console.error('One invocation failed (aborting whole execution)', err);
  44. sns.publish({
  45. TopicArn: '${SNS_FAILURE_TOPIC_ARN}',
  46. Message: 'One or more keepalive invocations failed: \\n' + JSON.stringify(err, null, 2)
  47. }).promise()
  48. .then(() => callback(err.message))
  49. .catch(snsError => callback(snsError.message));
  50. })
  51. };
  52. `;
  53. };
  54.  
  55. execa.shell(`cd .. && dawson describe --shell | grep -i '\\-lambda'`)
  56. .then(result => {
  57. assert(result.code === 0);
  58. const functionNames = result.stdout.split('\n').map(v => v.split('=')[1]);
  59. const functionCode = createFunctionCode(functionNames);
  60. process.stdout.write(functionCode);
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement