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
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 othertofu initoptions. 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
- OpenTofu installed: How to Install OpenTofu on macOS.
- A terminal and an empty folder, such as
upgrade-providers.
Step 1: Start on an Older Version
Create main.tf with this block, which asks for exactly 2.8.0:
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "2.8.0"
}
}
}
tofu init
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:
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.8"
}
}
}
Run tofu init again:
tofu init
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
tofu init -upgrade
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!
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:
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.
Related
- OpenTofu Tutorial — Part 3: Providers: Run the Website in Docker
- What Is .terraform.lock.hcl in OpenTofu?
- OpenTofu Version Constraints Explained
- What Does tofu init Do in OpenTofu?
Additional Resources
- Command: init (opens in a new tab)
- Dependency Lock File (opens in a new tab)
- Version Constraints (opens in a new tab)
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