1

I try to use "signal" in Django to send SNS email in AWS and my code is:

import boto3
from properties.models import PropertyList
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

@receiver(post_save, sender=PropertyList)
def send_property_details(sender, instance, created, **kwargs):
    if created:
        sns = boto3.client('sns')

        response = sns.publish(
            TopicArn='',# I write value of TopicArn
            Message={
            "name": instance.title,
            "description": instance.description
                },
            MessageStructure='json',
            Subject='New Property Created',
            MessageAttributes={
                'default':{
                    'DataType':'String',
                    'StringValue':instance.title
                    }
                },
            )

        print(response['MessageId'])

I get error as:

Parameter validation failed: Invalid type for parameter Message, value: {'name': 'aws', 'description': 'test'}, type: , valid types:

In AWS docs said If I want to send different messages for each transport protocol, set the value of the MessageStructure parameter to JSON and use a JSON object for the Message parameter. What is wrong in my code?

Note: I want to send more than values so I need to send JSON

gp_sflover
  • 3,460
  • 5
  • 38
  • 48

1 Answers1

5

In the example you paste the Message is a dictionary. That may be the reason for the error. Try to modify Message as follows:

import boto3, json    
...

mesg = json.dumps({
    "default": "defaultfield", # added this 
    "name": instance.title,
    "description": instance.description
})
response = sns.publish(
    TopicArn='topicARNvalue',
    Message=mesg,
    MessageStructure='json',
    Subject='New Property Created',
    MessageAttributes={}
)
Evhz
  • 8,852
  • 9
  • 51
  • 69
  • I get this error An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: Message Structure - No default entry in JSON message body – Ahmed El-Mahdy May 11 '18 at 12:21
  • Following the error, I update the post, it seems that the json object needs a `'default'`field. Have also a look at [this](https://github.com/aws/aws-sdk-js/issues/262). – Evhz May 11 '18 at 12:49
  • when I do that all I get in email , this defaultfield, value of default, when I make messageStructure = String I get full msg but as json "{"name":"aws", "description":"test"}" like this – Ahmed El-Mahdy May 11 '18 at 12:53