简体   繁体   中英

How to call parent resource output in child module in terraform

I am creating a below

ABC -- ecs.tf (gives me cluster id)

Content of ecs.tf:

resource "aws_ecs_cluster" "first_cluster" {
  name = "firstCluster"
  capacity_providers = ["FARGATE"]
  default_capacity_provider_strategy {
    capacity_provider = "FARGATE"
    base              = 0
    weight            = 1
  }
  setting {
    name  = "containerInsights"
    value = "enabled"
  }
}
output "cluster_id" {
  value = aws_ecs_cluster.first_cluster.id
}

Now under ABC folder which is root folder, i have another folder with CHILD folder which has app.tf

ABC/ ├─ 儿童/ │ ├─ app.tf ├─ ecs.tf

Question: How can i use cluster_id from ecs.tf in CHILD\app.tf?

app.tf:

  • module in this file is already calling different module and one of the input is cluster_id.

This is the challenge, i need to get the cluster_id value from ecs.tf output value from parent folder

My app.tf file contains something like below

module "xyz" {
source = "some modudle which needs cluster_id as input"
cluster_id = ??
}

Help me with what i need to put for cluster_id

How can i use cluster_id from ecs.tf in CHILD\app.tf?

You can't access it directly. Your parent module must pass cluster_id as an input argument to your module, eg:

resource "aws_ecs_cluster" "first_cluster" {
  name = "firstCluster"
  capacity_providers = ["FARGATE"]
  default_capacity_provider_strategy {
    capacity_provider = "FARGATE"
    base              = 0
    weight            = 1
  }
  setting {
    name  = "containerInsights"
    value = "enabled"
  }
}

module "child" {
  source = "./CHILD"
  cluster_id = aws_ecs_cluster.first_cluster.id
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM