在源代码管理中跟踪 Terraform 计划输出文件?

Track Terraform Plan Output Files in Source Control?

将地形规划写入文件的动机有点模糊。所以我想问是否还有其他人知道这背后的动机。在我看来,似乎有一个涉及使用这些计划文件的工作流程,但该工作流程一点也不清楚。在 plan 命令的文档末尾有这样的声明: "Future versions of Terraform will make plan files more secure." 很明显,Hashicorp 在使用这些计划文件时发现了 lot 的价值,但为什么呢?

应用预先计算的计划文件是可以预测的,而直接 运行ning terraform apply 则不是。

想象一下你 运行 terraform plan 一切看起来都很好。现在,在您 运行 terraform apply 之前,您的基础架构中发生了一些不可预测的变化,您没有注意到,这可能会导致与您预期不同的结果。

使用 terraform apply <plan-file>planapply 阶段之间的任何意外变化都会被捕获。特别是如果您考虑构建 terraform CI/CD 管道,使用计划文件是为您的自动化添加可预测性的好方法。


更新

如评论中所述,可以在 official documentation:

中找到使用计划文件进行自动化的示例工作流

When running Terraform in automation, the focus is usually on the core plan/apply cycle. The main path, then, is the broadly same as for CLI usage:

  1. Initialize the Terraform working directory.
  2. Produce a plan for changing resources to match the current configuration.
  3. Have a human operator review that plan, to ensure it is acceptable.
  4. Apply the changes described by the plan.

进一步更新

目前,我仍然是 运行ning terraform 0.10.8,但感谢 cm92 and Matt Schuchard 的评论,我意识到以下几点:

最近发布了 terraform 0.11.0changelog 提到了对 terraform apply 行为的有趣修改。默认情况下,它现在将首先生成一个计划并在应用之前等待确认:

The command terraform apply with no explicit plan argument is now interactive by default. Specifically, it will show the generated plan and wait for confirmation before applying it, similar to the existing behavior of terraform destroy. The behavior is unchanged when a plan file argument is provided, and the previous behavior can be obtained without a plan file by using the -auto-approve option

根据贵公司的审查流程,不同的可共享计划文件可能仍然是一个有趣的选择。