简体   繁体   English

terraform中子模块如何调用父资源output

[英]How to call parent resource output in child module in terraform

I am creating a below我正在创建一个下面

ABC -- ecs.tf (gives me cluster id) ABC -- ecs.tf(给我集群 ID)

Content of ecs.tf: 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 文件夹下,我有另一个带有 CHILD 文件夹的文件夹,其中有 app.tf

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

Question: How can i use cluster_id from ecs.tf in CHILD\app.tf?问题:如何在 CHILD\app.tf 中使用 ecs.tf 中的 cluster_id?

app.tf:应用程序.tf:

  • module in this file is already calling different module and one of the input is cluster_id.此文件中的模块已经在调用不同的模块,输入之一是 cluster_id。

This is the challenge, i need to get the cluster_id value from ecs.tf output value from parent folder这是挑战,我需要从父文件夹的 ecs.tf output 值中获取 cluster_id 值

My app.tf file contains something like below我的 app.tf 文件包含如下内容

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

Help me with what i need to put for cluster_id帮我解决我需要为 cluster_id 添加的内容

How can i use cluster_id from ecs.tf in CHILD\app.tf?我如何在 CHILD\app.tf 中使用 ecs.tf 中的 cluster_id?

You can't access it directly.您无法直接访问它。 Your parent module must pass cluster_id as an input argument to your module, eg:您的父模块必须将cluster_id作为输入参数传递给您的模块,例如:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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