我有一个资源块,在现有的VPC中创建了4个额外的子网:
# Create a subnet to launch instances into resource
"aws_subnet" "mesos_subnet" {
vpc_id = "${var.dcos_vpc}"
count = 4
cidr_block = "${cidrsubnet(var.subnet_range, 4, count.index)}"
availability_zone = "${element(split(",", lookup(var.azs, var.region)), count.index)}"
map_public_ip_on_launch = true
tags { "Name" = "${var.prefix}-${element(split(",", lookup(var.azs, var.region)),
count.index)}-sn" }
}
我正在使用像这样的资源集创建实例,它在可用的AZ(本区域中的4个)中分配实例:
resource "aws_instance" "agents" {
instance_type = "m4.2xlarge"
ami = "${lookup(var.amis, var.region)}"
count = "${var.agent_instance_count}"
key_name = "${var.key_name}"
vpc_security_group_ids = ["${aws_security_group.internal_sg.id}","${aws_security_group.admin_sg.id }"]
associate_public_ip_address = true
iam_instance_profile = "${aws_iam_instance_profile.agent_profile.name}"
availability_zone = "${element(split(",", lookup(var.azs, var.region)), count.index)}"
subnet_id = "${element(aws_subnet.dcos_subnet.*.id, count.index)}"
tags {
Name = "${var.prefix}-mesos-agent-${count.index}"
owner = "${var.owner}"
expiration = "${var.expiration}"
}
}
但是,在实例创建的子网选择器中,我似乎无法在具有匹配AZ的子网中创建实例,即 terraform尝试在子网中创建实例但与正确的AZ不匹配。所以我在申请时遇到了这个错误:
* aws_instance.agents.3: Error launching source instance: InvalidParameterValue: Value (eu-west-1a) for parameter availabilityZone is invalid. Subnet 'subnet-xxxxxx' is in the availability zone eu-west-1b
status code: 400, request id: 26665eee-071b-4ca1-a96b- 19b22c2fff94
* aws_instance.agents.4: Error launching source instance: InvalidParameterValue: Value (eu-west-1b) for parameter availabilityZone is invalid. Subnet 'subnet-yyyyyyyy' is in the availability zone eu-west- 1a
我想我需要一个select语句,它可以某种方式过滤掉并选择与AZ对应的子网ID(创建的子网) 选择在:
"availability_zone = "${element(split(",", lookup(var.azs, var.region)), count.index)}"".
有人知道我该怎么做吗?
答案 0 :(得分:1)
如果您只是分配subnet_id
,则实例将被放置到(该子网的)可用区域中。无需为subnet_id
资源分配availability_zone
和aws_instance
属性。两个属性都是可选的。只使用一个(子网)即可。