Tutorial: A Subscription Workflow with Amazon SWF and Amazon SNS
This section provides a tutorial that describes how to create an Amazon SWF workflow application that consists of a set of four activities that operate sequentially. It also covers:
Setting default and execution-time workflow and activity options.
Polling Amazon SWF for decision and activity tasks.
Passing data between the activities and the workflow with Amazon SWF.
Waiting for human tasks and reporting heartbeats to Amazon SWF from an activity task.
Using Amazon SNS to create a topic, subscribe a user to it, and publish messages to subscribed endpoints.
You can use Amazon Simple Workflow Service (Amazon SWF) and Amazon Simple Notification Service (Amazon SNS) together to emulate a "human task" workflow—one in which a human worker is required to perform some action and then communicate with Amazon SWF to launch the next activity in the workflow.
Because Amazon SWF is a cloud-based web service, communication with Amazon SWF can originate from anywhere a connection to the Internet is available. In this case, we will use Amazon SNS to communicate with the user by either email, an SMS text message, or both.
This tutorial uses the AWS SDK for Ruby to access Amazon SWF and Amazon SNS, but there are many development options available, including the AWS Flow Framework for Ruby, which provides easier coordination and communication with Amazon SWF.
For a complete list of Amazon SWF development options, see Development Options.
In this section:
About the Workflow
The workflow that we will be developing consists of four major steps:
Get a subscription address (email or SMS) from the user.
Create an SNS topic and subscribe the provided endpoints to the topic.
Wait for the user to confirm the subscription.
If the user confirms, publish a congratulatory message to the topic.
These steps include activities that are completely automated (steps 2 and 4), and others that require the workflow to wait for a human to provide some data to the activity before the workflow can progress (steps 1 and 3).
Because each step relies on data that is generated by the previous step (you must have an endpoint before subscribing it to a topic, and you must have a topic subscription before you can wait for confirmation, etc.) This tutorial will also cover how to provide activity results upon completion, and how to pass input to a task that is being scheduled. Amazon SWF handles coordination and delivery of information between the activities and the workflow, and vice-versa.
We're also using both keyboard input and Amazon SNS to handle communication between Amazon SWF and the human who is providing data to the workflow. In practice, you can use many different techniques to communicate with human users, but Amazon SNS provides a very easy way to use email or text messages to notify the user about events in the workflow.
To follow along with this tutorial, you will need the following:
If you already have these set up, you're ready to continue. If you don't want to run the example, you can still follow the tutorial—much of the content in this tutorial applies to using Amazon SWF and Amazon SNS regardless of what development options you are using.
Download the Source Code
You can download the complete source code for this tutorial from: https://s3.amazonaws.com/codesamples/ruby/swf_sns_sample.zip
Even if you intend to type in (or cut and paste) the code from this tutorial directly into your own source files, having the downloaded source code available to compare your own code with can help identify and solve issues if you run into any along the way.
This tutorial is divided into the following steps: