代码之家  ›  专栏  ›  技术社区  ›  Hleb

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

  •  1
  • Hleb  · 技术社区  · 6 年前

    我为我的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-stage-prod别名?

    1 回复  |  直到 6 年前
        1
  •  3
  •   JohnB    6 年前

    首先要了解如何使用图形用户界面。这里有一些关于你想做什么的文档。如果这是您第一次设置这个,您还需要添加一些额外的权限,包括在这里-

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

    但要快速回答问题,您需要查找的是$:StageVariables.Stage它所做的是链接要触发的lambda的别名。在GUI中,它看起来像这样: enter image description here

    这将允许lambda触发某个别名。一旦输入了这个选项,当在API网关中使用测试功能时,您将能够看到一个新的选项。所以在这里您将指定QA。 Enter the lambda Alias here

    所以,为了在云形成中反映这一点,我们需要做一些类似的事情-

      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 你想看到的是在页面的最底部。 希望这有帮助!