Automatically populating subnet ids in aws_elasticache_subnet_group from variable?

Hi,

Wondering if anyone can help me populate the aws_elasticache_subnet_group subnets automatically?

At the moment I’ve added in the subnet IDs manually but would like them to be generated automatically in order to scale it. I’ve already done similar to create subnets in each of the availability zones but not been able to get it right to pull it into the subnet group.

Here’s my config that I’m using to deploy a elasticache redis instance. This works successfully, it’s just that it’s manual on the subnet group!

Thanks

Here’s the variables used:

variable “vpc_subnet_ips” {
type=list
default = [
“172.31.0.0/24”,
“172.31.1.0/24”,
“172.31.2.0/24”,
“172.31.3.0/24”,
“172.31.4.0/24”,
“172.31.5.0/24”
]
}

variable “vpc_subnet_azs” {
type=list
default = [
“us-east-1a”,
“us-east-1b”,
“us-east-1c”,
“us-east-1d”,
“us-east-1e”,
“us-east-1f”
]
}

The resource definitions:

resource “aws_subnet” “subnets” {
count = length(var.vpc_subnet_ips)
vpc_id = aws_vpc.experfy.id
cidr_block = element(var.vpc_subnet_ips, count.index)
availability_zone = element(var.vpc_subnet_azs, count.index)
map_public_ip_on_launch = true
tags = {
Name = “{var.projectname}{element(var.vpc_subnet_names, count.index)}”
Environment = var.environment_tag
}
}

resource “aws_elasticache_subnet_group” “elasticacheredissubnet” {
name = “${var.projectname}-elasticacheredissubnet”
subnet_ids = [
“subnet-0eb049d54fe5903dc”,
“subnet-0b4110578a0b983f4”,
“subnet-02ee6edfb01fdad90”,
“subnet-0366616181c69a302”,
“subnet-03c8c6607240bdf36”,
“subnet-0f8fb736a95f708ac”
]
}