AWS Step Functions
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

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

Step Functions integrates with certain AWS services, such as Amazon Simple Notification Service (SNS). In this tutorial, you generate a code snippet that sends a text message 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 AWS services directly from the Amazon States Language, see:

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 AWS Management Console and open the Step Functions console at https://console.aws.amazon.com/states/home?#/statemachines/create.

  2. Choose Author with code snippets, and then enter a name for your state machine.

    The default HelloWorld state machine is displayed in the State machine definition.

    
            HelloWorld definition
  3. For Generate Code Snippet, choose Amazon SNS: Publish a message.

    The Generate SNS Publish task state window is displayed.

  4. On the Generate SNS Publish task state page, under Destination, choose Enter phone number and then enter your cell phone number.

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

  5. 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.

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 your 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. Create or enter an IAM role:

    • To create an IAM role for Step Functions, select Create an IAM role for me, and enter a Name for your role.

    • If you have previously created an IAM role with the correct permissions for your state machine, select Choose an existing IAM role. Select a role from the list, or provide an ARN for that role.

    Note

    If you delete the IAM role that Step Functions creates, Step Functions can't recreate it later. Similarly, if you modify the role (for example, by removing Step Functions from the principals in the IAM policy), Step Functions can't restore its original settings later.

  9. Choose Create state machine.

Step 3: Start an Execution

After it's created, the page from your new state machine is displayed.

  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 Executions tab, choose Start execution.

  3. (Optional) Enter a name for your execution.

    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.

    Choose Start execution.

If you configured a valid cell 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.