How do I add SQS message attributes to an SNS subscription?

The documentation for AWS SNS and SQS has sections on message attributes. But there is no explanation how to have SQS message attributes when this queue subscribes to an SNS topic.

Is there a way to configure AWS SNS to add specific message attributes to SQS messages sent by subscription?

+6


source to share


3 answers


Whereas using Amazon SNS message attributes sending Amazon SNS message attributes to Amazon SQS, it seems like the attributes are sent in the body of the message instead of being attached as message attributes as a result of Amazon SQS messages.

For example, I did the following:

  • Created an SNA theme for Amazon
  • Created another Aazon SQS queue and subscribed to the SNS topic
  • SNS post published

I posted via AWS CLI :

aws sns publish --topic-arn arn:aws:sns:ap-southeast-2:123456789012:foo --message msg --subject subj --message-attributes '{"somename" : { "DataType":"String", "StringValue":"somevalue"}}'

      



(i got syntax help from the map datatype in aws cli )

The received message in SQS showed the attributes as part of the message :

{
  "Type" : "Notification",
  "MessageId" : "53e3adad-723a-5eae-a7b7-fc0468ec2d37",
  "TopicArn" : "arn:aws:sns:ap-southeast-2:123456789012:foo",
  "Subject" : "subj",
  "Message" : "msg",
  "Timestamp" : "2017-05-29T12:48:22.186Z",
  ...
  "MessageAttributes" : {
    "somename" : {"Type":"String","Value":"somevalue"}
  }
}

      

It would be much better if these attributes were attached to the SQS post as official SQS attributes. Alas, this does not seem to be the case.

+9


source


From aws documentation:

To use message attributes with Amazon SQS endpoints, you must set the Raw Message Delivery subscription attribute to True. For more information on delivering raw messages, see the appendix: Large Payload and Delivery of Raw Messages. https://docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html https://docs.aws.amazon.com/sns/latest/dg/large-payload-raw-message.html

Added example from a real project. Hope this helps clarify the situation. The message posted in the sns thread is the following:

aws sns publish --topic-arn arn:aws:sns:us-west-2:xxx:pollution-event --message '{"operatorId":3375001,"eventTypeId":1,"eventLevelId":1,"validFrom":"2018-03-10T09:00:00Z","validTo":"2018-03-11T09:00:00Z"}'  --message-attributes '{"Type" : { "DataType":"String", "StringValue":"Orchestration.Services.Model.Pollution.PollutionMessage"}}'

      



Enable raw delivery is false (default) . The message received by sqs contains only content, no attributes

{
  "Type": "Notification",
  "MessageId": "78d5bc6f-142c-5060-a75c-ef29b774ec66",
  "TopicArn": "arn:aws:sns:eu-west-2:xxx:pollution-event",
  "Message": "{\"validFrom\": \"2018-03-10T09:00:00Z\",\"validTo\": \"2018-03-11T09:00:00Z\",\"eventLevelId\": 1,\"eventTypeId\": 1,\"operatorId\": 3375001}",
  "Timestamp": "2018-04-17T11:33:44.770Z",
  "SignatureVersion": "1",
  "Signature": "xxx==",
  "SigningCertURL": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-xxx.pem",
  "UnsubscribeURL": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:xxx",
  "MessageAttributes": {
    "Type": {
      "Type": "String",
      "Value": "Orchestration.Services.Model.Pollution.PollutionMessage"
    },
    "AWS.SNS.MOBILE.MPNS.Type": {
      "Type": "String",
      "Value": "token"
    },
    "AWS.SNS.MOBILE.MPNS.NotificationClass": {
      "Type": "String",
      "Value": "realtime"
    },
    "AWS.SNS.MOBILE.WNS.Type": {
      "Type": "String",
      "Value": "wns/badge"
    }
  }
}

      

There are no message attributes, it is contained within the message itself

Enable Raw delivery is true. The message contains message attributes and related content Message has an attribute The attribute contains expected value

+9


source


Enabled raw message delivery type when adding SQS subscription for topic inside SNS

0


source







All Articles