MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

AWS IAM SNS policy example to allow s3 events

To send S3 event notifications to an Amazon SNS topic, the topic needs an access policy that lets the S3 service (s3.amazonaws.com) call SNS:Publish, and only on behalf of your bucket and your account. This page explains each line of the topic policy above and fixes the account ID error in it. It then covers applying the policy, connecting the bucket and testing the setup with the AWS CLI.


What this policy is and where it goes

The JSON above is not an IAM identity policy that you attach to a user or role. It is a resource-based policy, also called a topic access policy, and it is stored on the SNS topic itself. When an S3 bucket has an event notification that points at the topic, the S3 service publishes messages to the topic on your behalf. SNS checks this policy and allows the publish only if a statement grants SNS:Publish to s3.amazonaws.com.

The gist file is named sns-s3-aws.yaml, but the content is JSON. SNS only accepts topic policies as JSON, so save the file as sns-policy.json.

Line-by-line explanation

ElementMeaning
Version: 2012-10-17The current policy language version. Always use this value.
Principal.Service: s3.amazonaws.comGives permission to the Amazon S3 service, not to a user or account.
Action: SNS:PublishThe only action S3 needs. Do not grant SNS:*.
ResourceThe ARN of the topic this policy is attached to: arn:aws:sns:<region>:<account-id>:<topic-name>.
ArnLike aws:SourceArnAllows publishing only when the request comes from this bucket. Without it, any bucket in any account could publish to your topic if it knew the ARN.
StringEquals aws:SourceAccountThe 12-digit account ID of the account that owns the bucket. This prevents the confused deputy problem, for example if your bucket is deleted and someone else creates a bucket with the same name.

Corrected policy

In the example above, aws:SourceAccount is set to 62509999767. That value has only 11 digits, so it can never match a real account, and it is different from the account in the topic ARN. Because the condition never matches, S3 cannot publish to the topic, and saving the bucket notification fails. The version below uses placeholders. Replace 111122223333 with your bucket owner's account ID, and replace the region, topic name and bucket name with your own.

{
  "Version": "2012-10-17",
  "Id": "s3-to-sns-policy",
  "Statement": [
    {
      "Sid": "AllowS3Publish",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "SNS:Publish",
      "Resource": "arn:aws:sns:ap-south-1:111122223333:s3-events",
      "Condition": {
        "ArnLike": {
          "aws:SourceArn": "arn:aws:s3:::my-upload-bucket"
        },
        "StringEquals": {
          "aws:SourceAccount": "111122223333"
        }
      }
    }
  ]
}Code language: JSON / JSON with Comments (json)

S3 bucket ARNs do not include a region or an account ID, so the exact form is arn:aws:s3:::bucket-name. The original form, arn:aws:s3:*:*:bucket, still matches under ArnLike, but the exact ARN is clearer and less permissive. To allow several buckets, make aws:SourceArn a JSON array of bucket ARNs.

Note: To find your account ID, run aws sts get-caller-identity --query Account --output text.

Apply the policy and connect the bucket with the AWS CLI

  1. Create a standard topic in the same region as the bucket. S3 event notifications cannot publish to an SNS topic in a different region, and they do not support FIFO topics.
  2. Attach the corrected policy to the topic with set-topic-attributes.
  3. Subscribe an endpoint, such as an email address, and confirm the subscription from the email that SNS sends.
  4. Save the bucket notification configuration that points to the topic.
  5. Upload a test object.
# 1. Create the topic
aws sns create-topic --name s3-events --region ap-south-1

# 2. Attach the access policy (this replaces the whole existing policy)
aws sns set-topic-attributes \
  --region ap-south-1 \
  --topic-arn arn:aws:sns:ap-south-1:111122223333:s3-events \
  --attribute-name Policy \
  --attribute-value file://sns-policy.json

# 3. Subscribe an email address, then confirm from the inbox
aws sns subscribe \
  --region ap-south-1 \
  --topic-arn arn:aws:sns:ap-south-1:111122223333:s3-events \
  --protocol email \
  --notification-endpoint ops@example.comCode language: PHP (php)

Next, create notification.json. This example sends an event for every object created under the uploads/ prefix:

{
  "TopicConfigurations": [
    {
      "Id": "uploads-created",
      "TopicArn": "arn:aws:sns:ap-south-1:111122223333:s3-events",
      "Events": ["s3:ObjectCreated:*"],
      "Filter": {
        "Key": {
          "FilterRules": [
            { "Name": "prefix", "Value": "uploads/" }
          ]
        }
      }
    }
  ]
}Code language: JSON / JSON with Comments (json)
# 4. Apply the notification configuration (replaces any existing one)
aws s3api put-bucket-notification-configuration \
  --bucket my-upload-bucket \
  --notification-configuration file://notification.json

