如何在 AWS CDK 的 API 网关部署中使用现有阶段?
How to use an existing stage in API Gateway deployments in AWS CDK?
我有一个现有的 API 网关,其中包含资源和阶段。我正在通过 aws cdk 向它添加新资源。网关配置为 deploy:false,因此我必须为其手动创建新部署。我可以导入网关,但我在 Stage class 中找不到类似的方法(fromLookup?)。我知道我可以创建一个新的舞台,但它听起来不像是一个可扩展的解决方案。
代码如下:
const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: 'XXX',
rootResourceId: 'YYYY',
});
const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
api,
});
// How to get an existing stage here instead of creating a new one?
const stage = new apigateway.Stage(this, 'test_stage', {
deployment,
stageName: 'dev',
});
api.deploymentStage = stage;
我今天遇到了同样的问题,但我发现如果您为部署资源设置 stageName 属性 它将使用现有阶段。
如果您查看部署资源的 CloudFormation 文档,它有一个 StageName 属性 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html)。
但是如果您检查 CDK 的部署实现,它不支持 stageName 属性 (https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-apigateway/lib/deployment.ts#L71) 并遵循在 Deployment class 上扩展,它从 CfnResource 扩展结束,它在构造函数中期望 stageName 值。
所以我最终通过这样做强制部署资源选择我想要的值:
const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: 'XXX',
rootResourceId: 'YYYY',
});
const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
api,
});
deployment.resource.stageName = 'YourStageName';
对我来说,问题是部署正在更新 API 的资源,而不是舞台的资源。解决方法是每次都创建一个新的部署 ID:
// Create deployment with ID based on current date
const deployment = new apigw.Deployment(this, 'deployment-' + new Date().toISOString(), { api });
// Deploy to existing API & stage
const stage = new apigw.Stage(this, 'stage-alpha', { deployment, stageName: 'alpha' });
api.deploymentStage = stage
使用您发布的代码,您应该会在“阶段”>“部署历史记录”选项卡中看到它不会添加新部署,直到您为其提供唯一 ID。
注意: 这可能并不理想,因为它会在您每次 运行 cdk deploy
时部署更新,即使没有进行其他更改[=12] =]
我有一个现有的 API 网关,其中包含资源和阶段。我正在通过 aws cdk 向它添加新资源。网关配置为 deploy:false,因此我必须为其手动创建新部署。我可以导入网关,但我在 Stage class 中找不到类似的方法(fromLookup?)。我知道我可以创建一个新的舞台,但它听起来不像是一个可扩展的解决方案。
代码如下:
const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: 'XXX',
rootResourceId: 'YYYY',
});
const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
api,
});
// How to get an existing stage here instead of creating a new one?
const stage = new apigateway.Stage(this, 'test_stage', {
deployment,
stageName: 'dev',
});
api.deploymentStage = stage;
我今天遇到了同样的问题,但我发现如果您为部署资源设置 stageName 属性 它将使用现有阶段。
如果您查看部署资源的 CloudFormation 文档,它有一个 StageName 属性 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html)。
但是如果您检查 CDK 的部署实现,它不支持 stageName 属性 (https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-apigateway/lib/deployment.ts#L71) 并遵循在 Deployment class 上扩展,它从 CfnResource 扩展结束,它在构造函数中期望 stageName 值。
所以我最终通过这样做强制部署资源选择我想要的值:
const api = apigateway.RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: 'XXX',
rootResourceId: 'YYYY',
});
const deployment = new apigateway.Deployment(this, 'APIGatewayDeployment', {
api,
});
deployment.resource.stageName = 'YourStageName';
对我来说,问题是部署正在更新 API 的资源,而不是舞台的资源。解决方法是每次都创建一个新的部署 ID:
// Create deployment with ID based on current date
const deployment = new apigw.Deployment(this, 'deployment-' + new Date().toISOString(), { api });
// Deploy to existing API & stage
const stage = new apigw.Stage(this, 'stage-alpha', { deployment, stageName: 'alpha' });
api.deploymentStage = stage
使用您发布的代码,您应该会在“阶段”>“部署历史记录”选项卡中看到它不会添加新部署,直到您为其提供唯一 ID。
注意: 这可能并不理想,因为它会在您每次 运行 cdk deploy
时部署更新,即使没有进行其他更改[=12] =]