Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS: Publish SNS message for Lambda function via boto3 (Python2)

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) ) 
like image 291
bmoran Avatar asked Dec 01 '15 20:12

bmoran


People also ask

How do I publish from SNS to Lambda in Python?

On AWS Lambda function, switch to Configuration tab and Permissions section. You will see the Execution Role name. Click on the role name which will redirect developers to the IAM console where the appropriate policy can be attached to allow Lambda execution role to publish notifications to the SNS topic.

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.

Can you invoke a Lambda function using AWS SNS notification?

Amazon SNS and AWS Lambda are integrated so you can invoke Lambda functions with Amazon SNS notifications. When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message.


1 Answers

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' ) 
like image 146
ryantuck Avatar answered Nov 10 '22 17:11

ryantuck