AWS SDK for Ruby
Developer Guide

Enabling a Resource to Publish to an Amazon SNS Topic

The following example enables the resource with the ARN my-resource-arn to publish to the topic with the ARN my-topic-arn in the us-west-2 region.

require 'aws-sdk-sns' # v2: require 'aws-sdk' policy = '{ "Version":"2008-10-17", "Id":"__default_policy_ID", "Statement":[{ "Sid":"__default_statement_ID", "Effect":"Allow", "Principal":{ "AWS":"*" }, "Action":["SNS:Publish"], "Resource":"' + my-topic-arn + '", "Condition":{ "ArnEquals":{ "AWS:SourceArn":"' + my-resource-arn + '"} } }] }' sns = Aws::SNS::Resource.new(region: 'us-west-2') # Get topic by ARN topic = sns.topic(my-topic-arn) # Add policy to topic topic.set_attributes({ attribute_name: "Policy", attribute_value: policy })