Terraform,如何将数据从一个子模块传递到另一个子模块

Terraform, how to pass data from one child module to another

我有以下文件夹结构

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

web-app 我创建了一个 SNS 主题。在 microservice 中,我想订阅我在那里创建的队列到我在 web-app 中创建的主题。 我的问题是我不知道如何将 arn of the topic 从 web-app 传递到 microservice1.

我怎样才能做到这一点?

您不能从 microservice1 执行此操作。您必须在 main.tf 中完成。所以首先你在 main.tf 中实例化一个 web-app 模块。该模块输出 sns arn。然后你将 arn 传递给 microservice 模块的瞬间,再次在 main.tf

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
}

要使其工作,您必须在 web-app:

中有输出
output "sns_arn" {
  value = aws_sns_topic.mytopic.id
}

/microservice1/variables.tf

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