1

当您在创建 RestApi 时创建 CustomDomain 或在 CDK 中创建 RestApi 后添加 .addDomainName 时,将为指定阶段创建到域根目录的默认基本路径映射。

我不希望这样被创建。我想创建自己的基本路径映射。当我通过 domain.addBasePathMapping() 添加一个时,我最终得到了到根的映射和到指定基本路径的映射。像这样:

  • api: example.com / stage: dev / path: (none) // 不想要这个。
  • api:example.com/stage:dev/path:the-base-path //想要这个。

有没有办法改变默认的基本路径映射或阻止它被创建?

代码重现了这个问题:

const apiSpec = <openapi spec loaded here>
const zone = route53.HostedZone.fromLookup(this, 'theZone', {
    domainName: 'example.com'
  });
  //Get the existing certificate
  const acmCertificate = acm.Certificate.fromCertificateArn(this, 'Certificate', CERTIFICATE_ARN);

  const apiDomainName = 'example.com';
  const theApi = new apigateway.SpecRestApi(this, `the-example-api`, {
    deploy: true,
    restApiName: 'ApiNameHere',
    deployOptions: {
       stageName: 'dev',
    },
    endpointTypes: [ apigateway.EndpointType.REGIONAL ],
    apiDefinition: apigateway.ApiDefinition.fromInline(apiSpec),
    endpointExportName: `endpointExportName`,
    domainName: {
      domainName: apiDomainName,
      certificate: acmCertificate,
      securityPolicy: apigateway.SecurityPolicy.TLS_1_2
    }
  });
  const domain = theApi.domainName
  domain.addBasePathMapping(theApi, {basePath: 'the-base-path', stage: theApi.deploymentStage});

  //Create alias record to route to apis
  const aRecord = new route53.ARecord(this, 'alias-record', {
    recordName: apiDomainName,
    zone, 
    target: route53.RecordTarget.fromAlias(new targets.ApiGateway(theApi))
  });
4

1 回答 1

0

addDomainName方法BaseRestApi始终使用 restApi 对象作为映射参数,如下所示:https ://github.com/aws/aws-cdk/blob/b3a7d5ba67bec09e422c0c843d7dee4653fe9aec/packages/%40aws-cdk/aws-apigateway/lib/restapi.ts #L346-L355

如果您不想这样,请不要domainName在创建 api 时提供参数,而是像这样单独创建它:

const domain = new apigateway.DomainName(theApi, 'CustomDomain', {
    domainName: apiDomainName,
    certificate: acmCertificate,
    securityPolicy: apigateway.SecurityPolicy.TLS_1_2
});
domain.addBasePathMapping(theApi, {basePath: 'the-base-path', stage: theApi.deploymentStage});
于 2021-06-21T16:35:58.293 回答