AWS Lambda &SNS:跨区域调用Lambda

AWS Lambda &SNS:跨区域调用Lambda

问题描述:

我在多个区域部署了一个 Lambda 函数.我想向将调用这些函数的 SNS 发布一条消息.

I have a Lambda function deployed to several regions. I would like to publish a message to SNS that will invoke these functions.

我使用 aws-cli 创建了主题,授予 Lambda 与 SNS 对话的权限,并创建订阅.订阅似乎已成功创建,我可以在 AWS 控制台中看到它.但是,它不起作用.不会调用 lambda 函数.

Using aws-cli I've created the topics, given Lambda permission to talk to SNS, and create the subscriptions. The subscription appears to be created successfully, and I can see it in the AWS console. But, it doesn't work. The lambda function does not get invoked.

这是基于 CloudFormation 的示例.您必须将 SNS 的调用权限添加到 Lambda 函数:

This is CloudFormation based example. You have to add invoke permission for SNS to the Lambda functions:

{
    "Type" : "AWS::Lambda::Permission",
    "Properties" : {
        "FunctionName" : { "Fn::GetAtt" : [ "YourLambda", "Arn" ] },
        "Action" : "lambda:InvokeFunction",
        "Principal" : "sns.amazonaws.com",
        "SourceArn" : { "Ref" : "YourSNSTopicArn" }
    }
}

然后您需要将您的 Lambdas 订阅到您的 SNS 主题.这可以通过 API 调用或通过 CloudFormation 来完成.

Then you need to subscribe your Lambdas to your SNS topic. This can be done via API call or through CloudFormation.

{
    "Type" : "AWS::SNS::Topic",
    "Properties" : {
        "TopicName" : "YourTopicName",
        "Subscription" : [ {
            "Endpoint" : { "Fn::GetAtt" : [ "YourLambda", "Arn" ] },
            "Protocol": "lambda"
        } ]
    }
}

如果您遗漏了其中任何一项,您的 Lambdas 将不会被调用.以上信息来源为官方博客文章InvokingLambda 通过 SNS 运行.

If you're missing any of this, your Lambdas won't invoke. Source for the above information is the official blog article Invoking Lambda functions via SNS.