Providers: install, version, and lock
Terraform 004 · Lesson 2 of 18 · 10:16
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: how Terraform uses providers as plugins, declaring them in required_providers with a source address, version constraints such as the pessimistic operator, and the dependency lock file, with terraform init and its upgrade option. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
It works on my laptop
Bluefin's first configuration worked on one laptop and failed on another, because each engineer had a different version of the AWS provider. Providers are the plugins that let Terraform talk to AWS, Azure, or any other API, and their versions matter. This lesson covers how Terraform uses providers, how to declare and install them, version constraints, and the dependency lock file.
How Terraform uses providers
Terraform has two parts: Terraform Core and plugins. Terraform Core is the terraform program itself: it reads the configuration, manages state, builds the resource graph, and runs the plan. Providers are plugins, separate programs that Core starts and talks to through remote procedure calls, or RPC. Each provider authenticates with its platform, defines the resource types and data sources that it manages, and can offer functions. Terraform Core knows nothing about AWS or Azure itself: everything specific to a platform belongs in its provider, which is released on its own schedule, separately from Terraform.
Declaring providers
You declare each provider in a required providers block, inside the top-level terraform block. The key is the provider's local name, such as aws, and the value gives its source address and a version constraint. The source address has three parts: a hostname, which defaults to the public Terraform Registry, a namespace, and a type, as in hashicorp slash aws. Then a provider block configures it, with settings like the Region, while credentials come from environment variables or shared credential files, not from the code. And terraform init downloads the providers into the hidden dot terraform directory of the working directory.
Version constraints
A version constraint lists the versions that a configuration accepts, with operators like equals, not equals, greater than or equal, and less than. The pessimistic constraint operator, a tilde followed by a greater than sign, lets only the rightmost number of the version increase. So 6.1, with this operator, allows 6.2 and six point ten, but not 7.0, while 6.1.0 allows 6.1.9, but not 6.2. HashiCorp recommends this operator in root modules, to set both a lower and an upper bound, and a required version setting in the terraform block constrains the version of Terraform itself.
The dependency lock file
terraform init records the provider versions that it selected, with their checksums, in the dependency lock file, named dot terraform dot lock dot H C L, in the working directory. Commit this file to version control: then everyone, and every pipeline, installs exactly the same provider versions. On later runs, init reuses the version recorded in the lock file, even when a newer version that matches the constraint exists, and checks the checksums of every package that it downloads. To move to a newer version, run terraform init with the upgrade flag, which selects the newest version that the constraints allow and updates the lock file. Note that the lock file tracks providers only, not modules.
Recap
Let's recap. Terraform Core reads the configuration and builds the plan, and providers are plugins, called through RPC, that talk to each platform's API. Declare providers in required providers, with a source address and a version constraint, and configure them in provider blocks. The pessimistic operator lets only the rightmost number increase. And the dependency lock file, committed to version control, pins the exact provider versions, until terraform init with the upgrade flag selects newer ones.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
During an apply, Terraform creates an instance on AWS. Which part of Terraform actually calls the AWS API?
- Terraform Core, directly
- The AWS provider, a separate plugin
- The state file of the configuration
- The public Terraform Registry
Show the answer
The AWS provider, a separate plugin
The AWS provider is a separate plugin, and it makes the API calls for the resources of its platform. “Terraform Core, directly” is wrong, because Terraform Core holds no platform-specific logic, and talks to the provider through RPC. “The state file of the configuration” is a record of what Terraform manages, and it calls nothing. And “The public Terraform Registry” is where you find and download providers, not what runs them.
The AWS provider is constrained to ~> 6.0. Versions 6.0.4, 6.9.0, and 7.0.0 are available, and there is no lock file yet. Which version does terraform init select?
- 6.0.4, the lowest allowed version
- 7.0.0, the newest version
- 6.9.0, the newest allowed version
- Any of them: constraints are hints
Show the answer
6.9.0, the newest allowed version
Without a lock file, init selects the newest version that the constraint allows, and six point nine point zero is the newest below seven. “6.0.4, the lowest allowed version” would be right only for the constraint six point zero point zero, which allows patch releases only. “7.0.0, the newest version” is excluded, because the constraint stops before version seven. And “Any of them: constraints are hints” is wrong, because Terraform enforces the constraint.
Two engineers get different AWS provider versions from the same configuration, because its constraint allows a range. What should the team do so that everyone uses the same version?
- Commit .terraform.lock.hcl to the repository
- Commit the .terraform directory
- Run terraform init -upgrade everywhere
- Remove the version constraint
Show the answer
Commit .terraform.lock.hcl to the repository
The lock file keeps the exact version and its checksums, and once it is committed, init installs that version everywhere. “Commit the .terraform directory” would share downloaded binaries for one platform only, and that directory does not belong in version control. “Run terraform init -upgrade everywhere” would make each machine move to the newest version, which is how they drifted apart. And “Remove the version constraint” would accept any version at all.