Using Code Snippets to Create a State to Send an Amazon SNS message - 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).

Using Code Snippets to Create a State to Send an Amazon SNS message

Amazon Step Functions integrates with certain Amazon Web Services, such as Amazon Simple Notification Service (Amazon SNS). In this tutorial, you generate a code snippet that sends a text message to an SMS-enabled device using Amazon SNS. You pass parameters directly to Amazon SNS from your state machine definition.

For more information about how Step Functions integrates with other Amazon Web Services directly from the Amazon States Language, see:

Prerequisites

Step 1: Generate a Code Snippet

To generate a code snippet, you must start by editing a state machine definition.

  1. Sign in to the Amazon Web Services Management Console and open the Step Functions console.

  2. Choose Write your workflow in code.

  3. For Type, keep the default selection of Standard.

    The default HelloWorld state machine is displayed under Definition.

    
                        HelloWorld definition
  4. From the Generate Code Snippet dropdown list, choose Amazon SNS: Publish a message.

    The Generate SNS Publish task state window is displayed.

  5. Under Destination, choose Phone number and then enter your phone number.

  6. Choose Enter phone number in the dropdown list that appears, and then enter your phone number in the box below.

    Use the format [+][country code][subscriber number including area code]. For example: +12065550123.

  7. Under Message, choose Enter message, and then enter some text to send as an SMS message.

    Note

    You can also choose Specify message at runtime with state input. This option enables you to use a reference path to select a message from the input of your state machine execution. For more information, see:

As you configure options on the Generate SNS Publish task state page, the Preview section updates with the Amazon States Language code for a Task state with the necessary options.

For example, choose these options.


                SNS state options

With these options selected, this is the generated code snippet that's displayed in the Preview area.

"Amazon SNS: Publish a message": { "Type": "Task", "Resource": "arn:aws:states:::sns:publish", "Parameters": { "Message": "Hello from Step Functions!", "PhoneNumber": "+12065550123" }, "Next": "NEXT_STATE" }
Note

Under the Task state options section, you can also configure Retry, Catch, and TimeoutSeconds options. See Error handling in Step Functions.

Step 2: Update Your State Machine Definition

Now that you have configured your Amazon SNS options, paste the generated code snippet into your state machine definition and update the existing Amazon States Language code.

  1. After you have reviewed the code in the Preview section, choose Copy to clipboard.

  2. Place your cursor after the closing bracket of the HelloWorld state in your state machine definition.

    
                        Place cursor at end of the HelloWorld state.

    Enter a comma, press Enter to start a new line, and then paste the code snippet into your state machine definition.

  3. Change the last line of the Amazon SNS: Publish a message state from "Next": "NEXT_STATE" to "End": true.

  4. Change the last line of the HelloWorld state from "End": true to "Next": "Amazon SNS: Publish a message".

  5. Choose 
       refresh
    in the Visual Workflow pane. Check the visual workflow to ensure your new state is included.

    
                        Review the visual workflow.
  6. (Optional) Indent the JSON to make your code easier to read. Your state machine definition should look like this.

    { "StartAt":"HelloWorld", "States":{ "HelloWorld":{ "Type":"Pass", "Result":"Hello World!", "Next":"Amazon SNS: Publish a message" }, "Amazon SNS: Publish a message":{ "Type":"Task", "Resource":"arn:aws:states:::sns:publish", "Parameters":{ "Message":"Hello from Step Functions!", "PhoneNumber":"+12065550123" }, "End":true } } }
  7. Choose Next.

  8. Enter a name for your state machine. For example, using-code-snippets.

  9. For Permissions, keep the default selection of Create new role.

  10. Choose Create state machine.

Step 3: Start an Execution

After you create your state machine, you start an execution.

  1. Review the details of your state machine, including the Amazon Resource Name (ARN), the related IAM ARN, and the state machine definition.

  2. On the using-code-snippets page, choose Start execution.

    The Start execution dialog box is displayed.

  3. (Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

    Note

    If we had chosen Specify message at runtime with state input when creating our Amazon SNS code snippet, we would include a message in the Input - optional. For now, you can use the default state input.

  4. Choose Start Execution.

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

If you specified a verified phone number in your code snippet, you should have received a text message from Amazon SNS that was triggered directly by your state machine execution.