Search code examples
terraformterraform-provider-awsterraform-modules

Does terraform module need "dependency lock file"?


I'm confused about how to manage the "dependency-lock-file" named .terraform.lock.hcl in my module. In my team terraform repo, there is a lockfile in a module.

I think it is not the position, since there is no reason to command terraform init in the module. So I'm gonna remove it. Do you guys think it is okay?


Solution

  • A dependency lock file is for an entire configuration (a tree of modules starting at a root module) rather than for individual modules.

    Due to how Terraform CLI works the dependency lock file ends up placed in the same directory as the root module -- the directory that you run terraform init etc from -- but it isn't technically part of the root module itself, and instead records the provider version selections made for the entire configuration tree.

    Therefore a module that is intended only to be called from other modules using a module block does not need to have a .terraform.lock.hcl file in its directory, and Terraform will ignore any such file if it isn't in the root module directory where you run terraform init.

    There is some subtlety here, though: a module that's normally used only as a shared module via a module block might still nonetheless be used directly when you are developing it. This will be particularly true in the forthcoming Terraform v1.6 which adds a terraform test command for automated integration testing of shared modules. In that case you would run terraform init in the module's directory only to perform development tasks, and the .terraform.lock.hcl file created will record selections that apply only to your development environment for that module. They'll still be ignored when the module is called from another module using a module block.