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.以上信息的来源是官方博客文章调用Lambda通过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.