Skip to main content
0 reactions

How to Upgrade OpenTofu Providers with tofu init -upgrade

A Terminal window running tofu init -upgrade: OpenTofu finds hashicorp/local versions matching ~> 2.8, installs v2.9.1, and says it has made changes to the provider selections in the .terraform.lock.hcl file.
tofu init -upgrade moves the provider from 2.8.0 to 2.9.1.

Once a provider is in your project’s lock file, tofu init keeps using that version, even after newer ones come out. To move to a newer version on purpose, you run tofu init -upgrade.

Syntax

Plain text
tofu init -upgrade [options]
  • -upgrade: installs the newest provider and module versions your version constraints allow, instead of the versions recorded in the lock file.
  • [options]: any other tofu init options. This guide uses none.

All the options are in Command: init (opens in a new tab) in the OpenTofu® docs.

In this guide, a project starts on version 2.8.0 of the hashicorp/local provider and moves to 2.9.1, its newest release.

Prerequisites

Step 1: Start on an Older Version

Create main.tf with this block, which asks for exactly 2.8.0:

Plain text
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.8.0"
    }
  }
}
Bash
tofu init
Plain text
Initializing provider plugins...
- Finding hashicorp/local versions matching "2.8.0"...
- Installing hashicorp/local v2.8.0...
- Installed hashicorp/local v2.8.0 (signed, key ID 0C0AF313E5FD9F80)

The lock file now records 2.8.0.

Step 2: Allow Newer Versions

Change the version line to allow any 2.x release from 2.8 on:

Plain text
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.8"
    }
  }
}

Run tofu init again:

Bash
tofu init
Plain text
Initializing provider plugins...
- Reusing previous version of hashicorp/local from the dependency lock file
- Using previously-installed hashicorp/local v2.8.0

OpenTofu has been successfully initialized!

OpenTofu still uses 2.8.0. It matches the new constraint, and the lock file says to use it.

Step 3: Upgrade with tofu init -upgrade

Bash
tofu init -upgrade
Plain text
Initializing provider plugins...
- Finding hashicorp/local versions matching "~> 2.8"...
- Installing hashicorp/local v2.9.1...
- Installed hashicorp/local v2.9.1 (signed, key ID 0C0AF313E5FD9F80)

Providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://opentofu.org/docs/cli/plugins/signing/

OpenTofu has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

OpenTofu has been successfully initialized!
A Terminal window running tofu init -upgrade: OpenTofu finds hashicorp/local versions matching ~> 2.8, installs v2.9.1, and says it has made changes to the provider selections in the .terraform.lock.hcl file.” class=”has-border-color wp-image-26188″ style=”border-color:#a7aaad;border-width:1px;border-radius:6px”/><figcaption class=tofu init -upgrade moves the provider from 2.8.0 to 2.9.1.

OpenTofu installed 2.9.1, the newest version ~> 2.8 allows, and changed the lock file to match. It now says:

Plain text
  version     = "2.9.1"
  constraints = "~> 2.8"

Step 4: Check and Commit the Change

Run tofu plan to check the project still works with the new version, then commit .terraform.lock.hcl, so everyone else gets 2.9.1 too.

Tip: Read what changed in the provider’s release notes before you upgrade a real project. A minor release can still change how a resource behaves.

Clean Up

Delete the upgrade-providers folder. Nothing outside it was created.

FAQ

Does tofu init -upgrade go past my version constraint?

No. It picks the newest version the constraint allows. To get a version outside it, change the constraint first.

Does it upgrade modules too?

Yes. -upgrade also checks the modules the project uses for newer versions within their constraints.

Conclusion

tofu init always keeps the versions in the lock file. tofu init -upgrade is how you move to newer ones within your version constraints, in one deliberate step you can review and commit.

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

0 comments