hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.49k stars 9.52k forks source link

"terraform providers add" subcommand for easier provider dependency additions #33015

Open apparentlymart opened 1 year ago

apparentlymart commented 1 year ago

Adding a new provider requirement to a Terraform configuration is a step typically done at least once for each module, since a Terraform module that doesn't use any providers is typically (but not always) of limited use.

Adding provider requirements in a robust way currently requires a number of manual steps:

  1. Look up the provider in Terraform Registry to find out what its current version number is.
  2. Add the required_providers block if this is the first provider dependency in the configuration, and then add an entry to that block mentioning the provider and setting a minimum or pessimistic version constraint for it.
  3. Run terraform init to get the provider installed and generate the .terraform.lock.hcl entry for it.
  4. (do all of the other things you intended to do with the provider...)

An idea from a long time ago that we didn't end up pursuing at the time was a CLI command to automate away some of these steps. For example:

$ terraform providers add hashicorp/aws

I imagine that this command would automatically perform actions functionally equivalent to steps 1 through 3 in my list above.

Of course, there are various details of this that we'd need to figure out, including but not limited to:


One major motivation for making this an imperative command is to make it easier to include in provider setup instructions and in other tutorials. Today provider developers and tutorial authors often need to spend a lot of words explaining how to do something like the steps 1 through 3 above, and it would be nice to replace that with just a single command to run.

There is precedent for commands like this in other language ecosystems:

These examples show that there might be some other options we could consider adding to terraform providers add either initially or in later updates:

zetHannes commented 1 year ago

It would be lovely if this was added. One question I'd have to the draft: If the default version should be specified with the >= constraint, how would the "correct" constrained version be determined?

apparentlymart commented 1 year ago

In the manual step 3, if Terraform finds a requirement for a provider that isn't currently present in the lock file then it selects the latest available version that matches the version constraints in the configuration.

In this more automated flow then, I expect that terraform providers add would find the latest available version for the provider and:

The first rule above is under the assumption that most authors won't immediately test their module against earlier versions of the provider anyway, and so it would be over-promising to choose any earlier version. Of course, this is only a bootstrapping helper so there's nothing to stop a module author from manually loosening the constraint and then testing with earlier versions of the provider, if they choose to.

One thing I notice in revisiting this proposal is that we'd be implicitly accepting the idea that even shared modules would end up with dependency lock files, but they'd be used only for direct development work such as running terraform test because dependency lock files are a whole-configuration concept that belongs in the root module. With the addition of terraform test this new possibility of shared modules having lock files seems inevitable to me anyway, but we should take some care to document this carefully to minimize confusion about how the dependency lock files of different modules will interact with one another. (The answer is: they actually won't interact at all, because only one dependency lock file is ever in scope at a time.)