(aws-cdk/api-gateway): No way to create a BasePathMapping that does not point to a stage
According to https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-basepathmapping.html it is possible to create mappings that do not point to any stage and point to REST API only. Current BasePathMapping construct does not allow this (see code snippet below).
Use Case
I have a production REST API that was created some time ago using CDK 1.45.0. In that version default base path mapping was created without stage specified, so all API users specify stage name as a part of the request URL. When upgrading to v1.115 I could not find a way to keep mapping the same using CDK, had to clone the construct into my project and patch it.
Proposed Solution
No good ideas. Maybe new static methods:
BasePathMapping.fromRestApi(same logic as the current constructor)BasePathMapping.fromRestApiAndStage(allow to passundefinedvalue forstageinto CFN) methods?
Other
/**
* This resource creates a base path that clients who call your API must use in
* the invocation URL.
*
* Unless you're importing a domain with `DomainName.fromDomainNameAttributes()`,
* you can use `DomainName.addBasePathMapping()` to define mappings.
*/
export class BasePathMapping extends Resource {
constructor(scope: Construct, id: string, props: BasePathMappingProps) {
super(scope, id);
if (props.basePath && !Token.isUnresolved(props.basePath)) {
if (!props.basePath.match(/^[a-zA-Z0-9$_.+!*'()-]+$/)) {
throw new Error(`A base path may only contain letters, numbers, and one of "$-_.+!*'()", received: ${props.basePath}`);
}
}
// if restApi is an owned API and it has a deployment stage, map all requests
// to that stage. otherwise, the stage will have to be specified in the URL.
const stage = props.stage ?? (props.restApi instanceof RestApiBase
? props.restApi.deploymentStage
: undefined);
new CfnBasePathMapping(this, 'Resource', {
basePath: props.basePath,
domainName: props.domainName.domainName,
restApiId: props.restApi.restApiId,
stage: stage && stage.stageName,
});
}
}
This is a :rocket: Feature Request
We also ran into this issue. We're working around it by pulling the child CfnBasePathMapping and setting the stage to undefined.
The constructor for the BasePathMapping construct does not require the stage property.
Have you tried using that?
Hi, Niranjan.
Unfortunately, if stage is not specified then BasePathMapping construct selects props.restApi.deploymentStage value - see constructor code I specified in my previous message. No way to propagate null/undefined into resulting CF template :(
Andrey Lipatkin.
Thanks Andrey. Adding this to our backlog.
Unfortunately, we're unable to work on this right now. We use +1s and community feedback to evaluate its prioerity. We accept PRs if you're interested in contributing.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.