简体   繁体   English

AWS Cloudformation配置API网关指向lambda function版本

[英]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.

相关问题 如何将AWS API网关阶段指向特定的lambda函数别名? - How to point AWS API gateway stage to specific lambda function alias? 如何使用aws中的cloudformation在api网关中请求参数并将其传递给lambda函数? - How can I request parameters in api gateway using cloudformation in aws and pass it down to lambda function? AWS Aurora 无服务器 mysql、Cognito、Lambda、API 网关、CloudFormation 启动器 - AWS Aurora serverless mysql, Cognito, Lambda, API gateway, CloudFormation starter AWS API 网关 - Lambda 代理未通过 CloudFormation 模板打开 - AWS API Gateway - Lambda Proxy not turning on via CloudFormation template 将现有的 AWS Lambda 和 API Gateway 导出到 Cloudformation 模板 - Export existing AWS Lambda and API Gateway to Cloudformation template 在 aws api 网关 cloudformation 中提供 vpc 端点 - providing vpc end point in aws api gateway cloudformation C#如何配置AWS API Gateway参数以映射到基本的AWS Lambda函数? - C# How do i configure AWS API Gateway parameters to map to a basic AWS Lambda Function? AWS CloudFormation中的Lambda函数 - Lambda Function in AWS CloudFormation 无法使用 cloudformation 模板在 api 网关方法中调用 lambda function - Unable to invoke lambda function in api gateway method using cloudformation template 如何使用 Cloudformation 将 lambda 函数附加到现有 API 网关 - How to use Cloudformation to attach lambda function to existing API gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM