子网是从下面的代码创建的:
resource "aws_subnet" "Private" {
for_each = var.subnet_cidrs_private
vpc_id = aws_vpc.vpc.id
cidr_block = each.value
availability_zone = each.key
tags = {
Name = format("%s-%s-%s", var.environment, "Pri-Subnet", substr(each.key, -2, -1))
}
}
尝试创建一个带有count参数的实例。
resource "aws_instance" "visualapp" {
instance_type = "t2.micro"
ami = var.visualapp-ami
key_name = var.key_pair
vpc_security_group_ids = [aws_security_group.visualapp.id]
subnet_id = element(aws_subnet.Private[*].id, count.index)
count = 1
tags = {
Name = format("%s-%s", var.environment, "mage-varnish-${count.index + 1}")
}
}
得到以下错误:
subnet_id = element(aws_subnet.Private[*].id, count.index)
This object does not have an attribute named "id".
变量:
variable "subnet_cidrs_private" {
default = {
eu-west-1a = "172.26.3.0/24",
eu-west-1b = "172.26.4.0/24"
}
}
有人能帮我吗?
如果使用count
,则使用element
。既然您已经使用了for_each
,那么您需要先使用values
:
subnet_id = element(values(aws_subnet.Private)[*].id, count.index)