Skip to content

Push message to SNS with MessageAttributes

0

Hi,

I am trying to push a message to a SNS with MessageAttributes and getting Parameter validation failed: Invalid type for parameter MessageAttributes, value: S3_Bucket={StringValue=test-s3,DataType=String}, type: <class 'str'>, valid types: <class 'dict'>'

MessageAttributes={"S3_Bucket": {"StringValue":"test-s3","DataType": "String"}}

Could you please help me on this issue

asked 2 years ago1K views
2 Answers
5

To successfully push a message to Amazon SNS with MessageAttributes, you need to ensure that the MessageAttributes parameter is correctly structured as a dictionary. Each attribute must be a dictionary containing StringValue and DataType.

import boto3

# Create an SNS client
sns_client = boto3.client('sns')

# Define the message
message = 'This is a test message'

# Define the MessageAttributes
message_attributes = {
    'S3_Bucket': {
        'StringValue': 'test-s3',
        'DataType': 'String'
    }
}

# Publish the message to the SNS topic
response = sns_client.publish(
    TopicArn='arn:aws:sns:region:account-id:topic-name',
    Message=message,
    MessageAttributes=message_attributes
)

print(response)

EXPERT
answered 2 years ago
  • Thank you very much Thanniru, It's awesome. It is working for the same that you shared.

    But I am not getting any response when I pass the bucket name from a variables

    s3_bucket_name="test-dummy" message_attributes = "{'S3_Bucket': {'StringValue':'" + s3_bucket_name+ "','DataType': 'String'}}"

0

I tried as below and its working now, Thank you very much Thanniru Anil Kumar !

s3_bucket_name='test-s3' message_attributes = { 'S3_Bucket': { 'StringValue': s3_bucket_name, 'DataType': 'String' } }

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.