首页 > 解决方案 > CDK 部署后 CloudFormation 模板未更新

问题描述

我有一个生成的 Swagger json 文件。我有一个读取此文件的 CDK 应用程序,然后执行

const cfnRestApi = this.api.node.defaultChild as apigateway.CfnRestApi;

cfnRestApi.body = JSON.parse(JSON.stringify(swaggerDefinition));

在我查看 Cloud Formation 模板时执行 cdk deploy 后,它包含我的 Swagger 信息。但是,当我对 Swagger json 文件进行更改时,即使 cdk deploy 成功运行,CloudFormation 中的模板也不会更新。我该如何解决这个问题?

标签: swaggeramazon-cloudformationaws-cdk

解决方案


我会将swaggerDefinition文件的读取移动bin/myapp.ts到您的堆栈接口并添加一个额外的参数。像这样的东西:

// ===>>> this is in the bin/myapp.ts
const swaggerDefinition = // read swagger file

new MyAppStack(app, 'MyAppStack', {
  swaggerDefinition,
});


// ===>>> now your lib/myapp.ts
export interface MyAppStackProps extends cdk.StackProps {
  swaggerDefinition: string; // or whatever type you think it needs
}

export class MyAppStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props: MyAppStackProps) {
    super(scope, id, props);
    ...
    // now you can use it freely in cdk code

其背后的原因是,一旦您处于 cdk 构造中,您就不能真正指望承诺的顺序,但是您可以在 bin 端以更可预测的方式做任何您想做的事情。


推荐阅读