[英]AWS Cloudformation configure API Gateway to point to lambda function version
I am currently trying to enable provisioned concurrency for my AWS lambda function.我目前正在尝试为我的 AWS lambda function 启用预置并发。 I already figured I can only do this on either a Lambda Function Version or a Lambda Function Alias.
我已经想到我只能在 Lambda Function 版本或 Lambda Z86408593C34AF77FDD90DF932F8 上执行此操作But I am having a hard time to point my API Gateway to this version, it seems to always point to the base function, not the version.
但是我很难将我的 API 网关指向这个版本,它似乎总是指向基础 function,而不是版本。
In the UI I can easily attach my Lambda Function Version to an API Gateway Endpoint, but I cannot figure out how to do it in my SAM Template.在 UI 中,我可以轻松地将 Lambda Function 版本附加到 API 网关端点,但我不知道如何在我的 SAM 模板中执行此操作。
This is what I currently have:这是我目前拥有的:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "Desc.",
"Parameters": { },
"Resources": {
"MyLambdaFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Runtime": "dotnetcore3.1",
"CodeUri": "MyCodeUri",
"MemorySize": 1024,
"Timeout": 30,
"Events": {
"HttpEvent1": {
"Type": "Api",
"Properties": {
"Path": "/v1/test",
"Method": "GET",
"RestApiId": {
"Ref": "ApiGateway"
}
}
}
},
"Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
}
},
"MyLambdaFunctionConcurrentV1": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaFunction"
},
"ProvisionedConcurrencyConfig": {
"ProvisionedConcurrentExecutions": 1
}
}
},
"ApiGateway": {
"Type": "AWS::Serverless::Api",
"Properties": {
"StageName": {
"Ref": "ApiStageName"
},
"Cors": {
"AllowCredentials": true,
"AllowHeaders": "'*'",
"AllowMethods": "'*'",
"AllowOrigin": "'*'",
"MaxAge": "'600'"
}
}
}
},
"Outputs": {
"ApiUrl": {
"Description": "API Gateway Endpoint URL",
"Value": {
"Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}-ApiUrl"
}
}
}
}
}
So I am fine to deploy my Lambda function, my API Gateway and my version.所以我可以部署我的 Lambda function、我的 API 网关和我的版本。 But I cannot figure out how to link the API Gateway to my version.
但我不知道如何将 API 网关链接到我的版本。
I just figured out I was looking at the wrong documentation.我只是发现我正在查看错误的文档。 As I have a SAM template and not a Cloudformation template I can use the
AutoPublishAlias
together with the ProvisionedConcurrencyConfig
directly attached to my lambda function.由于我有一个SAM 模板而不是Cloudformation 模板,我可以将
AutoPublishAlias
与直接附加到我的 lambda function 的ProvisionedConcurrencyConfig
一起使用。
Knowing this, the solution was way easier - the version is not necessary as the SAM template version of the AWS::Serverless::Function
supports ProvisionedConcurrencyConfig
directly - as long as AutoPublishAlias
is set as well.知道了这一点,解决方案就简单多了——该版本不是必需的,因为
AWS::Serverless::Function
的 SAM 模板版本直接支持ProvisionedConcurrencyConfig
只要还设置了AutoPublishAlias
。
This is my working template:这是我的工作模板:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "Desc.",
"Parameters": { },
"Resources": {
"MyLambdaFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"AutoPublishAlias": "V1",
"ProvisionedConcurrencyConfig": {
"ProvisionedConcurrentExecutions": 1
},
"Runtime": "dotnetcore3.1",
"CodeUri": "MyCodeUri",
"MemorySize": 1024,
"Timeout": 30,
"Events": {
"HttpEvent1": {
"Type": "Api",
"Properties": {
"Path": "/v1/test",
"Method": "GET",
"RestApiId": {
"Ref": "ApiGateway"
}
}
}
},
"Handler": "MyNamespace::MyNamespace.MyFunc::RunAsync"
}
},
"ApiGateway": {
"Type": "AWS::Serverless::Api",
"Properties": {
"StageName": {
"Ref": "ApiStageName"
},
"Cors": {
"AllowCredentials": true,
"AllowHeaders": "'*'",
"AllowMethods": "'*'",
"AllowOrigin": "'*'",
"MaxAge": "'600'"
}
}
}
},
"Outputs": {
"ApiUrl": {
"Description": "API Gateway Endpoint URL",
"Value": {
"Fn::Sub": "https://${ApiGateway}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${ApiStageName}"
},
"Export": {
"Name": {
"Fn::Sub": "${AWS::StackName}-ApiUrl"
}
}
}
}
}
Side note: I also tried to apply the same AutoPublishAlias
to multiple functions in the same stack - it works, so it does not need to be unique.旁注:我还尝试将相同的
AutoPublishAlias
应用于同一堆栈中的多个函数 - 它有效,因此它不需要是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.