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

Viewed 112

I'm trying to create or update a stack with the following CloudFormation Template:

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

The parameters are correctly passed by an external file, they look good in the Web Console, notably parameter ApiGatewayId has value 8548rqrsm5. Yet during deployment I have a CREATE_FAILED for ApiIntegration, with the message:

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

The API is an EDGE Rest API, in the same region than the CloudFormation stack. The ID is retrieved by the CLI with aws apigateway get-rest-apis.

Am I missing something in the ApiId format? The litterature is very scarce when not referencing an API in the same stack...

2 Answers

AWS::ApiGatewayV2 is only for WEBSOCKTES and HTTP types. From docs:

The API protocol. Valid values are WEBSOCKET or HTTP.

But since you are writing about Edge-optimized (not supported by HTTP api) it seems to that you are using REST API, rather then HTTP API. So you should be using AWS::ApiGateway resources, not AWS::ApiGatewayV2.

It seem's like the AWS::ApiGatewayV2::Route is created before the AWS::ApiGatewayV2::Integration. So When it trying to refer ApiIntegration it is not yet created.

So you should try to use DependsOn attribute.

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.

Try this below CloudFormation code:

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

I hope this will help you out to resolve your problem.

Link: DependsOn Attribute UserGuide

Related