amazon-web-services - 在 terraform 的另一个模块中使用一个模块局部变量

标签 amazon-web-services amazon-cloudwatch terraform

我正在尝试访问另一个新模块中的一个模块变量以获取在该模块中创建的 aws 实例 id,并在模块 Cloud watch alerts 中使用它们,该模块在这些实例 id 中创建警报。结构类似于下面

**Amodule #here this is used for creating kafka aws instances*

     main.tf
     kafkainstancevariables.tf

Bmodule #here 这用于在这些 kafka 实例中创建警报
     main.tf
     cloudwatchalertsforkafkainstancesVariables.tf

外部模块 terraform mainfile 从中调用所有模块
主文件
变量.tf***

如何在 Bmodule 中访问 Amodule 中创建的变量?

谢谢!

最佳答案

您可以使用outputs来实现这一点。在您的 kafka 模块中,您可以定义如下所示的输出:

output "instance_ids" {
  value = ["${aws_instance.kafka.*.id}"]
}

在另一个 terraform 文件中,假设您使用以下内容实例化了模块:
module "kafka" {
  source = "./modules/kafka"
}

然后,您可以按如下方式访问该输出:
instances = ["${module.kafka.instance_ids}"]

如果您的模块彼此隔离(即您的 cloudwatch 模块没有实例化您的 kafka 模块),您可以将输出作为模块之间的变量传递:
module "kafka" {
  source = "./modules/kafka"
}

module "cloudwatch" {
  source = "./modules/cloudwatch"
  instances = ["${module.kafka.instance_ids}"]
}

当然,您的“cloudwatch”模块必须声明 instances variable .

https://www.terraform.io/docs/modules/usage.html#outputs有关在模块中使用输出的更多信息。

关于amazon-web-services - 在 terraform 的另一个模块中使用一个模块局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41042096/

相关文章:

amazon-web-services - 验证错误: The specified value for roleName is invalid

amazon-web-services - 在AWS EMR中连接到Google NGrams数据

amazon-web-services - CDK 将映射模板添加到 LambdaIntegration

amazon-web-services - CloudWatch 警报和 CloudWatch 事件有什么区别?

amazon-web-services - 我可以使用自定义 CloudWatch 指标做什么?

Terraform 挂起且没有输出

amazon-web-services - 可以将环境变量与 sub 内部函数一起使用吗?

c++ - 如何等待PooledThreadExecutor完成?

amazon-web-services - CloudFormation AWS::Events::Rule:遇到不受支持的属性 ID

azure - 如何使用 Terraform AzureRm 在同一管理规则中包含多个地址前缀