2017-03-31 20:21:44 +02:00
|
|
|
---
|
|
|
|
layout: "aws"
|
|
|
|
page_title: "AWS: aws_subnet_ids"
|
|
|
|
sidebar_current: "docs-aws-datasource-subnet-ids"
|
|
|
|
description: |-
|
|
|
|
Provides a list of subnet Ids for a VPC
|
|
|
|
---
|
|
|
|
|
|
|
|
# aws\_subnet\_ids
|
|
|
|
|
|
|
|
`aws_subnet_ids` provides a list of ids for a vpc_id
|
|
|
|
|
|
|
|
This resource can be useful for getting back a list of subnet ids for a vpc.
|
|
|
|
|
|
|
|
## Example Usage
|
|
|
|
|
|
|
|
The following shows outputing all cidr blocks for every subnet id in a vpc.
|
|
|
|
|
2017-04-07 17:54:28 +02:00
|
|
|
```hcl
|
2017-03-31 20:21:44 +02:00
|
|
|
data "aws_subnet_ids" "example" {
|
|
|
|
vpc_id = "${var.vpc_id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_subnet" "example" {
|
|
|
|
count = "${length(data.aws_subnet_ids.example.ids)}"
|
2017-05-02 16:05:50 +02:00
|
|
|
id = "${data.aws_subnet_ids.example.ids[count.index]}"
|
2017-03-31 20:21:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output "subnet_cidr_blocks" {
|
|
|
|
value = ["${data.aws_subnet.example.*.cidr_block}"]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-04-25 15:46:51 +02:00
|
|
|
The following example retrieves a list 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.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
data "aws_subnet_ids" "private" {
|
|
|
|
vpc_id = "${var.vpc_id}"
|
|
|
|
tags {
|
|
|
|
Tier = "Private"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "app" {
|
|
|
|
count = "3"
|
|
|
|
ami = "${var.ami}"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
subnet_id = "${element(data.aws_subnet_ids.private.ids, count.index)}"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-03-31 20:21:44 +02:00
|
|
|
## Argument Reference
|
|
|
|
|
|
|
|
* `vpc_id` - (Required) The VPC ID that you want to filter from.
|
|
|
|
|
2017-04-25 15:46:51 +02:00
|
|
|
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
|
|
|
|
a pair on the desired subnets.
|
|
|
|
|
2017-03-31 20:21:44 +02:00
|
|
|
## Attributes Reference
|
|
|
|
|
|
|
|
* `ids` - Is a list of all the subnet ids found. If none found. This data source will fail out.
|