【问题标题】:Unable to invoke lambda function in api gateway method using cloudformation template无法使用 cloudformation 模板在 api 网关方法中调用 lambda 函数
【发布时间】:2021-05-04 19:00:42
【问题描述】:

我正在尝试使用 CloudFormation 模板配置 Lambda 和 API Gateway,并尝试在其中一种方法中调用 Lambda,但遇到类似函数 arn 的问题是无效的。请为此提供一些解决方案

{

"AWSTemplateFormatVersion":"2010-09-09",

"Transform":"AWS::Serverless-2016-10-31",

"Description":"Testing.",

"Parameters":{
   
},
"Conditions":{
   
},
"Resources":{
   "AspNetCoreFunction":{
      "Type":"AWS::Serverless::Function",
      "Properties":{
         "Handler":"<Handler_details>",
         "Runtime":"dotnetcore3.1",
         "CodeUri":"",
         "MemorySize":256,
         "Timeout":30,
         "Role":"$Function_Role",
         "Policies":null,
      }
   },
   "gateway":{
      "Type":"AWS::ApiGateway::RestApi",
      "Properties":{
         "Name":"Test",
         "Description":"Testing",
         "ApiKeySourceType":"HEADER",
         "EndpointConfiguration":{
            "Types":[
               "REGIONAL"
            ]
         }
      }
   },            
 
   "ApiMethod":{
      "Type":"AWS::ApiGateway::Method",
      "Properties":{
         "RestApiId":{
            "Ref":"gateway"
         },
         "HttpMethod":"ANY",
         "AuthorizationType":"NONE",
         "Integration":{
            "IntegrationHttpMethod":"POST",
            "TimeoutInMillis":29000,
            "Type":"AWS_PROXY",
            "Uri":{
               "Fn::Sub":[
                  "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations",
                  {
                     "lambdaArn":{
                        "Fn::GetAtt":[
                           "AspNetCoreFunction",
                           "Arn"
                           
                        ]
                     }
                  }
               ]
            }

         }
      }
   }

}

我尝试使用下面的uri,代表上面代码中提到的uri,但仍然有同样的问题。

"uri": "Fn::Sub":["arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${AspNetCoreFunction.Arn}/invocations"]

【问题讨论】:

  • 错误信息到底是什么?
  • @Marcin。谢谢你的评论。请找出错误。无效的 lambda 函数(服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:9cc5af0e-f6a1-4fe4-bad4-bdb066959928;代理:null)

标签: amazon-web-services aws-lambda amazon-cloudformation aws-api-gateway serverless


【解决方案1】:

我认为错误不是关于 ARN,而是关于 API 网关没有权限调用您的函数。您需要添加如下内容:

{
  "AllowApiInvokations": {
    "Type": "AWS::Lambda::Permission",
    "Properties": {
      "Action": "lambda:InvokeFunction",
      "FunctionName": "<function-name>" ,
      "Principal": "apigateway.amazonaws.com"
    }
  }
}

【讨论】:

  • @Thanks Marcin,但它期望在我使用调用时获得 arn 名称,而不仅仅是 lambda 函数。请根据您的建议找到我使用的代码。 "AllowApiInvokations": { "Type": "AWS::Lambda::Permission", "Properties": { "Action": "lambda:InvokeFunction", "FunctionName": "AspNetCoreFunction.Arn" , "Principal": "apigateway .amazonaws.com" } },
  • 错误:检测到 1 个验证错误:“functionName”处的值“AspNetCoreFunction.Arn”未能满足约束:成员必须满足正则表达式模式:(arn:(aws[a-zA-Z-] *)?:lambda:)?([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\d{1}:)?(\d{12 }:)?(函数:)?([a-zA-Z0-9-]+)(:(\$LATEST|[a-zA-Z0-9-]+))? (服务:AWSLambda;状态代码:400;错误代码:ValidationException;请求 ID:02eb03d7-d249-4d40-bea2-4c75c7720557;代理:null)
  • @kaka 你必须使用 Fn::GetAtt 来获取你在原始模板中所做的 arn。
  • @kaka 进展如何?还是不行?
  • AllowApiInvokations:类型:AWS::Lambda::Permission 属性:操作:lambda:InvokeFunction 函数名称:Fn::GetAtt:AspNetCoreFunction.Arn 主体:apigateway.amazonaws.com
猜你喜欢
  • 1970-01-01
  • 2017-11-13
  • 2020-07-19
  • 2020-03-27
  • 2013-03-07
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
相关资源
最近更新 更多