Using Code Snippets to Create a State to Send an Amazon SNS message - AWS Step Functions

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

AWS Step Functions integrates with certain AWS services, such as Amazon Simple Notification Service (Amazon 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:

(Optional) Prerequisites

If you're using an existing role in AWS Identity and Access Management (IAM), you'll need to attach SNS:Publish permissions. You can use the AmazonSNSFullAccess AWS-managed policy to attach full permissions for Amazon SNS.

To attach permissions to a role

  1. Open the Roles page.

  2. Choose a role.

  3. Choose Attach policies.

  4. Type AmazonSNSFullAccess in the Filter policies text field.

  5. Select the AmazonSNSFullAccess role.

  6. Choose Attach policy.

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

  2. Choose Author with code snippets. For Type, choose Standard, 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.


    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" }

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


    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.


    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.