{"id":30197,"date":"2026-12-05T22:05:17","date_gmt":"2026-12-05T22:05:17","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30197"},"modified":"2026-12-05T22:05:17","modified_gmt":"2026-12-05T22:05:17","slug":"aws-sns-policy-example-to-allow-s3-events","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/aws-sns-policy-example-to-allow-s3-events\/","title":{"rendered":"AWS IAM SNS policy example to allow s3 events"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To send S3 event notifications to an Amazon SNS topic, the topic needs an access policy that lets the S3 service (<code>s3.amazonaws.com<\/code>) call <code>SNS:Publish<\/code>, 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.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/65df3776df2f8b006c5903c0fff6a82c.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What this policy is and where it goes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JSON above is not an IAM identity policy that you attach to a user or role. It is a <strong>resource-based policy<\/strong>, 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 <code>SNS:Publish<\/code> to <code>s3.amazonaws.com<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The gist file is named <code>sns-s3-aws.yaml<\/code>, but the content is JSON. SNS only accepts topic policies as JSON, so save the file as <code>sns-policy.json<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Line-by-line explanation<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Element<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>Version: 2012-10-17<\/code><\/td><td>The current policy language version. Always use this value.<\/td><\/tr><tr><td><code>Principal.Service: s3.amazonaws.com<\/code><\/td><td>Gives permission to the Amazon S3 service, not to a user or account.<\/td><\/tr><tr><td><code>Action: SNS:Publish<\/code><\/td><td>The only action S3 needs. Do not grant <code>SNS:*<\/code>.<\/td><\/tr><tr><td><code>Resource<\/code><\/td><td>The ARN of the topic this policy is attached to: <code>arn:aws:sns:&lt;region&gt;:&lt;account-id&gt;:&lt;topic-name&gt;<\/code>.<\/td><\/tr><tr><td><code>ArnLike aws:SourceArn<\/code><\/td><td>Allows 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.<\/td><\/tr><tr><td><code>StringEquals aws:SourceAccount<\/code><\/td><td>The 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.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected policy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, <code>aws:SourceAccount<\/code> is set to <code>62509999767<\/code>. 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 <code>111122223333<\/code> with your bucket owner&#039;s account ID, and replace the region, topic name and bucket name with your own.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"Version\"<\/span>: <span class=\"hljs-string\">\"2012-10-17\"<\/span>,\n  <span class=\"hljs-attr\">\"Id\"<\/span>: <span class=\"hljs-string\">\"s3-to-sns-policy\"<\/span>,\n  <span class=\"hljs-attr\">\"Statement\"<\/span>: &#91;\n    {\n      <span class=\"hljs-attr\">\"Sid\"<\/span>: <span class=\"hljs-string\">\"AllowS3Publish\"<\/span>,\n      <span class=\"hljs-attr\">\"Effect\"<\/span>: <span class=\"hljs-string\">\"Allow\"<\/span>,\n      <span class=\"hljs-attr\">\"Principal\"<\/span>: {\n        <span class=\"hljs-attr\">\"Service\"<\/span>: <span class=\"hljs-string\">\"s3.amazonaws.com\"<\/span>\n      },\n      <span class=\"hljs-attr\">\"Action\"<\/span>: <span class=\"hljs-string\">\"SNS:Publish\"<\/span>,\n      <span class=\"hljs-attr\">\"Resource\"<\/span>: <span class=\"hljs-string\">\"arn:aws:sns:ap-south-1:111122223333:s3-events\"<\/span>,\n      <span class=\"hljs-attr\">\"Condition\"<\/span>: {\n        <span class=\"hljs-attr\">\"ArnLike\"<\/span>: {\n          <span class=\"hljs-attr\">\"aws:SourceArn\"<\/span>: <span class=\"hljs-string\">\"arn:aws:s3:::my-upload-bucket\"<\/span>\n        },\n        <span class=\"hljs-attr\">\"StringEquals\"<\/span>: {\n          <span class=\"hljs-attr\">\"aws:SourceAccount\"<\/span>: <span class=\"hljs-string\">\"111122223333\"<\/span>\n        }\n      }\n    }\n  ]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">S3 bucket ARNs do not include a region or an account ID, so the exact form is <code>arn:aws:s3:::bucket-name<\/code>. The original form, <code>arn:aws:s3:*:*:bucket<\/code>, still matches under <code>ArnLike<\/code>, but the exact ARN is clearer and less permissive. To allow several buckets, make <code>aws:SourceArn<\/code> a JSON array of bucket ARNs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> To find your account ID, run <code>aws sts get-caller-identity --query Account --output text<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apply the policy and connect the bucket with the AWS CLI<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a <strong>standard<\/strong> 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.<\/li>\n\n\n<li>Attach the corrected policy to the topic with <code>set-topic-attributes<\/code>.<\/li>\n\n\n<li>Subscribe an endpoint, such as an email address, and confirm the subscription from the email that SNS sends.<\/li>\n\n\n<li>Save the bucket notification configuration that points to the topic.<\/li>\n\n\n<li>Upload a test object.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. Create the topic<\/span>\naws sns create-topic --name s3-events --region ap-south<span class=\"hljs-number\">-1<\/span>\n\n<span class=\"hljs-comment\"># 2. Attach the access policy (this replaces the whole existing policy)<\/span>\naws sns set-topic-attributes \\\n  --region ap-south<span class=\"hljs-number\">-1<\/span> \\\n  --topic-arn arn:aws:sns:ap-south<span class=\"hljs-number\">-1<\/span>:<span class=\"hljs-number\">111122223333<\/span>:s3-events \\\n  --attribute-name Policy \\\n  --attribute-value file:<span class=\"hljs-comment\">\/\/sns-policy.json<\/span>\n\n<span class=\"hljs-comment\"># 3. Subscribe an email address, then confirm from the inbox<\/span>\naws sns subscribe \\\n  --region ap-south<span class=\"hljs-number\">-1<\/span> \\\n  --topic-arn arn:aws:sns:ap-south<span class=\"hljs-number\">-1<\/span>:<span class=\"hljs-number\">111122223333<\/span>:s3-events \\\n  --protocol email \\\n  --notification-endpoint ops@example.com<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, create <code>notification.json<\/code>. This example sends an event for every object created under the <code>uploads\/<\/code> prefix:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"TopicConfigurations\"<\/span>: &#91;\n    {\n      <span class=\"hljs-attr\">\"Id\"<\/span>: <span class=\"hljs-string\">\"uploads-created\"<\/span>,\n      <span class=\"hljs-attr\">\"TopicArn\"<\/span>: <span class=\"hljs-string\">\"arn:aws:sns:ap-south-1:111122223333:s3-events\"<\/span>,\n      <span class=\"hljs-attr\">\"Events\"<\/span>: &#91;<span class=\"hljs-string\">\"s3:ObjectCreated:*\"<\/span>],\n      <span class=\"hljs-attr\">\"Filter\"<\/span>: {\n        <span class=\"hljs-attr\">\"Key\"<\/span>: {\n          <span class=\"hljs-attr\">\"FilterRules\"<\/span>: &#91;\n            { <span class=\"hljs-attr\">\"Name\"<\/span>: <span class=\"hljs-string\">\"prefix\"<\/span>, <span class=\"hljs-attr\">\"Value\"<\/span>: <span class=\"hljs-string\">\"uploads\/\"<\/span> }\n          ]\n        }\n      }\n    }\n  ]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 4. Apply the notification configuration (replaces any existing one)<\/span>\naws s3api put-bucket-notification-configuration \\\n  --bucket my-upload-bucket \\\n  --notification-configuration file:<span class=\"hljs-comment\">\/\/notification.json<\/span>\n\n<span class=\"hljs-comment\"># 5. Test<\/span>\n<span class=\"hljs-keyword\">echo<\/span> hello &gt; test.txt\naws s3 cp test.txt s3:<span class=\"hljs-comment\">\/\/my-upload-bucket\/uploads\/test.txt<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> <code>put-bucket-notification-configuration<\/code> replaces the bucket&#039;s whole notification configuration. If the bucket already sends events to Lambda or SQS, first run <code>aws s3api get-bucket-notification-configuration --bucket my-upload-bucket<\/code> and merge the existing entries into your file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Expected result<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When the notification configuration is saved, S3 sends an <code>s3:TestEvent<\/code> message to the topic. After the upload, the subscriber gets a JSON message with a <code>Records<\/code> array. Each record includes <code>eventName<\/code> (for example <code>ObjectCreated:Put<\/code>), <code>s3.bucket.name<\/code> and <code>s3.object.key<\/code>. Object keys are URL-encoded in the event, so a key with spaces arrives with <code>+<\/code> in place of each space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>InvalidArgument: Unable to validate the following destination configurations<\/code><\/td><td>The topic policy is missing or does not allow S3. Common reasons are a wrong <code>aws:SourceAccount<\/code>, a wrong bucket name in <code>aws:SourceArn<\/code>, or a wrong topic ARN in <code>Resource<\/code>.<\/td><td>Check each value against your real account ID, bucket and topic ARN, then apply the policy again.<\/td><\/tr><tr><td>The same validation error even though the policy is correct<\/td><td>The topic is in a different region from the bucket, or it is a FIFO topic<\/td><td>Use a standard topic in the bucket&#039;s region.<\/td><\/tr><tr><td>The same validation error on an encrypted topic<\/td><td>The topic uses SSE-KMS and S3 cannot use the key<\/td><td>Allow S3 in the KMS key policy (see the next section).<\/td><\/tr><tr><td>The configuration saves, but no emails arrive<\/td><td>The subscription is still <code>PendingConfirmation<\/code>, or the object key does not match the prefix or suffix filter<\/td><td>Confirm the subscription. Run <code>aws sns list-subscriptions-by-topic<\/code> to check its status, and check the filter rules.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Encrypted topics and security practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If the topic uses server-side encryption, you must use a customer managed KMS key. The AWS managed key <code>alias\/aws\/sns<\/code> will not work, because you cannot edit its key policy to add the S3 service. Add this statement to the customer managed key&#039;s policy:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"Sid\"<\/span>: <span class=\"hljs-string\">\"AllowS3ToUseKeyForSNS\"<\/span>,\n  <span class=\"hljs-attr\">\"Effect\"<\/span>: <span class=\"hljs-string\">\"Allow\"<\/span>,\n  <span class=\"hljs-attr\">\"Principal\"<\/span>: { <span class=\"hljs-attr\">\"Service\"<\/span>: <span class=\"hljs-string\">\"s3.amazonaws.com\"<\/span> },\n  <span class=\"hljs-attr\">\"Action\"<\/span>: &#91;<span class=\"hljs-string\">\"kms:GenerateDataKey\"<\/span>, <span class=\"hljs-string\">\"kms:Decrypt\"<\/span>],\n  <span class=\"hljs-attr\">\"Resource\"<\/span>: <span class=\"hljs-string\">\"*\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Always keep both conditions, <code>aws:SourceArn<\/code> and <code>aws:SourceAccount<\/code>. A policy that allows <code>s3.amazonaws.com<\/code> without them lets any bucket in any account publish to the topic.<\/li>\n\n\n<li>Grant only <code>SNS:Publish<\/code> to S3.<\/li>\n\n\n<li><code>set-topic-attributes<\/code> 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.<\/li>\n\n\n<li>Manage the topic, policy and bucket notification together in CloudFormation or Terraform so they cannot drift apart.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Is the SNS policy for S3 events an IAM policy?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can an S3 bucket send events to an SNS topic in another region?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do S3 event notifications support SNS FIFO topics?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I let several buckets publish to one SNS topic?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Make the <code>aws:SourceArn<\/code> condition a JSON array of bucket ARNs, or use a wildcard pattern such as <code>arn:aws:s3:::logs-*<\/code> with <code>ArnLike<\/code>. Keep <code>aws:SourceAccount<\/code> set to the account that owns the buckets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does S3 say it is unable to validate the destination configuration?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">S3 test-publishes to the topic when you save the notification configuration, and the publish was denied. Check the topic policy&#039;s account ID, bucket ARN and topic ARN, the region and topic type, and the KMS key policy if the topic is encrypted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/cloud-computing\/aws\/\">More AWS tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/complete-guide-of-aws-organization-and-aws-access-portal\/\">Complete Guide of AWS Organization and AWS access portal<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/aws-tutorials-aws-identity-center-guide\/\">AWS Tutorials: AWS Identity Center Guide<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5633],"tags":[26239,26242],"class_list":["post-30197","post","type-post","status-publish","format-standard","hentry","category-aws","tag-amazon-s3","tag-aws-iam"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30197"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30197\/revisions"}],"predecessor-version":[{"id":80678,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30197\/revisions\/80678"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}