2

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/
├─ CHILD/
│  ├─ 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

Springhills
  • 360
  • 1
  • 5
  • 12

1 Answers1

-1

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, e.g.:

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
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    Hi Marcin, Thank you for the response. Now in my child, app.tf, how do i call this cluster_id ? app.tf calls another module which needs cluster_id as input module "xyz" { source = "someother module which needs cluster id" cluster_id = ?? } Can you please help me with what i need to put. – Springhills Apr 01 '22 at 01:14
  • @Springhills you have to keep passing it down in same way to further sub childs. – Marcin Apr 01 '22 at 01:57
  • Do you mind throwing snippet of code please How should the code look like ? – Springhills Apr 01 '22 at 03:10
  • @Springhills this is new issue thus new question should be asked. Also if my answer help you to overcome the original problem reported, it's acceptance would be appreciated. – Marcin Apr 01 '22 at 03:33