首页 > 解决方案 > 当我尝试向网关添加新路由时,AWS 云形成总是失败

问题描述

我现在尝试了 7 次,但仍然失败。我正在向我的 API 网关添加一个新的路由端点。我已对云形成文件进行了必要的更改并将其上传到 AWS。

每次尝试,我得到以下错误,我不知道如何解决它。

'comp/*/*' is not a valid method path. Method paths can be defined as
{resource_path}/{http_method} for an individual method override,
or */* for overriding all methods in the stage. 
(Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; 
Request ID: 0878sd34-4555-987s-3332-8dfg7d8f9;
 Proxy: null)

这是云形成文件的一部分。

      DevEnvStage:
        Type: "AWS::ApiGateway::Stage"
        Properties:
          RestApiId: !Ref RestApi
          StageName: 'dev'
          Description: Stage for development environment
          DeploymentId: !Ref FakeStageDeployment
          CacheClusterEnabled: false
          MethodSettings:
            - HttpMethod: "*"
              ResourcePath: "/red/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/yellow/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/blue/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/comp/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/dribl/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
          Variables:
            "": ""
    
      QaEnvStage:
        Type: "AWS::ApiGateway::Stage"
        Properties:
          RestApiId: !Ref RestApi
          StageName: 'qa'
          Description: Stage for development environment
          DeploymentId: !Ref FakeStageDeployment
          CacheClusterEnabled: false
          MethodSettings:
            - HttpMethod: "*"
              ResourcePath: "/black/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/red/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/yellow/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/blue/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/comp/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
          Variables:
            "": ""
    
      StageEnvStage:
        Type: "AWS::ApiGateway::Stage"
        Properties:
          RestApiId: !Ref RestApi
          StageName: 'stg'
          Description: Stage for development environment
          DeploymentId: !Ref FakeStageDeployment
          CacheClusterEnabled: false
          MethodSettings:
            - HttpMethod: "*"
              ResourcePath: "/black/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/red/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/yellow/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/blue/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/comp/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
          Variables:
            "": ""
    
      ProdEnvStage:
        Type: "AWS::ApiGateway::Stage"
        Properties:
          RestApiId: !Ref RestApi
          StageName: 'prod'
          Description: Stage for development environment
          DeploymentId: !Ref FakeStageDeployment
          CacheClusterEnabled: false
          MethodSettings:
            - HttpMethod: "*"
              ResourcePath: "/yellow/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/red/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/red/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/blue/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/comp/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
            - HttpMethod: "*"
              ResourcePath: "/*"
              CachingEnabled: true
              CacheDataEncrypted: false
              CacheTtlInSeconds: 180
          Variables:
            "Hostname": ""
    
      DevEnvGatewayCustomDomain:
        Type: "AWS::ApiGateway::DomainName"
        Properties:
          DomainName: ""
          CertificateArn: ""
          EndpointConfiguration:
            Types:
              - EDGE
    
      DevEnvGatewayBasePathMapping:
        Type: "AWS::ApiGateway::BasePathMapping"
        Properties:
          RestApiId: !Ref RestApi
          DomainName: !Ref DevEnvGatewayCustomDomain
          Stage: !Ref DevEnvStage
    
      QaEnvGatewayCustomDomain:
        Type: "AWS::ApiGateway::DomainName"
        Properties:
          DomainName: ""
          CertificateArn: ""
          EndpointConfiguration:
            Types:
              - EDGE
    
      QaEnvGatewayBasePathMapping:
        Type: "AWS::ApiGateway::BasePathMapping"
        Properties:
          RestApiId: !Ref RestApi
          DomainName: !Ref QaEnvGatewayCustomDomain
          Stage: !Ref QaEnvStage
    
      StageEnvGatewayCustomDomain:
        Type: "AWS::ApiGateway::DomainName"
        Properties:
          DomainName: ""
          CertificateArn: ""
          EndpointConfiguration:
            Types:
              - EDGE
    
      StageEnvGatewayBasePathMapping:
        Type: "AWS::ApiGateway::BasePathMapping"
        Properties:
          RestApiId: !Ref RestApi
          DomainName: !Ref StageEnvGatewayCustomDomain
          Stage: !Ref StageEnvStage
    
      ProdEnvGatewayCustomDomain:
        Type: "AWS::ApiGateway::DomainName"
        Properties:
          DomainName: ""
          CertificateArn: ""
          EndpointConfiguration:
            Types:
              - EDGE
    
      ProdEnvGatewayBasePathMapping:
        Type: "AWS::ApiGateway::BasePathMapping"
        Properties:
          RestApiId: !Ref RestApi
          DomainName: !Ref ProdEnvGatewayCustomDomain
          Stage: !Ref ProdEnvStage

正如您在方法设置部分中看到的那样。comp 的 HttpMehod 是“ /comp/*”不是“ /comp/*/*

标签: amazon-web-servicesamazon-cloudformationamazon-api-gateway

解决方案


您的 cloudformation 模板有问题

根据文档AWS::ApiGateway::Stage MethodSetting

  • Http方法

HTTP 方法。要将设置应用于多个资源和方法,请在 HttpMethod 和 ResourcePath 中指定一个星号 (*)。

  • 资源路径

此方法的资源路径。正斜杠 (/) 编码为 ~1,并且初始斜杠必须包含正斜杠。例如,路径值 /resource/subresource 必须编码为 /~1resource~1subresource。要指定根路径,请仅使用斜杠 (/)。要将设置应用于多个资源和方法,请在 HttpMethod 和 ResourcePath 中指定一个星号 (*)。


推荐阅读