在 Terraform 中,如何 运行 目录中的特定 *.tf 文件?

In Terraform, how do I run a specific *.tf file in a directory?

使用 Terraform 0.12.6

我有一个包含多个 *.tf 文件的目录,例如 product1.tfproduct2.tf 等。如何执行 terraform plan 和随后的 terraform apply某个 *.tf 文件?我希望它是 -target 选项,但我阅读了文档,但没有看到这一点。

你不能。 Terraform 将目录中的所有 .tf 文件串联起来并同时处理它们。

您可以使用 -target 定位特定资源,但它不知道它们都在哪个文件中。

-target 通常应该谨慎地用作逃生舱口,如果您需要 运行 一次分离 Terraform 的位,然后将您的 Terraform 代码拆分到单独的目录和状态文件中。

the docs中也有讨论:

This targeting capability is provided for exceptional circumstances, such as recovering from mistakes or working around Terraform limitations. It is not recommended to use -target for routine operations, since this can lead to undetected configuration drift and confusion about how the true state of resources relates to configuration.

Instead of using -target as a means to operate on isolated portions of very large configurations, prefer instead to break large configurations into several smaller configurations that can each be independently applied. Data sources can be used to access information about resources created in other configurations, allowing a complex system architecture to be broken down into more manageable parts that can be updated independently.