【问题标题】:Terraform lunch EC2 condition based on count基于计数的 Terraform 午餐 EC2 条件
【发布时间】:2021-09-22 14:38:37
【问题描述】:

根据条件(“单一目标”)尝试午餐 1 或 2 个实例

我得到错误:

132:   target_id        = var.ec2-2
var.ec2-2 is empty tuple
Inappropriate value for attribute "target_id": string required.

该变量的使用:

resource "aws_lb_target_group_attachment" "b" {
  target_id        = var.ec2-2

}

输出:

output "ec2-2_ot" {
 value = aws_instance.ec2V2[*].id
 description = "the value of the network module ec2-2 id"
}

变量:

variable "single-target" {
  type=bool
  default=false
}

main.tf:

module "network_module" {
   ec2-2 = module.compute_module.ec2-2_ot
}

ec2 启动:

resource "aws_instance" "ec2V2" {
  count = var.single-target ? 0 : 1
  ami                         = var.ec2_ami
  instance_type               = var.ec2_type
  associate_public_ip_address = true
  key_name = var.key_pair
  vpc_security_group_ids = [ var.vpc-sg ]
  subnet_id = var.subnet-2
  user_data = file("PATH/user-data.sh")
          
  tags = {
    Name = var.ec2-2_name
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws


    【解决方案1】:

    由于计数已设置为您尝试创建的资源,因此需要通过此 terraform 文档中提到的Splat Expression 访问返回的结果列表。

    如果您需要从结果列表中选择特定的索引,那么您可以使用element function 这样做

    因此,在您的情况下,如果您需要输出 EC2 id,如下所示。

    output "thisisoutput" {
           value = aws_instance.ec2V2[*].arn
    }
    

    【讨论】:

    • 嘿!把它改成你说的那样,问题是我在另一个模块中使用这个输出:ec2-2 = module.compute_module.ec2-2_ot 现在我得到:给定的值不适合子模块变量“ec2-2”定义在 ... 需要字符串
    • 您好,您是否介意在新问题中提出另一个问题。由于这篇文章中的问题已经得到解答。如果问题已根据答案解决,请随时接受答案。
    猜你喜欢
    • 2014-03-04
    • 2018-12-18
    • 1970-01-01
    • 2015-09-03
    • 2018-08-27
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多