This resource can be useful for getting back a set of subnet IDs.
The following shows outputting all CIDR blocks for every subnet ID in a VPC.
data "aws_subnets" "example" {
filter {
name = "vpc-id"
values = [var.vpc_id]
}
}
data "aws_subnet" "example" {
for_each = toset(data.aws_subnets.example.ids)
id = each.value
}
output "subnet_cidr_blocks" {
value = [for s in data.aws_subnet.example : s.cidr_block]
}
The following example retrieves a set of all subnets in a VPC with a custom
tag of Tier
set to a value of "Private" so that the aws_instance
resource
can loop through the subnets, putting instances across availability zones.
data "aws_subnets" "private" {
filter {
name = "vpc-id"
values = [var.vpc_id]
}
tags = {
Tier = "Private"
}
}
resource "aws_instance" "app" {
for_each = toset(data.aws_subnets.private.ids)
ami = var.ami
instance_type = "t2.micro"
subnet_id = each.value
}
filter
- (Optional) Custom filter block as described below.tags
- (Optional) Map of tags, each pair of which must exactly match
a pair on the desired subnets.More complex filters can be expressed using one or more filter
sub-blocks,
which take the following arguments:
name
- (Required) Name of the field to filter by, as defined by
the underlying AWS API.
For example, if matching against tag Name
, use:data "aws_subnets" "selected" {
filter {
name = "tag:Name"
values = [""] # insert values here
}
}
values
- (Required) Set of values that are accepted for the given field.
Subnet IDs will be selected if any one of the given values match.This data source exports the following attributes in addition to the arguments above:
ids
- List of all the subnet ids found.read
- (Default 20m
)