amazon-web-servicesaws-cloudformationaws-api-gateway

How can I configure the "Use Stage Variable" parameter for a VPC Link using CloudFormation?


I'm trying to create a configuration for API Gateway using CloudFormation, where I need to define a VPC Link as shown in the following image: example vpc created manually with [Use stage variable]

The configuration I want to achieve is that the VPC Link uses a stage variable, meaning selecting the option [Use stage variable] and providing a custom variable like ${stageVariables.vpclink_example}.

Currently, in my CloudFormation template, I am using the following code snippet to define the resource:

ProxyAnyMethod:
  Type: AWS::ApiGateway::Method
  Properties:
    AuthorizationType: CUSTOM
    HttpMethod: ANY
    ResourceId: !Ref DynamicProxyResource
    RestApiId: !FindInMap [ApiSettings, ApiConfig, RestApiId]
    Integration:
      IntegrationHttpMethod: ANY
      Type: HTTP_PROXY
      Uri: "http://${stageVariables.vpclink_example_nlb}/example/api/{proxy}"
      ConnectionType: VPC_LINK
      ConnectionId: "vpclink_example"

However, I am receiving an error indicating:

ConnectionId should be set to the vpcLinkId or stage variable on connection type VPC_LINK

My question is: How can I configure the [Use stage variable] option directly in a CloudFormation template for a VPC Link in API Gateway? I haven't found specific documentation covering this scenario.

Is there an alternative to achieve this configuration through CloudFormation? If it's not possible, what options do I have to apply this configuration during deployment?

I would greatly appreciate any guidance or solution you can offer.

Direct Parameter Reference in the ConnectionId Field :


Solution

  • To configure the [Use stage variable] option for a VPC Link in API Gateway via CloudFormation, you need to understand that the ConnectionId property in a AWS::ApiGateway::Method resource doesn’t directly accept ${stageVariables.variable_name}. Instead, you can use the StageVariables feature to dynamically reference stage variables at runtime.

    Unfortunately, CloudFormation does not natively support assigning ${stageVariables.vpclink_example} to the ConnectionId property because this field expects a static VPC Link ID during deployment.

    If you have predefined VPC Link IDs for each stage, you can use CloudFormation parameters and mappings to assign the correct VPC Link ID at deployment time. This method doesn’t allow full dynamic behavior but can simplify managing stage-specific configurations. Example:

    Parameters:
      Stage:
        Type: String
        AllowedValues:
          - dev
          - prod
    
    Mappings:
      VpcLinks:
        dev:
          VpcLinkId: vpclink-12345
        prod:
          VpcLinkId: vpclink-67890
    
    Resources:
      ProxyAnyMethod:
        Type: AWS::ApiGateway::Method
        Properties:
          ConnectionType: VPC_LINK
          ConnectionId: !FindInMap [VpcLinks, !Ref Stage, VpcLinkId]
    

    Or you can also configure a Lambda function to be triggered when the Stack get deployed, The Lambda function could be triggered by a custom resource in your stack.