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:
Topics
Prerequisites
-
Make sure you've added and verified the destination phone number to which you want to send SMS messages. For information about doing this, see Adding and verifying phone numbers in the SMS sandbox in the Amazon Simple Notification Service Developer Guide.
Step 1: Generate a Code Snippet
To generate a code snippet, you must start by editing a state machine definition.
-
Sign in to the Amazon Web Services Management Console and open the Step Functions console
. -
Choose Write your workflow in code.
-
For Type, keep the default selection of Standard.
The default
HelloWorld
state machine is displayed under Definition. -
From the Generate Code Snippet dropdown list, choose Amazon SNS: Publish a message.
The Generate SNS Publish task state window is displayed.
-
Under Destination, choose Phone number and then enter your phone number.
-
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
-
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.

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.
-
After you have reviewed the code in the Preview section, choose Copy to clipboard.
-
Place your cursor after the closing bracket of the
HelloWorld
state in your state machine definition.Enter a comma, press Enter to start a new line, and then paste the code snippet into your state machine definition.
-
Change the last line of the
Amazon SNS: Publish a message
state from
to"Next": "NEXT_STATE"
."End": true
-
Change the last line of the
HelloWorld
state from
to"End": true
."Next": "Amazon SNS: Publish a message"
-
Choose
in the Visual Workflow pane. Check the visual workflow to ensure your new state is included.
-
(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 } } } -
Choose Next.
-
Enter a name for your state machine. For example,
.using-code-snippets
-
For Permissions, keep the default selection of Create new role.
-
Choose Create state machine.
Step 3: Start an Execution
After you create your state machine, you start an execution.
-
Review the details of your state machine, including the Amazon Resource Name (ARN), the related IAM ARN, and the state machine definition.
-
On the using-code-snippets page, choose Start execution.
The Start execution dialog box is displayed.
-
(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.
-
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.