如何将 terraform 文件(main.tf)拆分为多个文件(无模块)? [英] How to split a terraform file (main.tf) in several files (No Modules)?
问题描述
我有一个通过 Terraform 部署的 GCP 基础架构:存储桶、服务帐户、计算引擎、VPC、云 SQL、BigTable、BigQuery、Composer 等.
I have a GCP infrastructure deployed through Terraform: buckets, service accounts, Compute Engines, VPC, cloud SQL, BigTable, BigQuery, Composer, etc.
Terraform v0.11.10提供者谷歌"(2.15.0)
Terraform v0.11.10 Provider "google" (2.15.0)
最近客户要求我将我们唯一的一个 terraform 文件(例如 main.tf)拆分为多个文件.例如:一个文件用于存储桶,另一个用于服务帐户,另一个用于数据库服务等.
Recently the client asked me to split our only one terraform file (e.g. main.tf) in several files. E.g: One files for Buckets, other for Service Accounts, other for Database Services, etc.
我在 GCP 存储桶中只有一个 terraform 状态文件.
I only have one terraform state file located in a GCP bucket.
我怎样才能以最小的影响做到这一点?那么 terraform 状态呢?(还会有多个状态文件吗?还是只保留一个TF文件的想法,即使我们将代码分开?)
How could I do it with the lowest impact ? What about with the terraform state ? (Will there also be also multiples state files ? Or Is the idea to keep only one TF file, even if we split the code out ?)
注意:这与 Terraform 模块无关,只是将单个 terraform 文件 (.tf) 分成多个文件 (.tf)
NOTE: This has nothing to do with Terraform modules, it's just about dividing a single terraform file (.tf) into several files (.tf)
提前致谢!
推荐答案
命令terraform apply
可以应用于文件或目录,根据文档.因此,将所有文件放在一个目录中并应用它们.
The command terraform apply
can be apply to a file or a directory, according with the documentation. Thus, put all your file in a directory and apply them.
tfstate 文件与部署相关.你将只有一个.
The tfstate file is relative to a deployment. You will have only one.
关于最低影响...很难说.一切都取决于您的变量和资源依赖项.如果您必须搜索多个文件以了解资源的创建方式或变量的设置位置,那么多个文件并不是一个好主意......但是,这是您的客户要求,并且为了可读性而做起来要简单得多.
About the lowest impact... Hard to say. All depends of your variables and your resource dependencies. If you have to search is several file to understand how a resource is created or where a variable is set, multiple file isn't a great idea... However, it's your customer requirement and do it the much simpler for readability.
这篇关于如何将 terraform 文件(main.tf)拆分为多个文件(无模块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!