Multiple providers and Terraform state
Terraform 004 · Lesson 3 of 18 · 16:40
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: several providers in one configuration, provider aliases for a second Region, why Terraform needs state to map resources to real objects, and how to handle the state file safely with a remote backend. 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.
Two clouds, two Regions
Bluefin's tracking API runs in two AWS Regions, one in Europe and one in the United States, and its analytics run on Azure. One configuration can drive all of them, with several providers, and several configurations of the same provider. This lesson covers both, then the state, which Terraform needs to know what it manages.
Several providers
A configuration can use as many providers as it needs, for example AWS and Azure, plus a DNS provider or GitHub. List them all in the required providers block, each with its source address and its version constraint. Terraform picks the provider of each resource from the first word of its type: a type that starts with aws belongs to the AWS provider, and a type that starts with azurerm to the Azure provider. And references work across providers: an Azure resource can use an attribute of an AWS resource, and Terraform orders the work across both.
Provider aliases
To use the same provider with two different settings, such as two Regions, declare two provider blocks, and give the second one an alias. The block without an alias is the default configuration, which every resource of that provider gets unless it asks for another one. A resource selects the other configuration with its provider argument, written as the provider name, a dot, and the alias, like aws dot us. Modules work the same way: a module block can pass an aliased configuration to a child module, with its providers argument. So Bluefin's API servers in Europe and in the United States come from one configuration, in two Regions.
Why Terraform needs state
Terraform needs state, a record of the infrastructure that it manages, kept by default in a state file in the working directory. First, state maps each resource in the configuration to a real world object, through its ID, and one real object belongs to one resource instance only. Second, it stores metadata, like the dependencies between resources, so that Terraform can destroy resources in the right order, even after you remove them from the configuration. Third, it keeps a cache of attribute values, which speeds up work on large infrastructures. And before each plan, Terraform refreshes the state: it reads the real objects, and updates the state to match.
Handling the state file
The state file is plain JSON, and it holds every attribute of every resource, including passwords and other sensitive values, in plain text. So never commit it to version control, and treat it as sensitive data. Never edit it by hand either: use the terraform state commands instead, or the blocks that lessons fifteen and sixteen cover. For a team, store the state in a remote backend, such as HCP Terraform or an S3 bucket, which shares it, locks it during a run, and can encrypt it. Lesson fourteen covers backends in detail.
Recap
Let's recap. One configuration can use several providers, listed in required providers, and each resource uses the provider named by the first word of its type. An alias adds a second configuration of the same provider, which a resource selects with its provider argument. State maps resources to real world objects, stores metadata like dependencies, and is refreshed before each plan. And the state file holds secrets in plain text: keep it out of Git, never edit it by hand, and use a remote backend for a team.
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.
A configuration uses the AWS and Azure providers. How does Terraform know which provider manages a resource of type azurerm_resource_group?
- From the first word of its type
- From the order of the provider blocks
- From the name of the file that holds it
- From a provider tag set on the resource
Show the answer
From the first word of its type
Each resource type starts with the local name of its provider, and Terraform uses that provider, unless the resource's provider argument names another configuration. “From the order of the provider blocks” is wrong, because the order of provider blocks means nothing to Terraform. “From the name of the file that holds it” is wrong too, because file names mean nothing: Terraform loads all the files of the directory together. And “From a provider tag set on the resource” is wrong, because tags are settings of the resource, not instructions to Terraform.
In the same change, Bluefin removes a database and the subnet that it depends on from the configuration. How does Terraform know to destroy the database first?
- From the order of blocks in the code
- By asking the cloud provider's API
- It destroys both at once, in any order
- From dependencies stored in the state
Show the answer
From dependencies stored in the state
The state stores the dependencies between resources, so Terraform still knows the order after the blocks are gone. “From the order of blocks in the code” is wrong, because both blocks have been removed from the code. “By asking the cloud provider's API” is wrong, because cloud APIs do not describe Terraform's dependencies. And “It destroys both at once, in any order” is wrong, because Terraform destroys dependent resources in reverse order, never at random.
To share the state with the team, an engineer suggests committing the state file to Git. What is the main risk?
- Git cannot store JSON files
- Secrets become readable in the repo
- Terraform refuses a state file from Git
- The provider lock file stops working
Show the answer
Secrets become readable in the repo
The state holds sensitive values in plain text, so anyone with access to the repository could see them. “Git cannot store JSON files” is wrong, because Git stores JSON like any other text. “Terraform refuses a state file from Git” is wrong, because Terraform uses a local state file wherever it comes from. And “The provider lock file stops working” is wrong, because the lock file pins provider versions, and has nothing to do with state.