Callback Pattern Example (Amazon SQS, Amazon SNS, Lambda) - Amazon Step Functions
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Callback Pattern Example (Amazon SQS, Amazon SNS, Lambda)

This sample project demonstrates how to have Amazon Step Functions pause during a task, and wait for an external process to return a task token that was generated when the task started.

When this sample project is deployed and an execution is started, the following steps occur:

  1. Step Functions passes a message that includes a task token to an Amazon Simple Queue Service (Amazon SQS) queue.

  2. Step Functions then pauses, waiting for that token to be returned.

  3. The Amazon SQS queue triggers an Amazon Lambda function that calls SendTaskSuccess with that same task token.

  4. When the task token is received, the workflow continues.

  5. The "Notify Success" task publishes an Amazon Simple Notification Service (Amazon SNS) message that the callback was received.

To learn how to implement the callback pattern in Step Functions, see Wait for a Callback with the Task Token.

For more information about how Amazon Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.

Create State Machine and Provision Resources

  1. Open the Step Functions console and choose Create a state machine.

  2. Choose Sample Projects, and then choose Callback Pattern Example.

    The state machine Code and Visual Workflow are displayed.

          Callback example workflow.
  3. Choose Create Sample Project.

    The Create Project Resources page is displayed, listing the resources that will be created. For this sample project the resources include:

    • An Amazon SQS message queue.

    • A Lambda function, that calls the Step Functions API action SendTaskSuccess.

    • An Amazon SNS topic to notify success or failure when the workflow can continue.


    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Create Project Resources page displays Creating resources, you can open the Stack ID: link to see which resources are being provisioned.

    When complete, the New execution page is displayed.

  4. (Optional) Enter an execution name, and sample input.

          Start callback execution
  5. Choose Start Execution.

    A new execution of your state machine starts, and a new page showing your running execution is displayed.

          Callback execution
  6. To review how Step Functions progressed through the workflow and received a callback from Amazon SQS, review the entries in the Execution event history.

Lambda Callback Example

To see how the components of this sample project work together, see the resources that were deployed in your Amazon account. For example, here is the Lambda function that calls Step Functions with the task token.

console.log('Loading function'); const aws = require('aws-sdk'); exports.lambda_handler = (event, context, callback) => { const stepfunctions = new aws.StepFunctions(); for (const record of event.Records) { const messageBody = JSON.parse(record.body); const taskToken = messageBody.TaskToken; const params = { output: "\"Callback task completed successfully.\"", taskToken: taskToken }; console.log(`Calling Step Functions to complete callback task with params ${JSON.stringify(params)}`); stepfunctions.sendTaskSuccess(params, (err, data) => { if (err) { console.error(err.message); callback(err.message); return; } console.log(data); callback(null); }); } };