Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Message Attributes to SNS Publish from API Gateway Integration Request

I can find formats for this using the CLI

aws sns publish --topic-arn arn:aws:sns:us-west-2:111111111111:test
    --message "Testing the CLI" 
    --subject "From the CLI" --message-attributes "{\"somename\":
        {\"DataType\":\"String\",\"StringValue\":\"somevalue\"}}"

But what I can't find (or figure out) is how to do this from the Integration Request on an API Gateway.

I believe it needs to be done as Query Parameters of the Integration Request, but the syntax is not the same as adding Message Attributes for SQS. I tested that by using a parameter naming notation along the lines of this example:

MessageBody=This+is+a+test+message
MessageAttribute.1.Name=test_attribute_name_1
MessageAttribute.1.Value.StringValue=test_attribute_value_1
MessageAttribute.1.Value.DataType=String

I also tried:

MessageAttributes   '{"store":{"DataType":"String","StringValue":"example_corp"}}'

enter image description here

So far can't get it working, any help is much appreciated.

like image 929
Patrick Avatar asked Mar 16 '18 01:03

Patrick


People also ask

What are message attributes in SNS?

Amazon SNS supports delivery of message attributes, which let you provide structured metadata items (such as timestamps, geospatial data, signatures, and identifiers) about the message. For SQS subscriptions, a maximum of 10 message attributes can be sent when Raw Message Delivery is enabled.

How do I publish a message to the SNS topic?

Sign in to the Amazon SNS console . In the left navigation pane, choose Topics. On the Topics page, select a topic, and then choose Publish message. The console opens the Publish message to topic page.

Can you publish a message to an SNS topic using an AWS Lambda function backed by Python?

Yes, you could write a Lambda function that publishes to an SNS topic.


1 Answers

After thorough research into the AWS docs, I found that there is no accurate documentation of setting up SNS Publish MessageAttributes in an API Gateway Resource Method as URL Query String Parameters.

Based on the partial syntax example they give here: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html, I was then able to throw things at it until something stuck.

This is the proper dot notation syntax and parameters you need to use:

MessageAttributes.entry.1.Name = "Attribute1"
MessageAttributes.entry.1.Value.DataType = 'String'
MessageAttributes.entry.1.Value.StringValue = 'Test'

Where "Name" and "DataType" are required.

enter image description here

Cheers!

like image 80
Lars Avatar answered Jan 02 '23 21:01

Lars