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

后端 未结 2 877
迷失自我
迷失自我 2021-02-01 15:25

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

相关标签:
2条回答
  • 2021-02-01 16:26

    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'
    )
    
    0 讨论(0)
  • 2021-02-01 16:27

    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'
    )
    
    0 讨论(0)
提交回复
热议问题