Home > Blockchain >  How to use output of one child module as an input to another child module in Terraform
How to use output of one child module as an input to another child module in Terraform

Time:09-23

I have the below directory structure


├── main.tf
├── output.tf
├── variables.tf
├── modules
│   ├── ServicePrincipal
│   │   ├── variables.tf
│   │   ├── outputs.tf
│   │   ├── main.tf
│   ├── aks
│   │   ├── main.tf
│   │   ├── output.tf
│   │   └── variables.tf
...

Issue: I want to use client_id and client_secret generated from service principal module as an input to create my aks cluster. I am able to reference the below output variables from my root main.tf by module.modulename.outputvarname however, I cannot access it in another child module(aks) as var.client_id or module.serviceprincipal.client_id

main.tf of root module where I am able to use client_id and client_secret

module "ServicePrincipal" {
  source                 = "./modules/ServicePrincipal"
  service_principal_name = var.service_principal_name
  redirect_uris          = var.redirect_uris

}

module "aks" {
  source                 = "./modules/aks/"
  service_principal_name = var.service_principal_name
  serviceprinciple_id    = module.ServicePrincipal.service_principal_object_id
  serviceprinciple_key   = module.ServicePrincipal.client_secret
  location            = var.location
  resource_group_name = var.rgname
  depends_on = [
    module.ServicePrincipal
  ]

}

main.tf of aks module

service_principal  {
    client_id = var.client_id
    client_secret = var.client_secret
  }

output.tf for my ServicePrincipal module

output "client_id" {
  description = "The application id of AzureAD application created."
  value       = azuread_application.main.application_id

}

output "client_secret" {
  description = "Password for service principal."
  value       = azuread_service_principal_password.main.*.value
  
}

Below is the error I am getting:


Error: Missing required argument

  on main.tf line 136, in module "aks":
 136: module "aks" {

The argument "client_id" is required, but no definition was found.


Error: Missing required argument

  on main.tf line 136, in module "aks":
 136: module "aks" {

The argument "client_secret" is required, but no definition was found.

I already defined those as variables in aks module and root module, am I missing something here?

Thanks in advance!

Piyush

CodePudding user response:

Child modules can't reference each others outputs. You have to explicitly pass them in the root module from one module to the second, e.g.

in root:

module "ServicePrincipal" {
}

module "aks" {
   client_id = module.ServicePrincipal.client_id
}

CodePudding user response:

You're using output name as client_id and client_secret but in module you're calling with different names ?

module.ServicePrincipal.service_principal_object_id

  • Related