Skip to content

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?

  1. AChange region in the provider block to eu-west-1 for the apply that creates the bucket
  2. BPut the bucket in a separate configuration, since one configuration can use only one region per provider
  3. CSet the bucket resource's own region argument to "eu-west-1"
  4. 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.

Question 2 · choose 2

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.)

  1. ACopy the provider "aws" block with alias = "dr" into the child module
  2. BNothing, because child modules inherit every provider configuration of the parent automatically
  3. CIn the module block, set providers = { aws.dr = aws.dr }
  4. DSet provider = aws.dr as an argument on the module block
  5. 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.

Question 3 · choose 1

A configuration has provider "google" { alias = "eu" ... }. How does a resource select this configuration?

  1. Aprovider = "eu"
  2. Bprovider = google.eu
  3. Calias = google.eu
  4. Dprovider = google.alias.eu
Show the answer and why
  • Aprovider = "eu"

    Incorrect

    The alias alone names no configuration; the reference also needs the provider's local name.

  • Bprovider = google.eu

    Correct

    The provider meta-argument takes an unquoted <PROVIDER>.<ALIAS> reference.

  • Calias = google.eu

    Incorrect

    alias is set on the provider block, not on the resource.

  • Dprovider = google.alias.eu

    Incorrect

    The reference has two parts only, the provider's local name and the alias: <PROVIDER>.<ALIAS>.

An aliased configuration is chosen with the provider meta-argument and an unquoted <NAME>.<ALIAS> reference.

Question 4 · choose 1

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?

  1. ANone, so plan fails until the module block sets providers
  2. BA new empty configuration created inside the module
  3. CThe configuration named in the child module's lock file
  4. 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.

Question 5 · choose 1

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?

  1. APartner providers can be used in the root module but not inside child modules
  2. BRequirements are not inherited; each module declares its own source
  3. CThe child module needs a provider block for netbox
  4. 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.

Question 6 · choose 1

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?

  1. AAn entry for acme/inventory only, since it is the one outside the hashicorp namespace
  2. BOne entry whose source lists all three providers
  3. CAn entry for each of the three providers, with its source and version constraint
  4. 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.

Question 7 · choose 1

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?

  1. Aalias and version cannot share a block; declare the constraint in required_providers
  2. BAn aliased block must also set source, which takes the place of version
  3. CAn aliased block needs its version as a list, such as version = ["~> 5.0"]
  4. 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.

Practise domain 2 →Practise all domains →