【问题标题】:Assign multi instances to lb group将多个实例分配给 lb 组
【发布时间】:2021-12-17 16:04:17
【问题描述】:

我正在使用 terraform 将实例分配给 lb_target_group。

我有 3 个服务器,分别命名为: test-1 、 test-2 、 test-3 - 这些服务器是从一个模块创建的,我做了一个输出

下面是使用单个实例的代码:test-1

我的目标是包含所有 3 台服务器

** 服务器输出 **

output "test-1-id" {
  value = module.test-1.tps-vm-instance-id
}
output "test-2-id" {
  value = module.test-2.tps-vm-instance-id
}
output "test-3-id" {
  value = module.test-3.tps-vm-instance-id
}

工作代码

resource "aws_lb_target_group" "test_node_instances_group" {
  name     = "test-node-instances-group"
  port     = 443
  protocol = "HTTPS"
  vpc_id   = module.vpc.vpc-id
}

resource "aws_lb_target_group_attachment" "test_node_instances" {
  target_group_arn = aws_lb_target_group.test_node_instances_group.arn
  target_id        = module.test.test-1-id
  port             = 443

不适合我的代码

resource "aws_lb_target_group" "test_node_instances_group" {
  name     = "test-node-instances-group"
  port     = 443
  protocol = "HTTPS"
  vpc_id   = module.vpc.vpc-id
}

resource "aws_lb_target_group_attachment" "test_node_instances" {
  count = 2
  target_group_arn = aws_lb_target_group.test_node_instances_group.arn
  target_id        = module.test.test-[count.index]-id
  port             = 443

感谢您的建议

【问题讨论】:

  • aws_instance.test-1-id的定义是什么?

标签: terraform terraform-provider-aws


【解决方案1】:

不知道更多,我您的问题出在这一行上(尽管一些错误消息或其他“不工作”的摘要会很好,否则我们不知道问题是什么) :

  target_id        = module.test.test-[count.index]-id

这似乎不是有效的 Terraform 语法。

相反,您可以使用要在此处使用的 id 定义一个列表或映射,例如像这样的东西(未测试):

locals {
 ids_of_instances = [module.test.test-1-id, module.test.test-2-id, module.test.test-3-id]
}

然后像这样使用它:

  target_id        = local.ids_of_instances[count.index]

另外 - 不知道为什么你有count = 2 而不是3

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2018-03-19
    • 2012-11-15
    相关资源
    最近更新 更多