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

后端 未结 3 778
[愿得一人]
[愿得一人] 2020-12-02 09:49

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

3条回答
  •  一向
    一向 (楼主)
    2020-12-02 10:13

    First, you need to grant your Lambda IAM role permissions to publish to your SNS topic using proper IAM policy.

    {
        "Action" : [
            "sns:Publish",
            "sns:Subscribe"
        ],
        "Effect" : "Allow",
        "Resource" : [
            { "Ref" : "" }
        ]
    }
    

    Then you can use following code to SNS publish to your SNS topic from your other Lambda or Node.js code.

    var message = {};
    var sns = new AWS.SNS();
    sns.publish({
        TopicArn: "",
        Message: JSON.stringify(message)
    }, function(err, data) {
        if(err) {
            console.error('error publishing to SNS');
            context.fail(err);
        } else {
            console.info('message published to SNS');
            context.succeed(null, data);
        }
    });
    

提交回复
热议问题