004 · Domain 2: Terraform fundamentals · 13% of the exam
Task 2.3: Write Terraform configuration using multiple providers
More than one provider or one provider many times: alias for extra configurations, the provider meta-argument on a resource, passing configurations to modules with providers, and local names for providers from different namespaces.
Study it
Several providers and aliases in one configuration and across modules
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A configuration uses version 6 of the AWS provider and has a single provider block:provider "aws" {
region = "us-east-1"
}One replica bucket must be created in eu-west-1, while every other resource stays in us-east-1. The team does not want to add a second provider configuration. What should it do?
AChange region in the provider block to eu-west-1 for the apply that creates the bucket
BPut the bucket in a separate configuration, since one configuration can use only one region per provider
CSet the bucket resource's own region argument to "eu-west-1"
DAdd an aws provider block with alias = "eu" and set provider = aws.eu on the bucket
Show the answer and why
AChange region in the provider block to eu-west-1 for the apply that creates the bucket
Incorrect
The provider block sets the region for every resource that uses it, so the whole configuration would be planned against eu-west-1, not just the bucket.
BPut the bucket in a separate configuration, since one configuration can use only one region per provider
Incorrect
There is no such limit, and with version 6 of the AWS provider a single provider configuration can manage resources in several regions.
CSet the bucket resource's own region argument to "eu-west-1"
Correct
Version 6.0.0 of the AWS provider adds a top-level region argument to most resources; it overrides the provider's region for that resource only, without an additional provider block.
DAdd an aws provider block with alias = "eu" and set provider = aws.eu on the bucket
Incorrect
That works, but it is exactly the second provider configuration the team wants to avoid.
Before version 6.0.0 of the AWS provider, every extra region needed its own provider configuration. Now most resources take a region argument that defaults to the provider's region, and changing it on an existing resource forces replacement.
A root module defines a default aws provider and an aliased one, aws.dr. A child module ./replica must create its resources with the aws.dr configuration and refer to it as aws.dr inside the module. Which two changes are needed? (Choose TWO.)
ACopy the provider "aws" block with alias = "dr" into the child module
BNothing, because child modules inherit every provider configuration of the parent automatically
CIn the module block, set providers = { aws.dr = aws.dr }
DSet provider = aws.dr as an argument on the module block
EIn the child module's required_providers entry for aws, add configuration_aliases = [aws.dr]
Show the answer and why
ACopy the provider "aws" block with alias = "dr" into the child module
Incorrect
Shared modules should not contain provider blocks; configurations come from the root module.
BNothing, because child modules inherit every provider configuration of the parent automatically
Incorrect
Using aws.dr inside the module needs the alias declared there and passed in by the caller; it is not available to the module on its own.
CIn the module block, set providers = { aws.dr = aws.dr }
Correct
Aliased configurations are passed to a child module explicitly through the providers map of the module block.
DSet provider = aws.dr as an argument on the module block
Incorrect
The singular provider meta-argument belongs to resources and data sources; module blocks take a providers map.
EIn the child module's required_providers entry for aws, add configuration_aliases = [aws.dr]
Correct
A child module declares the aliases it expects with configuration_aliases; without it, referencing aws.dr inside the module is an error.
Passing an alias into a module takes both halves: the caller maps it in providers, and the module declares it in configuration_aliases.
A root module has one provider "aws" block and calls a child module that only declares aws resources and its own required_providers entry. The module block has no providers argument. Which configuration do the child's resources use?
ANone, so plan fails until the module block sets providers
BA new empty configuration created inside the module
CThe configuration named in the child module's lock file
DThe root module's default aws configuration
Show the answer and why
ANone, so plan fails until the module block sets providers
Incorrect
The providers argument is needed only to pass something other than the defaults.
BA new empty configuration created inside the module
Incorrect
The module uses the parent's default configuration.
CThe configuration named in the child module's lock file
Incorrect
Lock files belong to the whole configuration and hold no configurations.
DThe root module's default aws configuration
Correct
A child module automatically inherits default provider configurations from its parent.
Simple configurations need no wiring: default provider configurations flow down to child modules.
A child module uses resources from a partner provider, mycorp/netbox. The root module declares it in required_providers, but the child module does not, and terraform init fails because it cannot find the provider the child module asks for. Why?
APartner providers can be used in the root module but not inside child modules
BRequirements are not inherited; each module declares its own source
CThe child module needs a provider block for netbox
DThe lock file is out of date and must be deleted
Show the answer and why
APartner providers can be used in the root module but not inside child modules
Incorrect
Any module can use a partner provider once it declares the requirement.
BRequirements are not inherited; each module declares its own source
Correct
Child modules do not inherit provider source or version requirements from their parents, so the child must declare mycorp/netbox itself.
CThe child module needs a provider block for netbox
Incorrect
A provider block configures a provider; it does not say where to install it from. The source address belongs in required_providers.
DThe lock file is out of date and must be deleted
Incorrect
The failing lookup comes from the child's missing requirement, not from the lock file.
Each module declares the providers it needs, with their source addresses, even when the root module already declares the same provider.
A module creates a VM with a cloud provider, generates its SSH key pair with the hashicorp/tls provider, and registers the VM in an inventory system through a partner provider, acme/inventory. According to the provider requirements documentation, what should the module's required_providers block contain?
AAn entry for acme/inventory only, since it is the one outside the hashicorp namespace
BOne entry whose source lists all three providers
CAn entry for each of the three providers, with its source and version constraint
DAn entry for the cloud provider, as the other two serve it as helpers
Show the answer and why
AAn entry for acme/inventory only, since it is the one outside the hashicorp namespace
Incorrect
The documentation asks a module to declare every provider it requires, whoever publishes it.
BOne entry whose source lists all three providers
Incorrect
Each key in required_providers declares one provider, with one source address.
CAn entry for each of the three providers, with its source and version constraint
Correct
Each module must declare which providers it requires, so that Terraform can install and use them; an entry gives the local name, the source address and the accepted versions.
DAn entry for the cloud provider, as the other two serve it as helpers
Incorrect
Helper providers such as tls are providers like any other and need their own requirement entry.
One configuration can mix many providers from different publishers; each one gets its own entry in required_providers.
An older configuration has this provider block, and an engineer adds an alias to it so that a second, default aws block can be added next to it:provider "aws" {
alias = "west"
version = "~> 5.0"
region = "us-west-2"
}Terraform now reports an error for this block. What is wrong, and what is the fix?
Aalias and version cannot share a block; declare the constraint in required_providers
BAn aliased block must also set source, which takes the place of version
CAn aliased block needs its version as a list, such as version = ["~> 5.0"]
DThe alias must include the provider name, written alias = "aws.west"
Show the answer and why
Aalias and version cannot share a block; declare the constraint in required_providers
Correct
The provider block reference says alias and version are optional and cannot both be used in one block; version is deprecated there in any case, and constraints belong in required_providers.
BAn aliased block must also set source, which takes the place of version
Incorrect
source is never set in a provider block; it is part of the provider's entry in required_providers.
CAn aliased block needs its version as a list, such as version = ["~> 5.0"]
Incorrect
A version constraint is a string, never a list; the error comes from combining version with alias in one block.
DThe alias must include the provider name, written alias = "aws.west"
Incorrect
alias takes the bare name, here "west"; the provider name is added only when a resource refers to the configuration as aws.west.
The provider block configures a provider; required_providers says which provider and which versions. Keeping version constraints out of provider blocks avoids this error and the deprecation warning alike.