51

I am trying to publish to an SNS topic which will then notify a Lambda function, as well as an SQS queue. My Lambda function does get called, but the CloudWatch logs state that my "event" object is None. The boto3 docs states to use the kwarg MessageStructure='json' but that throws a ClientError.

Hopefully I've supplied enough information.

Example Code:

import json
import boto3

message = {"foo": "bar"}
client = boto3.client('sns')
response = client.publish(
    TargetArn=arn,
    Message=json.dumps(message)
)
2
  • 1
    You only need the MessageStructure param if you are trying to send different messages to different types of subscribers (e.g. email vs. SMS). Could you include the code for your Lambda function? I'm assuming that the code shown above works without any errors, right? Commented Dec 2, 2015 at 14:00
  • 1
    If you're running this using the Python SDK on say an EC2-Instance don't forget to add a region inside the client e.g., client = boto3.client('sns', region_name='us-east-1') bradmontgomery.net/blog/… Commented May 18, 2018 at 16:35

3 Answers 3

111

you need to add a default key to your message payload, and specify MessageStructure:

import json
import boto3

message = {"foo": "bar"}
client = boto3.client('sns')
response = client.publish(
    TargetArn=arn,
    Message=json.dumps({'default': json.dumps(message)}),
    MessageStructure='json'
)
Sign up to request clarification or add additional context in comments.

7 Comments

Your example doesn't include a default
sure it does - it's in the dict passed to the Message arg.
I don't understand what your message object is in terms of the foo bar stuff. What's a real example? message = {"body": "Hello World!"}?
If you're running this using the Python SDK on say an EC2-Instance don't forget to add a region inside the client e.g., client = boto3.client('sns', region_name='us-east-1') bradmontgomery.net/blog/…
@ryantuck Is the 2nd json.dumps() necessary at all?
|
41

Just in case you want to have different messages for sms and email subscribers:

import json
import boto3

message = {"foo": "bar"}
client = boto3.client('sns')
response = client.publish(
    TargetArn=arn,
    Message=json.dumps({'default': json.dumps(message),
                        'sms': 'here a short version of the message',
                        'email': 'here a longer version of the message'}),
    Subject='a short subject for your message',
    MessageStructure='json'
)

2 Comments

If you're running this using the Python SDK on say an EC2-Instance don't forget to add a region inside the client e.g., client = boto3.client('sns', region_name='us-east-1') bradmontgomery.net/blog/…
help full in my case
5

In case you are publishing your message with a filter policy, you should also use MessageAttributes parameter to add your SNS filter.

To invoke your Lambda with this SNS subscription filter policy {"endpoint": ["distance"]}:

import json
import boto3

message = {"foo": "bar"}
client = boto3.client('sns')
response = client.publish(
    TargetArn=arn,
    Message=json.dumps({'default': json.dumps(message)}),
    MessageStructure='json',
    MessageAttributes={
                        'foo': {
                            'DataType': 'String',
                            'StringValue': 'bar'
                        }
                    },
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.