Better use lists of subnets to reduce the amount of variables. Then you can also use count = length(var.subnets) to get 2 instances of the route table association resource and pick the correct one from the subnets list.
variable "subnet_cidrs_public" { description = "Subnet CIDRs for public subnets (length must match configured availability_zones)" # this could be further simplified / computed using cidrsubnet() etc. # https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum- default = ["10.0.10.0/24", "10.0.20.0/24"] type = "list" } resource "aws_subnet" "public" { count = "${length(var.subnet_cidrs_public)}" vpc_id = "${aws_vpc.main.id}" cidr_block = "${var.subnet_cidrs_public[count.index]}" availability_zone = "${var.availability_zones[count.index]}" } resource "aws_route_table_association" "public" { count = "${length(var.subnet_cidrs_public)}" subnet_id = "${element(aws_subnet.public.*.id, count.index)}" route_table_id = "${aws_route_table.public.id}" }
I see you've been reading availability zones via data, which is fine and you can still do. You just have to somehow set the association between a subnet and the AZ. I leave that up to you.
Certainly more elegant would be to provision a subnet in every AZ of that region. Once we use cidrsubnet() to compute address spaces for the subnets, we could use length(data.availability_zones) as the driver for all the rest. Shouldn't be too complex.
Here is the full code:
provider "aws" { region = "eu-west-1" } variable "availability_zones" { description = "AZs in this region to use" default = ["eu-west-1a", "eu-west-1c"] type = "list" } variable "vpc_cidr" { default = "10.0.0.0/16" } variable "subnet_cidrs_public" { description = "Subnet CIDRs for public subnets (length must match configured availability_zones)" # this could be further simplified / computed using cidrsubnet() etc. # https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum- default = ["10.0.10.0/24", "10.0.20.0/24"] type = "list" } resource "aws_vpc" "main" { cidr_block = "${var.vpc_cidr}" tags { Name = "stackoverflow-51739482" } } resource "aws_subnet" "public" { count = "${length(var.subnet_cidrs_public)}" vpc_id = "${aws_vpc.main.id}" cidr_block = "${var.subnet_cidrs_public[count.index]}" availability_zone = "${var.availability_zones[count.index]}" } resource "aws_route_table" "public" { vpc_id = "${aws_vpc.main.id}" tags { Name = "public" } } resource "aws_route_table_association" "public" { count = "${length(var.subnet_cidrs_public)}" subnet_id = "${element(aws_subnet.public.*.id, count.index)}" route_table_id = "${aws_route_table.public.id}" }
count = length(var.subnets)to get 2 instances of the route table association resource and pick the correct one from the subnets list.