How do I publish to an Amazon SNS topic from a cross-account Amazon EC2 instance using an IAM EC2 role?

Last updated: 2021-07-20

I want to publish to an Amazon Simple Notification Service (Amazon SNS) topic from a cross-account Amazon Elastic Compute Cloud (Amazon EC2) instance. I want to do this with an AWS Identity and Access Management (IAM) role for an EC2 instance that uses a public subnet.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

Configure the account with your EC2 instance

1.    Create an IAM policy with permissions to publish to your SNS topic. For example:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid":
    "VisualEditor0",
            "Effect": "Allow",
            "Action": "sns:Publish",
           
    "Resource": "arn:aws:sns:us-east-2:123456789012:cross-account-ec2-sns"
        }
    ]
}

2.    Create an IAM role for your EC2 instance. For example: cross-acc-ec2-sns-publish-role. Then, attach the policy to your IAM role.

3.    Create an EC2 instance in a public subnet with internet access.

Attach the new IAM role to your EC2 instance

Confirm that your EC2 instance is running, and then complete the following steps:

1.    Open the Amazon EC2 console.

2.    In the navigation pane, choose Instances.

3.    Select your instance (cross-acc-ec2-sns-publish-role), and then choose Actions, Security, Modify IAM role.

4.    Select the IAM role that you created earlier to attach to your instance, and then choose Save.

Note: If your EC2 instance can't connect to the internet with an internet gateway, see Why can't my EC2 instance connect to the internet using an internet gateway?

Configure the account with your SNS topic

1.    Create an SNS topic with a name similar to the following: cross-account-ec2-sns

2.    Subscribe an endpoint to your topic that's based on your use case, and then confirm the subscription.

3.    Add permissions to your SNS topic's access policy. The permissions must allow the EC2 IAM role to perform the Publish API operation for Amazon SNS.

To edit your SNS topic access policy and add the permissions, do the following:

1.    Open the Amazon SNS console.

2.    On the navigation panel, choose Topics, and then choose the SNS topic that you want to publish to.

3.    Choose the Access policy tab.

4.    In the Details section of your topic page, choose Edit.

5.    Expand the Access policy section, and then add permissions to allow the IAM role (cross-acc-ec2-sns-publish-role) of your EC2 instance to publish to the SNS topic. For example:

{
  "Sid": "grant-65864586-publish",
  "Effect": "Allow",
  "Principal": {
    "AWS": [
      "arn:aws:iam::111122223333:role/cross-acc-ec2-sns-publish-role"
    ]
  },
  "Action": "sns:Publish",
  "Resource": "arn:aws:sns:us-east-2:123456789012:cross-account-ec2-sns"
}

6.    Choose Save changes.

Test the configuration by publishing the notification from your EC2 instance

1.    Log in to the account with your EC2 instance, and then connect to your EC2 instance. For Windows instances, see Connect to your Windows instance. For Linux instances, see Connect to your Linux instance.

2.    Run the Amazon SNS Publish command in the EC2 terminal, and then monitor the response. For example, you can use a test query in the AWS CLI similar to the following:

aws sns publish  --topic-arn "arn:aws:sns:us-east-2:123456789012:cross-account-ec2-sns"
    --message "test" --region us-east-2
{
    "MessageId": "c72e7c2f-f95a-5c57-97c5-25448b2a7e71"
}

Did this article help?


Do you need billing or technical support?