Home > Software engineering >  Terraform, how to pass data from one child module to another
Terraform, how to pass data from one child module to another

Time:03-08

I have the following folder structure

├── main.tf
├── modules
│   ├── web-app
│   │   ├── lambda.tf
│   │   ├── outputs.tf
│   │   ├── s3.tf
│   │   ├── sns.tf
│   │   ├── sqs.tf
│   │   └── variables.tf
│   ├── microservice1
│   │   ├── sns.tf
│   │   ├── sqs.tf
│   │   └── variables.tf
...

Inside the web-app I create a SNS topic. Inside the microservice I want to subscribe the queue I create there to the topic I created inside the web-app. My problem is that I cannot figure out how to pass the arn of the topic from web-app to the microservice1.

How could I achieve that?

CodePudding user response:

You can't do this from microservice1. You have to do it in your main.tf. So first you instantiate a web-app module in the main.tf. The module outputs sns arn. Then you pass the arn to the instant of microservice module, again in main.tf

In main.tf:

module "web-app" {
   source = "./modules/web-app"
   # other arguments
}

module "microservice1" {
   source = "./modules/microservice1"
   sns_arn = module.web-app.sns_arn
   # other arguments
}

for that to work, you have to have output in your web-app:

output "sns_arn" {
  value = aws_sns_topic.mytopic.id
}

/microservice1/variables.tf

variable "sns_arn" {
  description="topic arn from web_app"
}
  • Related