Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass subnet ids to aws_network_interface which has count attribute

I am getting the 3 existing subnet ids using data resource as below

data "aws_subnet" "AZA" {
  vpc_id = var.vpc_id

 filter {
    name   = "tag:Name"
    values = ["my-subnet-1a"]
  }

}

Similarly I’m getting AZB and AZC as well. Now I need to pass these subnet ids to aws_network_interface resource which has count attribute.

resource "aws_network_interface" "my_network_interface" {
    count          = 3
    private_ips     = [var.private_ips[count.index]]
    subnet_id       = ???

How do I pass subnet_id in each iteration?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The better way is to use aws_subnets (not aws_subnet):

data "aws_subnets" "AZ" {
  filter {
    name   = "tag:Name"
    values = ["my-subnet-1a", "my-subnet-1b", "my-subnet-1c"] 
  }
}

then

resource "aws_network_interface" "my_network_interface" {
    count          = 3
    private_ips     = [var.private_ips[count.index]]
    subnet_id       = data.aws_subnets.AZ.ids[count.index]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading