Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform child module does not inherit provider from root module

Tags:

terraform

My problem

I am having trouble defining the provider for my module. Terraform fails to find the provider's plugin when I run terraform init and it shows the wrong provider for my module when I run terraform providers.

Setup

I am using Terraform version 1.3.7 on Debian 11.

Here's an example of what I am trying to do.

I have a main.tf where is my main configuration and modules. In this example I use a single module for creating a docker container.

.
├── main.tf
└── modules/
    └── container_module/
        └── main.tf

In the root module project/main.tf file, I define the provider and call the module:

terraform {                                                                     
  required_providers {                                                          
    docker = {                                                                  
      source  = "kreuzwerker/docker"                                            
      version = "3.0.1"                                                         
    }                                                                           
  }                                                                             
}                                                                               
                                                                                 
provider "docker" {                                                             
  host = "unix:///var/run/docker.sock"                                          
}                                                                               
                                                                                
module "container" {                                                            
  source = "./modules/container_module"                                         
}                                                                               

In the modules/container_module/main.tf, I create the docker container resource:

resource "docker_image" "debian" {
  name = "debian:latest"
}

resource "docker_container" "foo" {
  image = docker_image.debian.image_id
  name  = "foo"
}

What I expect to happen

When I run terraform init, it should download the provider's plugin from kreuzwerker/docker.

What actually happens

Instead, terraform downloads the plugin from kreuzwerker/docker once, then attempt to download it again from hashicorp/docker.

Here's the command's output:

terraform init
Initializing modules...
- container in modules/container_module

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/docker...
- Finding kreuzwerker/docker versions matching "3.0.1"...
- Installing kreuzwerker/docker v3.0.1...
- Installed kreuzwerker/docker v3.0.1 (self-signed, key ID BD080C4571C6104C)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
╷
│ Error: Failed to query available provider packages
│ 
│ Could not retrieve the list of available versions for provider hashicorp/docker: provider registry registry.terraform.io does not have a provider named
│ registry.terraform.io/hashicorp/docker
│ 
│ Did you intend to use kreuzwerker/docker? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on
│ hashicorp/docker, run the following command:
│     terraform providers
╵

When I run terraform providers I get two different sources depending on the file:

terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/kreuzwerker/docker] 3.0.1
└── module.container
    └── provider[registry.terraform.io/hashicorp/docker]

According to the documentation, the child modules should inherit the provider from their parent:
Default Behavior: Inherit Default Providers:

If the child module does not declare any configuration aliases, the providers argument is optional. If you omit it, a child module inherits all of the default provider configurations from its parent module. (Default provider configurations are ones that don't use the alias argument.)

I have already checked this

  • Do terraform modules need required_providers?
    • This answer confirms the provider inheritence.
  • Terraform provider's resources not available in other tf files:
    • This question didn't help.
  • Terraform, providers miss inherits on module
    • This answer to this similar question says that I should add required_providers in the child module, but it is for an older version and it contradicts what I saw elsewhere.
  • I have the same issue when I create a providers.tf file in the root directory.

My question

How should I declare my provider so that the child module can inherit the provider from the root module?

like image 381
Guillaume Avatar asked Jan 27 '26 04:01

Guillaume


1 Answers

kreuzwerker/docker is not a hashicorp provider. Thus as explained here, you have to explicitly define required_providers in each module, as such providers are not inherited.

like image 200
Marcin Avatar answered Jan 29 '26 11:01

Marcin