Guest User

Untitled

a guest
Dec 13th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. module.exports.executeWorkflow = function (event, context) {
  2. if ('Records' in event) {
  3. const stateMachineName = process.env.STEP_FUNCTION_NAME;
  4. const stepfunctions = new AWS.StepFunctions();
  5.  
  6. async.waterfall([
  7. (next) => {
  8. console.log('Fetching the list of available workflows');
  9. return stepfunctions.listStateMachines({}, next);
  10. },
  11. (data, next) => {
  12. console.log(data, next);
  13. console.log('Searching for the step function', data);
  14.  
  15. for (var i = 0; i < data.stateMachines.length; i++) {
  16. const item = data.stateMachines[i];
  17. if (item.name === stateMachineName) {
  18. console.log('Found the step function', item);
  19. return next(null, item.stateMachineArn);
  20. }
  21. }
  22.  
  23. throw 'Step function with the given name doesn\'t exist';
  24. },
  25. (stateMachineArn, next) => {
  26. console.log('Executing the step function', stateMachineArn);
  27. const eventData = event.Records[0];
  28. return stepfunctions.startExecution({
  29. stateMachineArn: stateMachineArn,
  30. input: JSON.stringify({ objectKey: eventData.s3.object.key, bucketName: eventData.s3.bucket.name })
  31. }, next);
  32. },
  33. () => {
  34. return context.succeed('OK');
  35. }
  36. ]);
  37. } else {
  38. return context.fail('Incoming message doesn\'t contain "Records", it will be ignored', event);
  39. }
  40. };
Add Comment
Please, Sign In to add comment