Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I version the .terraform folder?

Tags:

terraform

I am starting to use Terraform and I have a .terraform folder created by "terraform init/apply" containing :

  • ./plugins/linux_amd64/lock.json
  • ./plugins/linux_amd64/terraform-provider-google
  • ./plugins/modules/modules.json
  • terraform.tfstate

Should I version these files ? I would say no ...

like image 840
poloC Avatar asked Dec 11 '19 15:12

poloC


1 Answers

The .terraform directory is a local cache where Terraform retains some files it will need for subsequent operations against this configuration. Its contents are not intended to be included in version control.

However, you can ensure that you can faithfully reproduce this directory on other systems by specifying certain things in your configuration that inform what Terraform will place in there:

  • Use required_providers in a terraform block to specify an exact version constraint for the Google Cloud Platform provider:

    terraform {
      required_providers {
        google = "3.0.0"
      }
    }
    

    (this relates to the .terraform/plugins directory)

  • In each module you call (which seems to be none so far, but perhaps in future), ensure its source refers to an exact version rather than to a floating branch (for VCS modules) or set version to an exact version (for modules from Terraform Registry):

    module "example"
      source = "git::https://github.com/example/example.git?ref=v2.0.0"
      # ...
    }
    
    module "example"
      source  = "hashicorp/consul/aws"
      version = "v1.2.0
    }
    

    (this relates to the .terraform/modules directory)

  • If you are using a remote backend, include the full configuration in the backend block inside the terraform block, rather than using the -backend-config argument to terraform init.

    (this relates to the .terraform/terraform.tfstate file, which remembers your active backend configuration for later operations)

like image 134
Martin Atkins Avatar answered Oct 03 '22 04:10

Martin Atkins