【发布时间】:2017-06-08 14:18:10
【问题描述】:
我正在尝试仅使用 CloudFormation 创建一个 Api-Gateway 作为 Lambda 代理。获得 Lambda 函数的正确权限似乎存在问题,即使我已经查看了所有内容并且似乎尝试了所有可能的方法,但我一无所获。围绕一些重要小细节的文档似乎丢失了,(或者我只是误解了它们?)。
这是我所拥有的:
{
"Description": "",
"Parameters": {
"IngressLambdaName": {
"Type": "String",
"Description": "Name of the lambda behind Api Gateway",
"Default": "LambdaIngress"
}
},
"Mappings": {
},
"Resources": {
"ApiGatewayToLambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [ {
"Effect": "Allow",
"Principal": {
"Service": [ "apigateway.amazonaws.com" ]
},
"Action": "sts:AssumeRole"
}]
},
"Policies": [{
"PolicyName": "ApiGatewayToLambdaPolicy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "*"
}]
}
}]
}
},
"IngressLambda":{
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"FunctionName": {"Ref": "IngressLambdaName"},
"Runtime": "nodejs4.3",
"Role": { "Fn::GetAtt": ["**Role that isn't shown here**", "Arn"]},
"Code": {
"ZipFile": { "Fn::Join": ["", [
"exports.handler = function(event, context) {",
" console.log('invoked the lambda!');",
" context.succeed({statusCode: 200, headers: {}, body: JSON.stringify({message: 'invoked the lambda!'})});",
"};"
]]}
}
}
},
"IngressLambdaPermission":{
"Type" : "AWS::Lambda::Permission",
"Properties" : {
"Action" : "lambda:InvokeFunction",
"FunctionName" : { "Ref" : "IngressLambdaName"},
"Principal" : "apigateway.amazonaws.com",
"SourceArn" : {"Fn::Sub": "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/POST/*"}
},
"DependsOn": ["IngressLambda"]
},
"RestApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": "API Gateway"
}
},
"TagModel": {
"Type": "AWS::ApiGateway::Model",
"Properties": {
"ContentType": "application/json",
"Name": "Tag",
"RestApiId": { "Ref": "RestApi" },
"Schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "TagModel",
"type": "object",
"properties": {
"payload": {"type": "object"},
"domain": {"type": "string"}
}
}
}
},
"TagsResource": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"RestApiId": { "Ref": "RestApi" },
"ParentId": { "Fn::GetAtt": ["RestApi", "RootResourceId"] },
"PathPart": "tag"
}
},
"TagsPost": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"ApiKeyRequired": "False",
"AuthorizationType": "NONE",
"HttpMethod": "POST",
"RestApiId": {"Ref": "RestApi"},
"ResourceId": { "Fn::GetAtt": ["RestApi", "RootResourceId"] },
"Integration": {
"Type": "AWS_PROXY",
"IntegrationHttpMethod": "POST",
"PassthroughBehavior": "NEVER",
"Uri": {"Fn::Join" : ["", ["arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/", {"Fn::GetAtt": ["IngressLambda", "Arn"]}, "/invocations"]]}
}
}
},
"RestApiDeployment": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": { "Ref": "RestApi" },
"StageName": "v1"
},
"DependsOn": ["RestApi", "TagModel", "TagsResource", "TagsPost"]
},
},
"Outputs": {
}
}
在 aws Web 门户控制台中的 API Gateway 中运行测试时,我收到错误:Execution failed due to configuration error: Invalid permissions on Lambda function
这快把我逼疯了。这里的任何方向都会很棒。我猜我的权限在某种程度上是错误的,但我不确定如何(这是我与文档斗争的地方)。
【问题讨论】:
-
我在回答中提供了一个完整的工作模板,可以解决您描述的问题。如果您的问题仍未得到解答,请添加更多详细信息(例如,minimal, complete and verifiable example 包含您当前正在尝试但仍有问题的模板的确切源代码)。
标签: amazon-web-services aws-lambda aws-api-gateway amazon-cloudformation