Nicola Ben
Nicola Ben

Reputation: 11327

ApiGateway: Only one base path mapping is allowed if the base path is empty

Creating an API Gateway I get this error:

api-mapping (apimappingXXXXXXX) Only one base path mapping is allowed if the base path is empty. (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException;

where my code is:

 // External API Gateway
 const externalApi = new apigateway.RestApi(this, 'external-api-gw',
 {
   apiKeySourceType: apigateway.ApiKeySourceType.AUTHORIZER,
   restApiName: 'external-api',
   deploy: false,
   endpointConfiguration: {
     ...
   },
   policy: new iam.PolicyDocument({
     statements: [
      ..
         ],
       }),
     ]
   })
 }
);

const domainName = externalApi.addDomainName('domain-name', {
 domainName: props.apigatewayRecordName + '.' + props.hostedZone,
 certificate: existingCertificate,
 endpointType: apigateway.EndpointType.REGIONAL,
});

const myApiGateway = new route53targets.ApiGateway(externalApi);

// deployment
const apiDeployment = new apigateway.Deployment(this, 'deployment', {
  api: externalApi
});

// stage
const apiStage = new apigateway.Stage(this, 'stage', {
  stageName: 'api',
  accessLogDestination: new apigateway.LogGroupLogDestination(logGroup),
  accessLogFormat: apigateway.AccessLogFormat.jsonWithStandardFields(),
  loggingLevel: apigateway.MethodLoggingLevel.INFO,
  dataTraceEnabled: true,
  deployment: apiDeployment
});
externalApi.deploymentStage = apiStage;
domainName.addBasePathMapping(externalApi, { basePath: 'api', stage: apiStage} );

It seems that an empty base path mapping is created automatically and the second one cannot be added. Any suggestions, please?

Upvotes: 1

Views: 2846

Answers (2)

AfroRick
AfroRick

Reputation: 630

Not sure if something changed over the versions, but this does not work with 1.57 of the CDK.

Will generate: "Error: API does not define a default name"

Upvotes: 0

Amit Baranes
Amit Baranes

Reputation: 8122

As mentioned in comments, Below you can find the working code snippet:

Create CfnDomainName:

// import relevant data from ssm
const certificateArn = ssm.StringParameter.valueFromLookup(this, 'CertificateArn');
const domainNameURL = ssm.StringParameter.valueFromLookup(this, 'ApiCustomDomainUrl');
const certificate = cert.Certificate.fromCertificateArn(this, 'Certificate', certificateArn);

// create DomainName
const domainName  = new apigateway.DomainName(this, 'DomainName', {
    domainName: domainNameURL,
    certificate: certificate ,
    endpointType: apigateway.EndpointType.REGIONAL, 
});

Add base path mapping:


// create the api
const api = new apigateway.RestApi(this, id, {
    restApiName: 'API GW ' + id,
    deployOptions: {
        stageName: 'dev',
    },
    endpointTypes: [apigateway.EndpointType.REGIONAL]
});

// add new base path to domain name
new apigateway.BasePathMapping(this, 'my-mapping', {
    domainName: domainName,
    restApi: api,
    basePath: 'my-mapping'
});

// add new base path to domain name
new apigateway.BasePathMapping(this, 'my-mapping-two', {
    domainName: domainName,
    restApi: api,
    basePath: 'my-mapping-two'
});

More about BasePathMapping , DomainName.

Upvotes: 1

Related Questions