# 5. Test
echo hello > test.txt
aws s3 cp test.txt s3://my-upload-bucket/uploads/test.txtCode language: PHP (php)

Note: put-bucket-notification-configuration replaces the bucket's whole notification configuration. If the bucket already sends events to Lambda or SQS, first run aws s3api get-bucket-notification-configuration --bucket my-upload-bucket and merge the existing entries into your file.

Expected result

When the notification configuration is saved, S3 sends an s3:TestEvent message to the topic. After the upload, the subscriber gets a JSON message with a Records array. Each record includes eventName (for example ObjectCreated:Put), s3.bucket.name and s3.object.key. Object keys are URL-encoded in the event, so a key with spaces arrives with + in place of each space.

Common errors and fixes

SymptomLikely causeFix
InvalidArgument: Unable to validate the following destination configurationsThe topic policy is missing or does not allow S3. Common reasons are a wrong aws:SourceAccount, a wrong bucket name in aws:SourceArn, or a wrong topic ARN in Resource.Check each value against your real account ID, bucket and topic ARN, then apply the policy again.
The same validation error even though the policy is correctThe topic is in a different region from the bucket, or it is a FIFO topicUse a standard topic in the bucket's region.
The same validation error on an encrypted topicThe topic uses SSE-KMS and S3 cannot use the keyAllow S3 in the KMS key policy (see the next section).
The configuration saves, but no emails arriveThe subscription is still PendingConfirmation, or the object key does not match the prefix or suffix filterConfirm the subscription. Run aws sns list-subscriptions-by-topic to check its status, and check the filter rules.

Encrypted topics and security practices

If the topic uses server-side encryption, you must use a customer managed KMS key. The AWS managed key alias/aws/sns will not work, because you cannot edit its key policy to add the S3 service. Add this statement to the customer managed key's policy:

{
  "Sid": "AllowS3ToUseKeyForSNS",
  "Effect": "Allow",
  "Principal": { "Service": "s3.amazonaws.com" },
  "Action": ["kms:GenerateDataKey", "kms:Decrypt"],
  "Resource": "*"
}Code language: JSON / JSON with Comments (json)
  • Always keep both conditions, aws:SourceArn and aws:SourceAccount. A policy that allows s3.amazonaws.com without them lets any bucket in any account publish to the topic.
  • Grant only SNS:Publish to S3.
  • set-topic-attributes overwrites the default topic policy. Principals in the same account still get access through their IAM permissions. Other accounts that need to subscribe or publish must be added back explicitly.
  • Manage the topic, policy and bucket notification together in CloudFormation or Terraform so they cannot drift apart.

Frequently asked questions

Is the SNS policy for S3 events an IAM policy?

No. It is a resource-based access policy attached to the SNS topic. The principal is the S3 service, so you do not create an IAM role for this integration.

Can an S3 bucket send events to an SNS topic in another region?

No. The SNS topic must be in the same AWS region as the bucket. The topic can be in a different account if its policy allows it.

Do S3 event notifications support SNS FIFO topics?

No. S3 event notifications can only publish to standard SNS topics. If you need ordering, send the events to SQS or EventBridge and process them from there.

How do I let several buckets publish to one SNS topic?

Make the aws:SourceArn condition a JSON array of bucket ARNs, or use a wildcard pattern such as arn:aws:s3:::logs-* with ArnLike. Keep aws:SourceAccount set to the account that owns the buckets.

Why does S3 say it is unable to validate the destination configuration?

S3 test-publishes to the topic when you save the notification configuration, and the publish was denied. Check the topic policy's account ID, bucket ARN and topic ARN, the region and topic type, and the KMS key policy if the topic is encrypted.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

To become a DevOps Engineer on AWS, you should learn the following:

Jami Raj MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errandsโ€”all with the MotoShare.in…

Read More

AWS Interview Questions & Answers Complete Sets

AWS Global Infrastructure Explain the following Availability zoneRegionEdge location IAM What is IAM? What are some of its features? Given an example of IAM best practices? What…

Read More

Example of S3 Bucket Policy to allow from IP address and Domain name

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

AWS Interview Questions and Answer Part โ€“ 35

Cloud Trail delivers logs to which of the following? S3 buckets and RedShift instances S3 buckets and EC2 instances S3 buckets and CloudWatch Logs groups (Ans) S3…

Read More

How to set Security Credentials in a AWS Code Build

AWS CodeBuild should get its AWS credentials from an IAM service role attached to the build project, not from access keys typed into environment variables. This guide…

Read More

AWS IAM Policy to Protect SES SMTP

You can lock down Amazon SES SMTP credentials with an IAM policy on the IAM user that owns them. A Deny on aws:SourceIp limits which IP addresses…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x