簡體   English   中英

terraform中子模塊如何調用父資源output

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

我正在創建一個下面

ABC -- ecs.tf(給我集群 ID)

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
}

現在在作為根文件夾的 ABC 文件夾下,我有另一個帶有 CHILD 文件夾的文件夾,其中有 app.tf

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

問題:如何在 CHILD\app.tf 中使用 ecs.tf 中的 cluster_id?

應用程序.tf:

  • 此文件中的模塊已經在調用不同的模塊,輸入之一是 cluster_id。

這是挑戰,我需要從父文件夾的 ecs.tf output 值中獲取 cluster_id 值

我的 app.tf 文件包含如下內容

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

幫我解決我需要為 cluster_id 添加的內容

我如何在 CHILD\app.tf 中使用 ecs.tf 中的 cluster_id?

您無法直接訪問它。 您的父模塊必須將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