Using count.index within a value to lookup resources already created

1,199 views
Skip to first unread message

Paul Schooss

unread,
Nov 17, 2014, 5:12:06 PM11/17/14
to terrafo...@googlegroups.com
Hello 

I am trying to use the count.index variable within a resource to use resources already created. I am using  "${concat("aws_subnet.public." , count.index, ".id}")}" as the subnet_id for a resource. However it doesn't really work very well. Since the interpolation of that is a string and not a reference to the resource's real id.


resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${concat(var.vpc_cidr_block_base, ".", count.index ,".0/24")}"
availability_zone = "${concat(var.region, lookup(var.vpc_availability_zone, concat("zone_", count.index)))}"
count = "${var.zone_count}"
depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

# Routing table for public subnets

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.default.id}"

route {
cidr_block = "0.0.0.0/0"
}
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

resource "aws_route_table_association" "public" {
subnet_id = "${concat("aws_subnet.public." , count.index, ".id}")}"
route_table_id = "${aws_route_table.public.id}"
count = "${var.zone_count}"
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

Paul Schooss

unread,
Nov 17, 2014, 5:16:37 PM11/17/14
to terrafo...@googlegroups.com
Answered my own question, was using the incorrect way of interpolation.


resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${concat(var.vpc_cidr_block_base, ".", count.index ,".0/24")}"
availability_zone = "${concat(var.region, lookup(var.vpc_availability_zone, concat("zone_", count.index)))}"
count = "${var.zone_count}"
depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

# Routing table for public subnets

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.default.id}"

route {
cidr_block = "0.0.0.0/0"
}
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

resource "aws_route_table_association" "public" {
subnet_id = "${aws_subnet.public.${count.index}.id}"
route_table_id = "${aws_route_table.public.id}"
count = "${var.zone_count}"
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

Paul Schooss

unread,
Nov 17, 2014, 7:46:13 PM11/17/14
to terrafo...@googlegroups.com
Ugh still not working....


module.vpc.aws_route_table_association.public.1: Creating...
  route_table_id: "" => "rtb-0668bf63"
  subnet_id:      "" => "${aws_subnet.public.1.id}"
module.vpc.aws_route_table_association.public.0: Creating...
  route_table_id: "" => "rtb-0668bf63"
  subnet_id:      "" => "${aws_subnet.public.0.id}"
module.vpc.aws_route_table_association.public.0: Error: The subnet ID '${aws_subnet.public.0.id}' does not exist (InvalidSubnetID.NotFound)


I think I am going to try this out, https://github.com/hashicorp/terraform/pull/554

Paul Schooss

unread,
Nov 18, 2014, 1:18:05 AM11/18/14
to terrafo...@googlegroups.com
Was able to compile a new version but ran into another issue. 

Error running plan: Resource 'aws_subnet.public' not found for variable 'aws_subnet.public.*.id'


using

resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${concat(var.vpc_cidr_block_base, ".", count.index ,".0/24")}"
availability_zone = "${concat(var.region, lookup(var.vpc_availability_zone, concat("zone_", count.index)))}"
count = "${var.zone_count}"
depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

# Routing table for public subnets

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.default.id}"

route {
cidr_block = "0.0.0.0/0"
}
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}

resource "aws_route_table_association" "public" {
subnet_id = "${element(aws_subnet.public.*.id,  count.index)}"
route_table_id = "${aws_route_table.public.id}"
count = "${var.zone_count}"
    depends_on = ["aws_vpc.default", "aws_internet_gateway.default"]
}
Reply all
Reply to author
Forward
0 new messages