Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat .terraform/providers as a cache to avoid re-downloading on subsequent "terraform init" in the same directory #27155

Closed
joncolby opened this issue Dec 5, 2020 · 9 comments

Comments

@joncolby
Copy link

joncolby commented Dec 5, 2020

This new issue re-raises an issue that I believe was incorrectly closed. Issue #27099 was closed with the reason "duplicate issue of #27092", however the issue used as a reason to close #27099 is not the same use case.

Problem: running terraform init multiple times should re-use previously downloaded provider versions. It seems like terraform providers are always downloaded, regardless of any additional settings.

Due to this issue, the added time to re-download providers with every run causes terraform workflows to take longer

Terraform Version

Terraform v0.14.0

Expected Behavior

Running terraform init 2 or more times, with no provider version changes

Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/local from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Reusing previous version of hashicorp/template from the dependency lock file
- Reusing previous version of hashicorp/archive from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file

Terraform has been successfully initialized!

Actual Behavior

Terraform Init First Run

Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/local from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Reusing previous version of hashicorp/template from the dependency lock file
- Reusing previous version of hashicorp/archive from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/archive v2.0.0...
- Installed hashicorp/archive v2.0.0 (signed by HashiCorp)
- Installing hashicorp/aws v3.20.0...
- Installed hashicorp/aws v3.20.0 (signed by HashiCorp)
- Installing hashicorp/local v2.0.0...
- Installed hashicorp/local v2.0.0 (signed by HashiCorp)
- Installing hashicorp/null v3.0.0...
- Installed hashicorp/null v3.0.0 (signed by HashiCorp)
- Installing hashicorp/template v2.2.0...
- Installed hashicorp/template v2.2.0 (signed by HashiCorp)

Terraform has been successfully initialized!

Terraform init Second run

Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/local from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Reusing previous version of hashicorp/template from the dependency lock file
- Reusing previous version of hashicorp/archive from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/archive v2.0.0...
- Installed hashicorp/archive v2.0.0 (signed by HashiCorp)
- Installing hashicorp/aws v3.20.0...
- Installed hashicorp/aws v3.20.0 (signed by HashiCorp)
- Installing hashicorp/local v2.0.0...
- Installed hashicorp/local v2.0.0 (signed by HashiCorp)
- Installing hashicorp/null v3.0.0...
- Installed hashicorp/null v3.0.0 (signed by HashiCorp)
- Installing hashicorp/template v2.2.0...
- Installed hashicorp/template v2.2.0 (signed by HashiCorp)

Terraform has been successfully initialized!

Relevant Configuration

terraform {
  required_version = ">= 0.14"
  required_providers {
    archive = {
      source  = "hashicorp/archive"
      version = "~> 2.0.0"
    }
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
    local = {
      source  = "hashicorp/local"
      version = "~> 2.0.0"
    }
    null = {
      source  = "hashicorp/null"
      version = "~> 3.0.0"
    }
    template = {
      source  = "hashicorp/template"
      version = "~> 2.2.0"
    }
  }
}

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init (multiple times for the same configuration)

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

@joncolby joncolby added bug new new issue not yet triaged labels Dec 5, 2020
@adicostil
Copy link

This behavior seems to be by design.

to solve the caching issue you mentioned you need to tell terraform to cache the plugins. Add this line in $HOME/.terraformrc

  plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"

then it should work as expected. Not sure why this is not the default behavior.

see https://www.terraform.io/docs/commands/cli-config.html

@joncolby
Copy link
Author

joncolby commented Dec 6, 2020

@adicostil - thanks for this! This helps to "workaround" the problem seen with terraform 0.14.1

I tested this with terraform 0.13.5 and the providers download behaviour is different (it is the expected behaviour)

