简体   繁体   English

在template.yaml中创建多个API网关实例,运行'sam local start-api'时如何控制模仿哪个网关?

[英]When creating multiple API Gateway instances in template.yaml, how do you control which Gateway is imitated when running 'sam local start-api'?

I've created a SAM template.yaml file that contains two API Gateway instances - one for production and one for staging.我创建了一个 SAM template.yaml 文件,其中包含两个 API Gateway 实例 - 一个用于生产,一个用于登台。 Each has its own stage called Production and Staging respectively and each of these stages have their own Stage Variables which are specific to the environment.每个阶段都有自己的阶段,分别称为 Production 和 Staging,每个阶段都有自己特定于环境的阶段变量。

The application I'm building locally has been created using the AWS SAM CLI and I've been using the command 'sam local start-api' to run local instances of an API Gateway to test calling endpoints in postman - which has been working fine.我在本地构建的应用程序是使用 AWS SAM CLI 创建的,我一直在使用命令“sam local start-api”来运行 API 网关的本地实例来测试 postman 中的调用端点 - 一直工作正常. Unfortunately, I now need to start testing endpoints that require stage variables and I can't see any way of telling SAM CLI which of the two API Gateway instances in the template file to imitate.不幸的是,我现在需要开始测试需要阶段变量的端点,而且我无法告诉 SAM CLI 模仿模板文件中的两个 API Gateway 实例中的哪一个。 Obviously I don't want it to use Production as that will have data that connects to live services.显然,我不希望它使用 Production,因为它将具有连接到实时服务的数据。

I'm aware that I could have created one API Gateway instance that contains two Stages and so, in the event that there isn't a way to do the above, is there a way to get SAM to use a particular stage within an API Gateway instance instead?我知道我可以创建一个包含两个阶段的 API 网关实例,因此,如果无法执行上述操作,有没有办法让 SAM 使用 API 中的特定阶段网关实例代替? Below is a snippet from my template file.下面是我的模板文件中的一个片段。

ApiProduction:
    Type: AWS::Serverless::Api
    Properties:
      Name: service-layer-production-v1
      StageName: Production
      OpenApiVersion: 3.0.1
      Auth:
        ApiKeyRequired: true
      Variables:
        IS_STAGING: false
        VARIABLE2: value-a
        VARIABLE3: value-a
      Models:
        Error:
          $schema: http://json-schema.org/draft-04/schema#
          title: Error Schema
          type: object
          properties:
            message:
              type: string
        Empty:
          $schema: http://json-schema.org/draft-04/schema#
          title: Empty Schema
          type: object
          properties:
            message:
              type: string

  ApiStaging:
    Type: AWS::Serverless::Api
    Properties:
      Name: service-layer-staging-vnull
      StageName: Staging
      OpenApiVersion: 3.0.1
      Auth:
        ApiKeyRequired: true
      Variables:
        IS_STAGING: true
        VARIABLE2: value-b
        VARIABLE3: value-b
      Models:
        Error:
          $schema: http://json-schema.org/draft-04/schema#
          title: Error Schema
          type: object
          properties:
            message:
              type: string
        Empty:
          $schema: http://json-schema.org/draft-04/schema#
          title: Empty Schema
          type: object
          properties:
            message:
              type: string

You can use cloud formation if condition https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-if to achieve that.如果条件https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-if ,您可以使用云形成来实现。

Here is a quick example:这是一个快速示例:


# Set expected parameter to be passed to sam local
Parameters:
  Stage:
    Type: String
    Default: staging
    Description: Parameter for getting the deployment stage

# Create a condition based on the parameter
Conditions:
  isStagingEnvironment: !Equals
    - Ref: Stage
    - staging

Resources:

  MyFunction:
    Type: "AWS::Serverless::Function"
    Properties:
      Events:
        CatchAll:
          Type: Api
          Properties:
            Method: GET
            Path: /my-sample-function
            # If condition to switch which API to use for this event while invoking the function
            RestApiId: !If
              - isStagingEnvironment
              - !Ref ApiStaging
              - !Ref ApiProduction

Then you can run your sam locally this way:然后你可以通过这种方式在本地运行你的 sam:

sam local start-api --parameter-overrides Stage=staging

The same technique can be used if you have multiple stages per single API Gateway.如果每个 API 网关有多个阶段,则可以使用相同的技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM