Getting Amazon SNS Notifications When a Command Changes Status
You can configure Amazon Simple Notification Service (Amazon SNS) to send notifications about the status of commands you send using Amazon EC2 Run Command. Amazon SNS coordinates and manages the delivery or sending of notifications to subscribing clients or endpoints. You can receive a notification whenever a command changes to a new state or changes to a specific state, such as failed or timed out. In cases where you send a command to multiple instances, you can receive a notification for each copy of the command sent to a specific instance. Each copy is called an invocation.
Amazon SNS can deliver notifications as HTTP or HTTPS POST, email (SMTP, either plain-text or in JSON format), or as a message posted to an Amazon Simple Queue Service (Amazon SQS) queue. For more information, see What Is Amazon SNS in the Amazon Simple Notification Service Developer Guide.
For example, if you configure Amazon SNS to send a notification when a command status changes to failed, SNS sends an email notification with the details of the command execution.
Note
If you prefer, you can use Amazon CloudWatch Events to configure a target to invoke an AWS Lambda function when a command changes status. For more information, see Log Command Execution Status Changes for Run Command.
To set up Amazon SNS notifications when a command changes status, you must complete the following tasks.
Configure Amazon SNS Notifications for SSM
Run Command supports sending Amazon SNS notifications for commands that enter the following statuses. For information about the conditions that cause a command to enter one of these statuses, see Monitoring Commands.
In Progress
Success
Failed
Timed Out
Canceled
Note
Commands sent using Run Command also report Cancelling and Pending status. These statuses are not captured by SNS notifications.
If you configure Run Command for SNS notifications, SNS sends summary messages that include the following information:
| Field | Type | Description |
|---|---|---|
|
EventTime |
String |
The time the event was triggered. The time stamp is important because SNS does not guarantee message delivery order. Example: 2016-04-26T13:15:30Z |
|
DocumentName |
String |
The name of the SSM document used to execute this command. |
|
CommandId |
String |
The ID generated by Run Command after the command was sent. |
ExpiresAfter | Date | If this time is reached and the command has not already started executing, it will not execute. |
| OutputS3BucketName | String | The Amazon Simple Storage Service (Amazon S3) bucket where the responses to the command execution should be stored. |
| OutputS3KeyPrefix | String | The Amazon S3 directory path inside the bucket where the responses to the command execution should be stored. |
|
RequestedDateTime |
String |
The time and date the request was sent to this specific instance. |
|
InstanceId |
String |
The instance targeted by the command. |
|
Status |
String |
Command status for the command. |
If you send a command to multiple instances, Amazon SNS can send messages about each copy or invocation of the command that include the following information:
| Field | Type | Description |
|---|---|---|
|
EventTime |
String |
The time the event was triggered. The time stamp is important because SNS does not guarantee message delivery order. Example: 2016-04-26T13:15:30Z |
|
DocumentName |
String |
The name of the SSM document used to execute this command. |
|
RequestedDateTime |
String |
The time and date the request was sent to this specific instance. |
|
CommandId |
String |
The ID generated by Run Command after the command was sent. |
|
InstanceId |
String |
The instance targeted by the command. |
|
Status |
String |
Command status for this invocation. |
Configure Account Permissions
When you send a command that is configured for notifications, you specify a service role Amazon Resource Name (ARN). For example: --service-role-arn=arn:aws:iam::123456789012:myrole. This service role is used by SSM to trigger SNS notifications.
To receive notifications from the Amazon SNS service, you must either attach the iam:PassRole policy to your existing AWS Identity and Access Management (IAM) user account, or create a new IAM account and attach this policy to it. If you create a new account, you must also attach the AmazonSSMFullAccess policy so the account can communicate with the SSM API.
Use the following procedure to attach an IAM policy to your user account. If you need to create a new user account, see Creating an IAM User in Your AWS Account in the IAM User Guide.
To attach the iam:PassRole policy to your user account
Open the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.
In the navigation pane, choose Users and select the user (under User name).
At the top of the page, copy your User ARN to the clipboard.
Under Permissions, verify that either the
AmazonSSMFullAccesspolicy is listed or there is a comparable policy that gives you permission to the SSM API.Choose Add inline policy.
On the Set Permissions page, choose Policy Generator, and then choose Select.
Verify that Effect is set to Allow.
From AWS Services choose AWS Identity and Access Management.
From Actions choose PassRole.
In the Amazon Resource Name (ARN) field, paste your ARN.
Choose Add Statement, and then choose Next.
On the Review Policy page, choose Apply Policy.
Create an IAM Role for Notifications
In the previous procedure, you added an IAM policy to your user account so that you could send commands that return notifications. In the following procedure, you will create a role so that the SSM service can act on your behalf when sending notifications.
Open the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.
In the navigation pane, choose Roles, and then choose Create New Role.
In Step 1: Set Role Name enter a name that identifies this role as a Run Command role for notifications.
In Step 2: Select Role Type choose Amazon EC2. The system skips Step 3: Establish Trust because this is a managed policy.
In Step 4: Attach Policy choose AmazonSNSFullAccess.
Choose Next Step and then choose Create Role. The system returns you to the Roles page.
Locate the role you just created and double-click it.
Choose the Trust Relationships tab, and then choose Edit Trust Relationship.
Add "ssm.amazonaws.com" to the existing policy as the following code snippet illustrates:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com", "Service": "ssm.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }Note
You must add a comma after the existing entry. "Service": "sns.amazonaws.com", or the JSON will not validate.
Choose Update Trust Policy.
Copy or make a note of the Role ARN. You will specify this ARN when you send a command that is configured to return notifications.
Configure Amazon SNS
To use Amazon SNS to send email notifications, you must first create a topic and then subscribe your email addresses to the topic.
Create an Amazon SNS Topic
An Amazon SNS topic is a logical access point, a communication channel that Run Command uses to send the notifications. You create a topic by specifying a name for your topic.
For more information, see Create a Topic in the Amazon Simple Notification Service Developer Guide.
Note
After you create the topic, copy or make a note of the Topic ARN. You will specify this ARN when you send a command that is configured to return status notifications.
Subscribe to the Amazon SNS Topic
To receive the notifications that Run Command sends to the topic, you must subscribe an endpoint to the topic. In this procedure, for Endpoint, specify the email address where you want to receive the notifications from Run Command.
For more information, see Subscribe to a Topic in the Amazon Simple Notification Service Developer Guide.
Confirm Your Amazon SNS Subscription
Amazon SNS sends a confirmation email to the email address that you specified in the previous step.
Make sure you open the email from AWS Notifications and choose the link to confirm the subscription before you continue with the next step.
You will receive an acknowledgement message from AWS. Amazon SNS is now configured to receive notifications and send the notification as an email to the email address that you specified.
Send a Command that Returns Status Notifications
This section shows you how to send a command that is configured to return status notifications using either the Amazon EC2 console or the AWS Command Line Interface (AWS CLI).
To send a command from the Amazon EC2 console that returns notifications
Open the Amazon EC2 console and choose Command History in the navigation pane.
Choose Run a Command.
In the Command document list, choose an SSM document.
Choose Select target instances to select the instances where you want the command to run. If you do not see a complete list of instances, the missing instances might not be configured properly for Run Command. For more information, see Amazon EC2 Run Command Prerequisites.
Enter information in the fields required by the SSM document. In the SNS Notifications section, choose Enable SNS notifications.
In the Role field, type or paste the IAM role ARN you created earlier.
In the SNS Topic field, type or paste the Amazon SNS ARN you created earlier.
In the Notify me on field, choose the events for which you want to receive notifications.
In the Notify me for field, choose to receive notifications for each copy of a command sent to multiple instances (invocations) or the command summary.
Choose Run.
Check your email for a message from Amazon SNS and open the email. Amazon SNS can take a few minutes to send the mail.
To send a command that is configured for notifications from the AWS CLI
Open the AWS CLI.
Specify parameters in the following command.
aws ssm send-command --instance-ids "ID-1, ID-2" --document-name "name" --parameters commands=date --service-roleServiceRole ARN--notification-config NotificationArn=SNS ARNFor example
aws ssm send-command --instance-ids "i-12345678, i-34567890" --document-name "AWS-RunPowerShellScript" --parameters commands=date --service-role arn:aws-cn:iam:: 123456789012:myrole --notification-config NotificationArn=arn:aws-cn:sns:cn-north-1:123456789012:testPress Enter.
Check your email for a message from Amazon SNS and open the email. Amazon SNS can take a few minutes to send the mail.
For more information about configuring Run Command from the command line, see Amazon EC2 Simple Systems Manager API Reference and the SSM AWS CLI Reference.