This is the Terraform 0.13.5 output after running terraform init a second time:


Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.20.0
- terraform.io/builtin/terraform is built in to Terraform
- Using previously-installed hashicorp/archive v2.0.0
- Using previously-installed hashicorp/local v2.0.0
- Using previously-installed hashicorp/null v3.0.0
- Using previously-installed hashicorp/external v2.0.0
- Using previously-installed hashicorp/template v2.2.0
- Using previously-installed hashicorp/kubernetes v1.13.3
- Using previously-installed terraform-providers/statuscake v1.0.0
- Using previously-installed carlpett/sops v0.5.3
- Using previously-installed hashicorp/random v3.0.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

@joncolby joncolby changed the title 0.14.0 terraform init redownloads plugins every time (reopen request) terraform init redownloads plugins every time (reopen request) Dec 6, 2020
@joncolby joncolby changed the title terraform init redownloads plugins every time (reopen request) 0.14.0 terraform init redownloads plugins every time (reopen request) Dec 6, 2020
@danieldreier danieldreier added the v0.14 Issues (primarily bugs) reported against v0.14 releases label Dec 7, 2020
@apparentlymart
Copy link
Contributor

Hi all,

What's happened here is that previously Terraform was using the contents of the .terraform directory as a sort of implied dependency lock -- the presence of a provider in there caused Terraform to re-select that version unless you use -upgrade -- but now the .terraform.lock.hcl file has replaced it, so Terraform only considers the lock file when deciding what to install, and it does not consider what's already installed in .terraform because the lock file is always authoritative.

Of course, a side-effect of that new design is that the lock file can only specify a version, and can't also serve as a sort of implied "cache" of the packages. Therefore Terraform always installs what the lock file says because the lock file doesn't contain any packages.

It would be possible in principle for Terraform to treat the .terraform directory just as a cache, with .terraform.lock.hcl serving as the authority for which packages are needed. That's some new behavior that we'll need to design, so I'm going to relabel this as an enhancement just to send this into our "design a new behavior" process, rather than our "make the implementation match the intended design" (bug fixing) process.

@apparentlymart apparentlymart added cli enhancement and removed bug new new issue not yet triaged v0.14 Issues (primarily bugs) reported against v0.14 releases labels Dec 7, 2020
@apparentlymart apparentlymart changed the title 0.14.0 terraform init redownloads plugins every time (reopen request) Treat .terraform/providers as a cache to avoid re-downloading on subsequent "terraform init" in the same directory Dec 8, 2020
@joncolby
Copy link
Author

joncolby commented Dec 8, 2020

@apparentlymart - Thanks for the explanation. Now I understand how this behaviour relates to the new lock mechanism.

For now, I will use the plugins-cache-dir as a workaround to achieve the desired cache behaviour.

keep up the good work!

@erezool
Copy link

erezool commented Dec 8, 2020

@apparentlymart - Thanks for the explanation. Now I understand how this behaviour relates to the new lock mechanism.

For now, I will use the plugins-cache-dir as a workaround to achieve the desired cache behaviour.

keep up the good work!

Only that the offered work around doesn't work too.

@joncolby
Copy link
Author

@erezool - The workaround works for me. I am using a client configuration file .terraformrc containing the following. what did you try exactly?

plugin_cache_dir = "mycachedir"

@erezool
Copy link

erezool commented Dec 10, 2020

@joncolby Thanks for replying. It does work now and I didn't change a thing. Pretty strange I must say

@apparentlymart
Copy link
Contributor

Hi all!

It looks like this issue was duplicated with #27534, but we didn't notice at the time. We resolved that in v0.14.6 so that Terraform's installer will now only attempt to fetch and install a provider package if the version indicated in the lock file isn't already present. That is, in effect, what I described in my earlier comment above.

Given that, I'm going to close this. If you are still seeing redundant installation on Terraform v0.14.6 or later then please open a new issue to discuss that, since the details of how the installer works are now different than they were for this issue and so we'll need to start over with a new set of debug/trace logs, etc, in order to investigate.

Thanks!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants