在 CloudFormation 模板中指定外部 EDGE 网关 ApiId 的格式是什么?

What is the format to specify an external EDGE Gateway ApiId in CloudFormation templates?

我正在尝试使用以下 CloudFormation 模板创建或更新堆栈:

AWSTemplateFormatVersion: '2010-09-09'
Parameters: 
  ApiGatewayId:
    Type: String
  ApiLayerArn: 
    Type: String
  JarLocation: 
    Type: String
Resources:
  Function:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: net.bitsandpaper.api.kiosk.PlatformChecker
      Runtime: java11
      Code: 
        S3Bucket: bnp-build-artifacts
        S3Key: !Ref JarLocation
      Description: ''
      MemorySize: 128
      Timeout: 5
      Role: arn:aws:iam::479832603967:role/bnp-api-lambda-execution-role
      Layers:
        - !Ref ApiLayerArn
  ApiIntegration:
    Type: AWS::ApiGatewayV2::Integration
    Properties: 
      ApiId: !Ref ApiGatewayId
      IntegrationType: AWS_PROXY
      IntegrationUri:  !Join 
        - ''
        - - 'arn:'
          - !Ref 'AWS::Partition'
          - ':apigateway:'
          - !Ref 'AWS::Region'
          - ':lambda:path/2015-03-31/functions/'
          - !Ref Function
          - /invocations
      TimeoutInMillis: 6000
  ApiRoute:
    Type: AWS::ApiGatewayV2::Route
    Properties:
      ApiId: !Ref ApiGatewayId
      RouteKey: 'GET /kiosk/platform-check'
      Target: !Join
        - /
        - - integrations
          - !Ref ApiIntegration

参数由外部文件正确传递,它们在 Web 控制台中看起来不错,特别是参数 ApiGatewayId 的值为 8548rqrsm5。然而在部署期间,我有一个 CREATE_FAILED 用于 ApiIntegration,消息为:

Invalid API identifier specified 479832603967:8548rqrsm5 (Service: AmazonApiGatewayV2; Status Code: 404; Error Code: NotFoundException; Request ID: 84918a83-cf9d-48d2-acf7-18d9d2e4d330; Proxy: null)

API 是 EDGE Rest API,与 CloudFormation 堆栈位于同一区域。该 ID 由 CLI 使用 aws apigateway get-rest-apis.

检索

我是否遗漏了 ApiId 格式的内容?当不在同一堆栈中引用 API 时,文献非常稀缺...

AWS::ApiGatewayV2::Route 似乎是在 AWS::ApiGatewayV2::Integration 之前创建的。因此,当它尝试引用 ApiIntegration 时,它尚未创建。

所以你应该尝试使用 DependsOn 属性。

With the DependsOn attribute you can specify that the creation of a specific resource follows another. When you add a DependsOn attribute to a resource, that resource is created only after the creation of the resource specified in the DependsOn attribute.

试试下面的 CloudFormation 代码:

ApiRoute:
    Type: AWS::ApiGatewayV2::Route
    DependsOn: ApiIntegration
    Properties:
      ApiId: !Ref ApiGatewayId
      RouteKey: 'GET /kiosk/platform-check'
      Target: !Join
        - /
        - - integrations
          - !Ref ApiIntegration

希望本文能帮助您解决问题。

Link: DependsOn Attribute UserGuide

AWS::ApiGatewayV2 仅适用于 WEBSOCKTES 和 HTTP 类型。来自 docs:

The API protocol. Valid values are WEBSOCKET or HTTP.

但是由于您正在撰写有关 Edge-optimized(不受 HTTP api 支持)的文章,因此您似乎使用的是 REST API,而不是 HTTP API。所以你应该使用 AWS::ApiGateway 资源,而不是 AWS::ApiGatewayV2.