Allow filtering of aws_subnet_ids by tags
This is the minimal amount of work needed to be able to create a list of a subset of subnet IDs in a VPC, allowing people to loop through them easily when creating EC2 instances or provide a list straight to an ELB.
This commit is contained in:
parent
42473d5129
commit
bc46b1cbf9
|
@ -12,10 +12,14 @@ func dataSourceAwsSubnetIDs() *schema.Resource {
|
||||||
return &schema.Resource{
|
return &schema.Resource{
|
||||||
Read: dataSourceAwsSubnetIDsRead,
|
Read: dataSourceAwsSubnetIDsRead,
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
|
|
||||||
|
"tags": tagsSchemaComputed(),
|
||||||
|
|
||||||
"vpc_id": &schema.Schema{
|
"vpc_id": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"ids": &schema.Schema{
|
"ids": &schema.Schema{
|
||||||
Type: schema.TypeSet,
|
Type: schema.TypeSet,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
@ -37,6 +41,10 @@ func dataSourceAwsSubnetIDsRead(d *schema.ResourceData, meta interface{}) error
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
req.Filters = append(req.Filters, buildEC2TagFilterList(
|
||||||
|
tagsFromMap(d.Get("tags").(map[string]interface{})),
|
||||||
|
)...)
|
||||||
|
|
||||||
log.Printf("[DEBUG] DescribeSubnets %s\n", req)
|
log.Printf("[DEBUG] DescribeSubnets %s\n", req)
|
||||||
resp, err := conn.DescribeSubnets(req)
|
resp, err := conn.DescribeSubnets(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -21,7 +21,8 @@ func TestAccDataSourceAwsSubnetIDs(t *testing.T) {
|
||||||
{
|
{
|
||||||
Config: testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt),
|
Config: testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
resource.TestCheckResourceAttr("data.aws_subnet_ids.selected", "ids.#", "1"),
|
resource.TestCheckResourceAttr("data.aws_subnet_ids.selected", "ids.#", "3"),
|
||||||
|
resource.TestCheckResourceAttr("data.aws_subnet_ids.private", "ids.#", "2"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -39,20 +40,50 @@ func testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_subnet" "test" {
|
resource "aws_subnet" "test_public_a" {
|
||||||
vpc_id = "${aws_vpc.test.id}"
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
cidr_block = "172.%d.123.0/24"
|
cidr_block = "172.%d.123.0/24"
|
||||||
availability_zone = "us-west-2a"
|
availability_zone = "us-west-2a"
|
||||||
|
|
||||||
tags {
|
tags {
|
||||||
Name = "terraform-testacc-subnet-ids-data-source"
|
Name = "terraform-testacc-subnet-ids-data-source-public-a"
|
||||||
|
Tier = "Public"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_subnet" "test_private_a" {
|
||||||
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
|
cidr_block = "172.%d.125.0/24"
|
||||||
|
availability_zone = "us-west-2a"
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "terraform-testacc-subnet-ids-data-source-private-a"
|
||||||
|
Tier = "Private"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_subnet" "test_private_b" {
|
||||||
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
|
cidr_block = "172.%d.126.0/24"
|
||||||
|
availability_zone = "us-west-2b"
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "terraform-testacc-subnet-ids-data-source-private-b"
|
||||||
|
Tier = "Private"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data "aws_subnet_ids" "selected" {
|
data "aws_subnet_ids" "selected" {
|
||||||
vpc_id = "${aws_vpc.test.id}"
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
}
|
}
|
||||||
`, rInt, rInt)
|
|
||||||
|
data "aws_subnet_ids" "private" {
|
||||||
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
|
tags {
|
||||||
|
Tier = "Private"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`, rInt, rInt, rInt, rInt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccDataSourceAwsSubnetIDsConfig(rInt int) string {
|
func testAccDataSourceAwsSubnetIDsConfig(rInt int) string {
|
||||||
|
@ -65,14 +96,37 @@ func testAccDataSourceAwsSubnetIDsConfig(rInt int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_subnet" "test" {
|
resource "aws_subnet" "test_public_a" {
|
||||||
vpc_id = "${aws_vpc.test.id}"
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
cidr_block = "172.%d.123.0/24"
|
cidr_block = "172.%d.123.0/24"
|
||||||
availability_zone = "us-west-2a"
|
availability_zone = "us-west-2a"
|
||||||
|
|
||||||
tags {
|
tags {
|
||||||
Name = "terraform-testacc-subnet-ids-data-source"
|
Name = "terraform-testacc-subnet-ids-data-source-public-a"
|
||||||
|
Tier = "Public"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`, rInt, rInt)
|
|
||||||
|
resource "aws_subnet" "test_private_a" {
|
||||||
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
|
cidr_block = "172.%d.125.0/24"
|
||||||
|
availability_zone = "us-west-2a"
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "terraform-testacc-subnet-ids-data-source-private-a"
|
||||||
|
Tier = "Private"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_subnet" "test_private_b" {
|
||||||
|
vpc_id = "${aws_vpc.test.id}"
|
||||||
|
cidr_block = "172.%d.126.0/24"
|
||||||
|
availability_zone = "us-west-2b"
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "terraform-testacc-subnet-ids-data-source-private-b"
|
||||||
|
Tier = "Private"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`, rInt, rInt, rInt, rInt)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,33 @@ output "subnet_cidr_blocks" {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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)}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Argument Reference
|
## Argument Reference
|
||||||
|
|
||||||
* `vpc_id` - (Required) The VPC ID that you want to filter from.
|
* `vpc_id` - (Required) The VPC ID that you want to filter from.
|
||||||
|
|
||||||
|
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
|
||||||
|
a pair on the desired subnets.
|
||||||
|
|
||||||
## Attributes Reference
|
## Attributes Reference
|
||||||
|
|
||||||
* `ids` - Is a list of all the subnet ids found. If none found. This data source will fail out.
|
* `ids` - Is a list of all the subnet ids found. If none found. This data source will fail out.
|
||||||
|
|
Loading…
Reference in New Issue