[英]Terraform lunch EC2 condition based on count
根據條件(“單一目標”)嘗試午餐 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
}
主文件:
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
}
}
由於計數已設置為您嘗試創建的資源,因此需要通過此 terraform 文檔中提到的Splat 表達式訪問返回結果列表。
如果您需要從結果列表中選擇特定的索引,那么您可以使用element 函數來做到這一點。
因此,在您的情況下,如果您需要輸出 EC2 id,則如下所示。
output "thisisoutput" {
value = aws_instance.ec2V2[*].arn
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.