AWS Launch Template with fixed subnet assignment - per availability zone

Hi Everyone,

I am trying to create a launch template, but it’s important to assign the network interfaces in a correct order and assign the subnets to those interfaces. If my autoscale group would only span across one single availability zone it would work. The problem comes when I want to deploy the autoscale group across different availability zones (For example “a” and “b”), however the launch_template only supports 1 particular subnet id or interface id to be assigned per instance. I was looking around everywhere but I couldn’t find any help around this. Appreciate if you have any ideas, suggestions. :slight_smile:

Example code of the module:

#Example way of receiving the subnet id - I use it based on name
data “aws_subnet” “subnet_mgmt” {

filter{
name = “tag:Name”
values = ["${var.subnet_mgmt_name}"]
}
}

#Launch template with fixed interfaces and subnets assigned per interface
resource “aws_launch_template” test_template" {
name_prefix = “{var.vpc_name}-test-" image_id = "123456789" instance_type = var.instance_size key_name = var.key_name tags = { Name = "{var.vpc_name}-${var.instance_name}”
}

user_data = base64encode(data.template_file.test-init.rendered)

network_interfaces {
device_index = 0
subnet_id = data.aws_subnet.subnet_mgmt.id
}

network_interfaces {
device_index = 1
subnet_id = data.aws_subnet.subnet_mgmt.id
}

network_interfaces {
device_index = 2
subnet_id = data.aws_subnet.subnet_data_1.id
}

network_interfaces {
device_index = 3
subnet_id = data.aws_subnet.subnet_data_2.id
}
}