Amazon web services AWS CDK部署到后台而不删除以前的后台
我正在使用AWS CDK管理API网关的部署。我使用阶段通过环境(例如开发、测试、预编写、生产)来提升我的代码。我的脚本如下所示:Amazon web services AWS CDK部署到后台而不删除以前的后台,amazon-web-services,amazon-cloudformation,aws-api-gateway,aws-cdk,Amazon Web Services,Amazon Cloudformation,Aws Api Gateway,Aws Cdk,我正在使用AWS CDK管理API网关的部署。我使用阶段通过环境(例如开发、测试、预编写、生产)来提升我的代码。我的脚本如下所示: export class MyStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const api = new apigateway.SpecRest
export class MyStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const api = new apigateway.SpecRestApi(this, 'my-api', {
deploy: false,
apiDefinition: apigateway.ApiDefinition.fromAsset('path/to/swagger.yaml'),
});
const stageName = this.node.tryGetContext('stageName');
const deployment = new apigateway.Deployment(this, `my-api-deployment-${stageName}`, { api });
new apigateway.Stage(this, `my-api-stage-${stageName}`, {
stageName,
deployment,
});
}
}
不幸的是,当我将代码从一个阶段升级到下一个阶段时,例如cdk deploy--context stageName=prepod
,前面的阶段会被删除,因此我的API网关中只有一个阶段
是否可以在不删除其他阶段的情况下部署到某个阶段?我注意到:
- 如果更改第二个参数,即Cloudformation中资源的逻辑名称,它将始终删除并重新创建
- 如果我们需要两个阶段,我们必须有两个不同的物理资源,我们不能只有一个并不断更改名称,它将删除并重新创建它
- 阶段的部署不受阶段控制,而是由实际部署控制。因此,保持stage资源名称为静态,但只有部署是动态的,真正动态的,而不是stageName
export class MyStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const api = new apigateway.SpecRestApi(this, 'my-api', {
deploy: false,
apiDefinition: apigateway.ApiDefinition.fromAsset('path/to/swagger.yaml'),
});
const stageName = this.node.tryGetContext('stageName');
const deployment = new apigateway.Deployment(this, `my-api-deployment-${stageName}`, { api });
new apigateway.Stage(this, `my-api-stage-${stageName}`, {
stageName,
deployment,
});
}
}
以下是完整的代码:
const myRestApi = new apigateway.RestApi(this, "rest-api", {
deploy: false,
});
const methods = myRestApi.root.addMethod(
"ANY",
new apigw.MockIntegration()
);
const StageOneDeploy = this.node.tryGetContext("StageOneDeploy");
const stageOneDeployment = new apigateway.Deployment(
this,
`api-deployment-${StageOneDeploy}`,
{
api: myRestApi,
}
);
stageOneDeployment._addMethodDependency(methods);
new apigateway.Stage(this, `stageOne`, {
stageName: "stageOne",
deployment: stageOneDeployment,
});
const StageTwoDeploy = this.node.tryGetContext("StageTwoDeploy");
const stageTwoDeployment = new apigateway.Deployment(
this,
`api-deployment-${StageTwoDeploy}`,
{
api: myRestApi,
}
);
stageTwoDeployment._addMethodDependency(methods);
new apigateway.Stage(this, `stageTwo`, {
stageName: "stageTwo",
deployment: stageTwoDeployment,
});
要部署,我们可以更改为一个随机数,将部署到哪个阶段
cdk deploy HelloCdkStack --context StageOneDeploy=123456 --context StageTwoDeploy=9493812
当您部署相同的堆栈和部署的
id
时,stageName参数会因不同而发生变化。Cloudformation将删除以前的资源并创建一个新资源
您需要将代码分成两个堆栈,以使其正常工作。
堆栈1将包含创建api并将其导出为的代码
export class MyStack1 extends cdk.Stack {
public readonly api: apigateway.SpecRestApi;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.api = new apigateway.SpecRestApi(this, 'my-api', {
deploy: false,
apiDefinition: apigateway.ApiDefinition.fromAsset('path/to/swagger.yaml'),
});
MyStack2将拥有创建阶段和部署的代码
在bin/App文件中,需要将api从MyStack1传递到MyStack2构造函数
export class MyStack2 extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, api: apigateway.SpecRestApi, props?: cdk.StackProps) {
super(scope, id, props);
const stageName = this.node.tryGetContext('stageName');
const deployment = new apigateway.Deployment(this, `my-api-deployment`, { api });
new apigateway.Stage(this, `my-api-stage`, {
stageName,
deployment,
});
保持MyStack2id
类似于myapi部署堆栈-${stageName}
这将为您提供一个带有api网关的通用堆栈和每个环境的不同堆栈,如dev、test、prepod和prod。好的,那么如果我想保持StageOne与之前分配的部署保持静态,并仅为StageVo创建一个新部署,同时保持这两种状态,该怎么办?