main.tf
resource "aws_vpc" "main" {
cidr_block = "${var.main_cidr}"
enable_dns_hostnames = true
}
resource "aws_subnet" "main" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "${var.main_subnet.cidrs[count.index]}"
availability_zone = "${var.main_subnet.availability_zones[count.index]}"
map_public_ip_on_launch = true
count = "${length(var.main_subnet.cidrs)}"
}
variable "main_cidr" {
description = "AWS vpc main cidr"
}
variable "main_subnet_cidrs" {
type = "list"
}
variable "main_subnet_availability_zones" {
type = "list"
}
terraform.tfvars
main_cidr = "172.17.0.0/16"
main_subnet.cidrs = ["172.17.0.0/20","172.17.16.0/20"]
main_subnet.availability_zones = ["us-east-1b","us-east-1e"]
これでvpc_subnetを参照するようなときに一つの値で記述できます。
# aws_elbのsubnetsとか
subnets = ["${aws_subnet.main.*.id}"]
# autoscaling_groupのvpc_zone_identifierとか
vpc_zone_identifier = ["${aws_subnet.main.*.id}"]
# countを使ったインスタンスのsubnetを交互に選択させるとか
subnet_id = "${element(aws_subnet.main.*.id, count.index%length(aws_subnet.main.*.id))}"