Search code examples
amazon-web-servicesaws-api-gateway

AWS CDK BasePathMapping already exists error


When trying to deploy a CDK stack with a REST API and an apigateway.BasePathMapping, I get the following error:

<my_domain>|(none) already exists in stack <my_stack_arn>

I tried deleting the mapping both with the CLI using the delete-base-path-mapping command and from the user interface but the problem remains.

Deleting the entire stack and re-creating it does not solve the issue either.


Solution

  • I'm using AWS CDK and was facing the same issue until I used different base paths for different APIs. Make sure your basePath is different for each stage/service.

    const { RestApi, EndpointType, Cors, DomainName, BasePathMapping } = require('aws-cdk-lib/aws-apigateway');
    
    const my_apig = new RestApi(this, 'my_apig', {
          deployOptions: {
            stageName: props.env.stage
          },
          endpointConfiguration: {
            types: [EndpointType.REGIONAL]
          },
          defaultCorsPreflightOptions: {
            allowOrigins: Cors.ALL_ORIGINS,
            allowMethods: Cors.ALL_METHODS,
            allowHeaders: Cors.DEFAULT_HEADERS,
            allowCredentials: true
          }
    });
    
    const myCustomDomain = DomainName.fromDomainNameAttributes(this, 'my-domain', {
          domainName: 'example.com'
    });
    
    new BasePathMapping(this, 'base-path-mapping', {
          domainName: myCustomDomain,
          restApi: my_apig,
          basePath: props.env.serviceName    // set this using a dynamic parameter for different stage/service
    });