Data Source: aws_subnet_ids#
Provides a list of subnet IDs for a VPC.
!> The aws_subnet_ids data source has been deprecated and will be removed in future versions.
Use the aws_subnets data source instead.
Example usage#
Specific examples#
The following shows all CIDR blocks for every subnet ID in a VPC.
variable vpc_id {}
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 Tier tag set to Private.
So the aws_instance resource can loop through the subnets, putting instances across availability zones.
variable vpc_id {}
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.example.ids)
ami = "cmi-12345678" # add image id, change instance type if needed
instance_type = "m1.micro"
subnet_id = each.value
}
For matching against tag Name, use:
data "aws_subnet_ids" "selected" {
filter {
name = "tag:Name"
values = [""] # insert values here
}
}
Argument reference#
The following arguments are required:
vpc_id- (Required, String) The VPC that has attached subnets to filter from.
The following arguments are optional:
filter- (Optional, Block) One or more name/value pairs to use as filters.- Valid values: See supported names and values in EC2 API documentation
tags- (Optional, Map of strings) Key-value pairs. Must exactly match pairs on the required resources.
filter#
name- (Required, String) The name of the filter.- Constraints: Filter names are case-sensitive
values- (Required, List of strings) One or more filter values.- Constraints: Filter values are case-sensitive
Attribute reference#
In addition to all arguments above, the following attribute is exported:
id- (String) The ID of the VPC.ids- (List of strings) The list of found subnet IDs.