How to split a terraform file (main.tf) in several files (No Modules)?

27,536

Solution 1

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.

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.

Solution 2

Terraform does not ascribe any special meaning to which filenames you use and how many files you have. Terraform instead reads all of the .tf files and considers their contents together.

Therefore you can freely move the blocks from your main.tf file into as many separate .tf files in the same directory as you like, and Terraform will consider the configuration to be exactly equivalent as long as you change nothing in the contents of those blocks while you do it.

(There is a special case for Override Files that makes the above not strictly true. As long as you avoid naming any of your files override.tf or with an _override.tf suffix that special case will not apply, but I'm mentioning it just for completeness.)

Solution 3

Even though no module has been included in your project. Terraform always runs in the context of a single root module. The .tf file is under root module.

Based on Terraform Documentation:

Terraform evaluates all of the configuration files in a module, effectively treating the entire module as a single document. Separating various blocks into different files is purely for the convenience of readers and maintainers, and has no effect on the module's behavior.

https://www.terraform.io/docs/language/files/index.html#directories-and-modules

Solution 4

You can try as per https://marcelzehner.ch/2018/05/22/terraform-using-multiple-files-for-configurations-and-variables. I'm sure it will be useful in your scenario.

Share:
27,536
José Enrique Hernández
Author by

José Enrique Hernández

Updated on July 09, 2022

Comments

  • José Enrique Hernández
    José Enrique Hernández almost 2 years

    I have a GCP infrastructure deployed through Terraform: buckets, service accounts, Compute Engines, VPC, cloud SQL, BigTable, BigQuery, Composer, etc.

    Terraform v0.11.10 Provider "google" (2.15.0)

    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.

    I only have one terraform state file located in a GCP bucket.

    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 ?)

    NOTE: This has nothing to do with Terraform modules, it's just about dividing a single terraform file (.tf) into several files (.tf)

    Thanks in advance!

  • coding_idiot
    coding_idiot over 4 years
    Links can go dead; it's ok to refer them but they shouldn't be relied upon to form the answer. Please add answer here. meta.stackexchange.com/questions/8231/…
  • trallnag
    trallnag almost 4 years
    But what about resource names? I can use the same combination in multiple files. file1.tf resource "type" "this" file2.tf resource "type" "this"
  • Elydasian
    Elydasian almost 3 years
    Please provide a detailed explanation to your answer, in order for the next user to understand your answer better. Also, provide a basic coverage of the content of your link, in case it stops working in the future.