Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports.executeWorkflow = function (event, context) {
- if ('Records' in event) {
- const stateMachineName = process.env.STEP_FUNCTION_NAME;
- const stepfunctions = new AWS.StepFunctions();
- async.waterfall([
- (next) => {
- console.log('Fetching the list of available workflows');
- return stepfunctions.listStateMachines({}, next);
- },
- (data, next) => {
- console.log(data, next);
- console.log('Searching for the step function', data);
- for (var i = 0; i < data.stateMachines.length; i++) {
- const item = data.stateMachines[i];
- if (item.name === stateMachineName) {
- console.log('Found the step function', item);
- return next(null, item.stateMachineArn);
- }
- }
- throw 'Step function with the given name doesn\'t exist';
- },
- (stateMachineArn, next) => {
- console.log('Executing the step function', stateMachineArn);
- const eventData = event.Records[0];
- return stepfunctions.startExecution({
- stateMachineArn: stateMachineArn,
- input: JSON.stringify({ objectKey: eventData.s3.object.key, bucketName: eventData.s3.bucket.name })
- }, next);
- },
- () => {
- return context.succeed('OK');
- }
- ]);
- } else {
- return context.fail('Incoming message doesn\'t contain "Records", it will be ignored', event);
- }
- };
Add Comment
Please, Sign In to add comment