amazon-web-services - 无法使用 cloudformation 模板在 api 网关方法中调用 lambda 函数
问题描述
我正在尝试使用 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"]
解决方案
我认为错误与 ARN 无关,而是与 API 网关无权调用您的函数有关。您需要添加如下内容:
{
"AllowApiInvokations": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": "<function-name>" ,
"Principal": "apigateway.amazonaws.com"
}
}
}
推荐阅读
- javascript - 使用纯 Javascript 的动态下拉菜单
- android - Android TextView, lineSpacingExtra + backgroundColor 问题
- nlp - 使用 spacy 3.0 进行预训练时出错。raw_text 到底是什么?
- excel-formula - Excel 中的月份短名称和区域格式
- angular - 在 Angular 中上传/导入 excel 工作表时,日期以数字格式显示
- python - 从另一个 Azure 函数调用 Azure 函数
- c# - 将 WebAPI 资源模型映射到 Web UI 模型
- websphere - 如何在没有实体的情况下使用 IBM Watson 上下文变量?
- google-cloud-platform - GKE 工作负载身份池 VS 来自工作负载身份联合的工作负载身份池
- locale - Keycloak:未为/登录后设置浏览器的区域设置