【问题标题】:CloudFormation template not updating after CDK deploymentCDK 部署后 CloudFormation 模板未更新
【发布时间】:2020-09-06 08:52:47
【问题描述】:
我有一个生成的 Swagger json 文件。我有一个读取此文件的 CDK 应用程序,然后我就这样做了
const cfnRestApi = this.api.node.defaultChild as apigateway.CfnRestApi;
cfnRestApi.body = JSON.parse(JSON.stringify(swaggerDefinition));
执行 cdk deploy 后,当我查看 Cloud Formation 模板时,它包含我的 Swagger 信息。但是,当我对 Swagger json 文件进行更改时,即使 cdk deploy 成功运行,CloudFormation 中的模板也不会更新。我该如何解决这个问题?
【问题讨论】:
标签:
swagger
amazon-cloudformation
aws-cdk
【解决方案1】:
我会将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 构造中,您就不能真正指望 promise 的顺序,但是您可以在 bin 端以更可预测的方式做任何您想做的事情。