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 :
An attempt was made to pass a value such as "${stageVariables.vpclink_example} ” in the ConnectionId field to simulate the use of a stage variable, attempting to have CloudFormation interpret it as a dynamic reference.
This approach also produced errors, since CloudFormation expects ConnectionId to be the ID of an explicit VPC Link and not a runtime variable. Testing of Parameters Defined in Template:
A parameter was defined in the CloudFormation template that allowed to receive the value of the VPC Link as a string and then use it in the ConnectionId.
Despite receiving the value during execution, CloudFormation was unable to automatically set the [Use stage variable] option in the API Gateway console. This caused the VPC Link value to be fixed and not dependent on the stage context.
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.