Search code examples
node.jsamazon-web-servicesaws-step-functions

Manual approval task in step function - invoke through a script


I have a step function with an activity task that should wait for an input from a script that I will run from my terminal. If the script is invoked, the task that is waiting should get succeeded

Could someone provide examples on how to achieve this?

Any helpful documentations or referenced code links are appreciated. Would I need an activity worker to invoke this? Can a script running in my terminal invoke the lambda and mark it as succeeded?

enter image description here

node report-choice-step-success.js --stepfunction-arn <SFN-EXEC> --step-name ManualTask

Script report-choice-step-success.js

const main = () => {
    let sfnClient;
    const rolename =  `StepFunctionExecuter-LOCAL`;

    return getcreds({ accountId: '123456789012', region: 'us-east-1', rolename })
        .then(params => {
            sfnClient = new AWS.StepFunctions(params)
        })
        
        .then(() => startmystepfunction(sfnClient));
};




const startmystepfunction = (sfnClient) => {
    const stateMachineArn = `arn:aws:states:us-east-1:123456789012:stateMachine:MYSTEPFUNCTION`;
    const name = `Manual step`;

    const executionParams = { name, stateMachineArn };
    return sfnClient.startExecution(executionParams).promise()
           .then(response => {
                if (response && response.executionArn) { 
                    print(`Started SFN execution for arn: ${response.executionArn}`);)
};




How should I modify the task so that it waits for a manual input so that it gets succeeded?

{
  "Comment": "My state machine",
  "StartAt": "Manual step",
  "States": {
    "ManualStep": {
      "Type": "Task",
      "Resource": "arn:aws:states:::activity:manualtask",
      "End": true
    }
  }
}

Solution

  • I could get the Activity ARN from the executionARN using the getExecutionHistory method and filtered the scheduled activities. Then I used that particular activityARN to getActivityTask and then used the sendTaskSuccess method to transition the step to succeeded.

    sfnClient.getActivityTask(params).promise()
      .then(data => {
        var params = {
          output: data.input.toString(),
          taskToken: data.taskToken.toString()
        };
    
        sfnClient.sendTaskSuccess(params).promise()
    
      }).catch(err => {
        console.error(err);
        console.error(err.stack);
      });