LoginSignup
9
9

More than 5 years have passed since last update.

terraformで複数のvpc_subnetを一つにまとめる

Last updated at Posted at 2015-08-07
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))}"
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9