Yes, it is possible to add MessageAttributes when publishing from AWS EventBridge to SNS as a rule target in order to enable subscription filtering.
Here is an example of creating an SNS topic with a specific set of attributes and then creating an EventBridge rule that publishes to that topic with message attributes:
import boto3
# Create SNS client
sns_client = boto3.client('sns')
# Create SNS topic with specific set of attributes
topic_response = sns_client.create_topic(
Name='my-topic',
Attributes={
'DisplayName': 'My Topic',
'TopicArn': 'arn:aws:sns:us-east-1:123456789012:my-topic',
'DeliveryPolicy': {
'http': {
'defaultHealthyRetryPolicy': {
'minDelayTarget': 1,
'maxDelayTarget': 2,
'numRetries': 3,
'numMaxDelayRetries': 2,
'backoffFunction': 'linear'
},
'disableSubscriptionOverrides': False,
'defaultThrottlePolicy': {
'maxReceivesPerSecond': 5
},
'version': '2012-10-17'
}
}
}
)
# Create EventBridge client
event_bridge_client = boto3.client('events')
# Create EventBridge rule that publishes to SNS topic with message attributes
rule_response = event_bridge_client.put_rule(
Name='my-rule',
EventPattern={
'source': ['my-source']
},
State='ENABLED',
Targets=[
{
'Arn': topic_response['TopicArn'],
'Id': 'my-target',
'InputPath': '$.detail',
'InputTransformer': {
'InputPathsMap': {
'attribute1': '$.my_attribute1',
'attribute2': '$.my_attribute2'
},
'InputTemplate': json.dumps({
'Default': '$',
'my_message': {
'attribute1.$': '$.my_attribute1',
'attribute2.$': '$.my_attribute2',
'content.$': '$'
}
})
},