AWS API 网关,与 CDK 的默认基础映射
AWS API Gateway, default base mappings with CDK
我正在使用 AWS CDK 设置环境,但我在使用 API 自定义域的网关和基本映射时遇到了问题。
我得到一个 API 应该有两个阶段:“内部”和“外部”。
每当我创建一个新的 RestApi 并将 domainName 指定为构造函数的道具时,或者之后使用 addDomainName 方法。这将始终创建一个我不想要的默认基础映射。我想像这样添加自己的映射:
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'internal', stage: internalStage });
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'external', stage: externalStage });
如果我执行上述操作,问题是默认映射已经创建,并且由于它将使用空的 basePath 创建,我无法将任何其他映射添加到相同的 API。
我检查了源代码,似乎没有办法在您添加域时传递映射,它们总是自动创建的。
有没有办法更改默认映射,或者以其他方式解决这个问题?
很好的例子:
apiGateway.domainName.basePathMappings[0] = { ... }
我现在的代码:
const apiGateway = new apigw.RestApi(this, 'RestApi', {
deploy: false,
domainName: {
domainName: 'sub.example.com',
certificate,
endpointType: apigw.EndpointType.REGIONAL,
securityPolicy: apigw.SecurityPolicy.TLS_1_2,
},
});
const deployment = new apigw.Deployment(this, 'Deployment', { api: apiGateway });
const internalStage = new apigw.Stage(this, 'InternalStage', {
stageName: 'internal',
deployment,
});
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'internal', stage: internalStage });
const externalStage = new apigw.Stage(this, 'ExternalStage', {
stageName: 'external',
deployment,
});
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'external', stage: externalStage });
当我运行 Synth时生成的语法,将显示3种不同的AWS::ApiGateway::BasePathMapping。
一种用于内部,一种用于外部(正确设置了 basePath),另一种是默认创建的,没有 basePath(我想去掉)。
当我们通过传递给 RestApi 或调用 .addDomainName
添加域名时,cdk 正在添加基本路径映射 /
.
我能够通过将 cfn 资源用于域名和基本路径映射来解决问题。
const cfnInternalDomain = new apigw.CfnDomainName(this, "internal-domain", {
domainName: internalDomainName,
regionalCertificateArn: myCert.certificateArn,
endpointConfiguration: { types: [apigw.EndpointType.REGIONAL] },
});
const intBasePath = new apigw.CfnBasePathMapping(
this,
"internal-base-path",
{
basePath: "intPath",
domainName: cfnInternalDomain.ref,
restApiId: myRestApi.restApiId,
stage: internalStage.stageName,
}
);
这是完整代码。
const myRestApi = new apigw.RestApi(this, "rest-api", {
deploy: false,
});
myRestApi.root.addMethod("ANY", new apigw.MockIntegration());
const deployment = new apigw.Deployment(this, "api-deployment", {
api: myRestApi,
retainDeployments: false,
});
const internalStage = new apigw.Stage(this, "internal-stage", {
stageName: "internal",
deployment,
});
const internalDomainName = "internal.mytest.domain.com";
const cfnInternalDomain = new apigw.CfnDomainName(this, "internal-domain", {
domainName: internalDomainName,
regionalCertificateArn: myCert.certificateArn,
endpointConfiguration: { types: [apigw.EndpointType.REGIONAL] },
});
const intBasePath = new apigw.CfnBasePathMapping(
this,
"internal-base-path",
{
basePath: "intPath",
domainName: cfnInternalDomain.ref,
restApiId: myRestApi.restApiId,
stage: internalStage.stageName,
}
);
我正在使用 AWS CDK 设置环境,但我在使用 API 自定义域的网关和基本映射时遇到了问题。
我得到一个 API 应该有两个阶段:“内部”和“外部”。 每当我创建一个新的 RestApi 并将 domainName 指定为构造函数的道具时,或者之后使用 addDomainName 方法。这将始终创建一个我不想要的默认基础映射。我想像这样添加自己的映射:
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'internal', stage: internalStage });
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'external', stage: externalStage });
如果我执行上述操作,问题是默认映射已经创建,并且由于它将使用空的 basePath 创建,我无法将任何其他映射添加到相同的 API。
我检查了源代码,似乎没有办法在您添加域时传递映射,它们总是自动创建的。
有没有办法更改默认映射,或者以其他方式解决这个问题?
很好的例子:
apiGateway.domainName.basePathMappings[0] = { ... }
我现在的代码:
const apiGateway = new apigw.RestApi(this, 'RestApi', {
deploy: false,
domainName: {
domainName: 'sub.example.com',
certificate,
endpointType: apigw.EndpointType.REGIONAL,
securityPolicy: apigw.SecurityPolicy.TLS_1_2,
},
});
const deployment = new apigw.Deployment(this, 'Deployment', { api: apiGateway });
const internalStage = new apigw.Stage(this, 'InternalStage', {
stageName: 'internal',
deployment,
});
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'internal', stage: internalStage });
const externalStage = new apigw.Stage(this, 'ExternalStage', {
stageName: 'external',
deployment,
});
apiGateway.domainName.addBasePathMapping(apiGateway, { basePath: 'external', stage: externalStage });
当我运行 Synth时生成的语法,将显示3种不同的AWS::ApiGateway::BasePathMapping。 一种用于内部,一种用于外部(正确设置了 basePath),另一种是默认创建的,没有 basePath(我想去掉)。
当我们通过传递给 RestApi 或调用 .addDomainName
添加域名时,cdk 正在添加基本路径映射 /
.
我能够通过将 cfn 资源用于域名和基本路径映射来解决问题。
const cfnInternalDomain = new apigw.CfnDomainName(this, "internal-domain", {
domainName: internalDomainName,
regionalCertificateArn: myCert.certificateArn,
endpointConfiguration: { types: [apigw.EndpointType.REGIONAL] },
});
const intBasePath = new apigw.CfnBasePathMapping(
this,
"internal-base-path",
{
basePath: "intPath",
domainName: cfnInternalDomain.ref,
restApiId: myRestApi.restApiId,
stage: internalStage.stageName,
}
);
这是完整代码。
const myRestApi = new apigw.RestApi(this, "rest-api", {
deploy: false,
});
myRestApi.root.addMethod("ANY", new apigw.MockIntegration());
const deployment = new apigw.Deployment(this, "api-deployment", {
api: myRestApi,
retainDeployments: false,
});
const internalStage = new apigw.Stage(this, "internal-stage", {
stageName: "internal",
deployment,
});
const internalDomainName = "internal.mytest.domain.com";
const cfnInternalDomain = new apigw.CfnDomainName(this, "internal-domain", {
domainName: internalDomainName,
regionalCertificateArn: myCert.certificateArn,
endpointConfiguration: { types: [apigw.EndpointType.REGIONAL] },
});
const intBasePath = new apigw.CfnBasePathMapping(
this,
"internal-base-path",
{
basePath: "intPath",
domainName: cfnInternalDomain.ref,
restApiId: myRestApi.restApiId,
stage: internalStage.stageName,
}
);