amazon-web-services - terraform aws 将实例列表从一个模块传递到另一个模块

标签 amazon-web-services module terraform

我在 1 个模块中创建 2 个实例,现在我需要将这 2 个实例附加到使用另一个模块(同一文件)创建的 ELB - 如果不手动指定它们是否可行?

  module "instances" {
  source = "../../../../modules/ec2"

  ami                         = "ami...."
  number_of_instances         = 2
  instance_type               = "t2.micro"
}

  module "elb" {
  source = "../../../../modules//elb"

  name = "some elb"
  instances = ["???"] #something like ["${module.ec2.instances.id}"]?
}

最佳答案

首先,在您的 ec2 模块中定义输出:

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

注意:资源名称web 是一个例子。请在模块中指定实际的资源名称。

接下来,在您的 elb 模块中声明 list 变量:

variable "instances" {
  type = "list"
}

最后将ec2模块的输出传递给elb模块:

module "instances" {
  source = "../../../../modules/ec2"

  ami                         = "ami...."
  number_of_instances         = 2
  instance_type               = "t2.micro"
}

module "elb" {
  source = "../../../../modules/elb"

  name = "some elb"
  instances = ["${module.instances.instance_ids}"]
}

关于amazon-web-services - terraform aws 将实例列表从一个模块传递到另一个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850441/

相关文章:

mysql - 不同 VPC 上的 AWS RDS 只读副本

php - K2_content 模块评级

c++ - 子模块 : how to divide C++ module into submodules

azure - 每个 VM/NIC 的 Terraform Azure

google-cloud-platform - Terraformer 合并多个 tfstate 文件

terraform - 在 variavles.tf 中为 type = map(object()) 添加默认字段

ios - 对适用于 iOS 的 dynamoDB (AWS) 执行扫描或查询

amazon-web-services - 旧 Lambda 函数是使用新 Lambda 函数更新现有 API Gateway 后触发的函数

amazon-web-services - 如何更新 DynamoDB 中的嵌套字段?

python - 如何从子目录运行 Python 脚本?