Code snippet to update the sns subscription filter policy. Before updating the filter policy, update the filter policy scope also. By default the FilterPolicyScope is MessageAttributes
import boto3
# Create Boto3 SNS Client
sns_client = boto3.client('sns')
# This is sample Subscription Arn Replace It with the exact subscription Arn
subscription_arn = 'arn:aws:sns:us-east-1:88383899399:VerifyEmail:kkwi-4bba-iiio-kkkeioeoo'
# Define your updated filter policy
updated_filter_policy = {
'detail-type': ['AWS API Call via CloudTrail']},
'detail': {'eventName': ['CreateVolume']}
}
# Convert the updated filter policy to a JSON-formatted string
updated_filter_policy_json = json.dumps(updated_filter_policy)
try:
# Update the subscription Filter PolicyScope
sns_client.set_subscription_attributes(
SubscriptionArn=subscription_arn,
AttributeName='FilterPolicyScope',
AttributeValue='MessageBody' # By Default It is MessageAttributes
)
# Update the subscription filter policy
sns_client.set_subscription_attributes(
SubscriptionArn=subscription_arn,
AttributeName='FilterPolicy',
AttributeValue=updated_filter_policy_json
)
print(f"Subscription filter policy for {subscription_arn} has been updated.")
except Exception as e:
print(f"Exception: {e}")
raise e