将特定 AWS API 网关阶段连接到 CloudFormation 模板中的特定 Lambda 别名

Connect specific AWS API Gateway stage to specific Lambda alias in CloudFormation template

我为我的 AWS API 网关和 Lambda 函数创建了 CloudFormation 模板,我需要将特定的 API 网关阶段连接到特定的 Lambda 别名。我有两个别名 - QA 和 Prod,以及两个 API 阶段(也是 QA 和 Prod),在 CloudFormation 模板中它看起来像:

AWSTemplateFormatVersion: "2010-09-09"

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

Description: Lambda function configuration

Resources:
  EndpointLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      FunctionName: "endpoint-lambda"
      Handler: "com.test.aws.RequestHandler::handleRequest"
      Runtime: java8
      Code:
        S3Bucket: "lambda-functions"
        S3Key: "test-endpoint-lambda-0.0.1.jar"
      Description: Test Lambda function
      MemorySize: 256
      Timeout: 60
      Environment:
        Variables:
          ES_HOST: test-es-host-url
          ES_ON: true
          ES_PORT: 443
          ES_PROTOCOL: https
          REDIS_URL: test-redis-host-url

  QaLambdaAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      FunctionName: !Ref EndpointLambda
      FunctionVersion: 1
      Name: "QA"
      Description: "QA alias"

  ProdLambdaAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      FunctionName: !Ref EndpointLambda
      FunctionVersion: 1
      Name: "Prod"
      Description: "Production alias"

  RestApi:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Name: "test-rest-api"
      Description: "Test REST API"

  RestApiResource:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      RestApiId: !Ref "RestApi"
      ParentId: !GetAtt "RestApi.RootResourceId"
      PathPart: "/test"

  RestApiDeployment:
    Type: "AWS::ApiGateway::Deployment"
    Properties:
      RestApiId: !Ref "RestApi"

  QaRestApiStage:
    Type: "AWS::ApiGateway::Stage"
    Properties:
      DeploymentId: !Ref "RestApiDeployment"
      RestApiId: !Ref "RestApi"
      StageName: "qa"

  ProdRestApiStage:
    Type: "AWS::ApiGateway::Stage"
    Properties:
      DeploymentId: !Ref "RestApiDeployment"
      RestApiId: !Ref "RestApi"
      StageName: "prod"

我如何在模板中描述 QA API 阶段应该调用 Lambda 函数的 QA 别名,以及 Prod 阶段 - Prod 别名?

首先了解如何使用 GUI 进行操作。这里有一些关于您想做什么的文档。如果这是您第一次设置它,您还需要添加一些额外的权限,这些权限包含在此处 -

https://docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html

但是为了快速回答您要寻找的是 $:{stageVariables.stage} 它的作用是链接您要触发的 lambda 的别名。在 GUI 中它看起来像这样:

这将允许您的 lambda 触发某个别名。输入后,您将能够在 API 网关中使用测试功能时看到一个新选项。所以在这里你要指定质量检查。

因此,为了在 Cloudformation 中反映这一点,我们需要做类似的事情 -

  RestApi:
      Type: "AWS::ApiGateway::RestApi"
      Properties:
          Name: "test-rest-api"
          Description: "Test REST API"
          paths:
              /ExamplePath:
                put:
                    #Here will go all the configuration setting you want
                    #Such as security, httpMethod, amd passthroughBehavior
                    #But what you need is
                    uri: 'arn:aws:apigateway:${AWS:Region}:lambda:path/2-15/03/31/functions/${LambdaARN}:${!stageVariables.stage}/invocations'

可在此处找到更多相关信息: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html 您希望看到的内容位于页面的最底部。 希望这对您有所帮助!