[英]What is the purpose of app.synth() in AWS CDK?
What is the purpose of the app.synth()
line in AWS CDK applications? AWS CDK 应用程序中
app.synth()
行的用途是什么? For example it can be seen here on line 29: https://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/ecs/fargate-application-load-balanced-service/index.ts例如,它可以在第 29 行看到: https ://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/ecs/fargate-application-load-balanced-service/index.ts
I have looked at the CDK docs but I cannot find what the actual purpose of that function is.我查看了 CDK 文档,但找不到该函数的实际用途。 As I understand it, the app is automatically synthesized upon
cdk deploy
or cdk synth
.据我了解,该应用程序是在
cdk deploy
或cdk synth
时自动合成的。 I also took the example shown in repo linked above, commented out the app.synth()
line, deployed it, and it seemed to work as expected.我还采用了上面链接的 repo 中显示的示例,注释掉了
app.synth()
行,部署了它,它似乎按预期工作。 Therefore I'm wondering if I'm missing something when it comes to the purpose of app.synth()因此,我想知道在 app.synth() 的目的方面我是否遗漏了什么
cdk synth only constructs your CloudFormation template . cdk synth仅构建您的 CloudFormation 模板。 It does not deploy (create actual resources) it to AWS.
它不会将其部署(创建实际资源)到 AWS。 You can take the template constructed, deploy it manually in CFN console, edit or inspect.
您可以使用构建的模板,在 CFN 控制台中手动部署,编辑或检查。
cdk deploy is going to construct the template and deploy it to AWS as well. cdk deploy将构建模板并将其部署到 AWS 。
People use synth
with deploy
because it is a good practice :人们将
synth
与deploy
一起deploy
因为这是一个很好的做法:
It is optional (though good practice ) to synthesize before deploying.
在部署之前进行综合是可选的(尽管很好的做法)。 The AWS CDK synthesizes your stack before each deployment.
AWS CDK 在每次部署之前综合您的堆栈。
I know it's been a while since this question was asked, but I was curious and looked into it myself, so I'll share it with you.我知道这个问题被问到已经有一段时间了,但是我很好奇并自己研究了它,所以我会与你分享。
The App
class has an option called autoSynth
which is set to true
by default, so even if you omit app.synth()
in your code, it will be executed automatically. App
类有一个名为autoSynth
的选项,默认设置为true
,因此即使您在代码中省略app.synth()
,它也会自动执行。 https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.AppProps.html#autosynth https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.AppProps.html#autosynth
As mentioned in the documentation, this feature may not work in some languages, so it seems to be explicitly specified in many sample codes.如文档中所述,此功能可能不适用于某些语言,因此似乎在许多示例代码中都明确指定了它。
According to the documentation, it is OK to run app.synth()
multiple times.根据文档,多次运行
app.synth()
是可以的。 https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html#synthoptions https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html#synthoptions
autoSynth
works fine in Node.js. autoSynth
在 Node.js 中运行良好。 It is implemented in Node.js using process.once("beforeExit", listener)
.它是在 Node.js 中使用
process.once("beforeExit", listener)
实现的。 https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/core/lib/app.ts#L127-L132 https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/core/lib/app.ts#L127-L132
I started this investigation because when I ran CDK in Deno, I had to explicitly run app.synth()
to generate the CDK assembly.我开始这项调查是因为当我在 Deno 中运行 CDK 时,我必须显式运行
app.synth()
来生成 CDK 程序集。 As a result of this investigation, I found out that this is because the Node compatibility layer in Deno does not currently support "beforeExit" events.通过这次调查,我发现这是因为 Deno 中的 Node 兼容层目前不支持“beforeExit”事件。 https://github.com/denoland/deno_std/blob/113435ada1948b90188586f022d55745c6d2d19b/node/process.ts#L51-L52
https://github.com/denoland/deno_std/blob/113435ada1948b90188586f022d55745c6d2d19b/node/process.ts#L51-L52
As per this experience, it is better to explicitly call app.synth()
as recommended.根据此经验,最好按照建议显式调用
app.synth()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.