diff --git a/aws/data_source_aws_vpc_peering_connections.go b/aws/data_source_aws_vpc_peering_connections.go new file mode 100644 index 00000000000..534d3935ccf --- /dev/null +++ b/aws/data_source_aws_vpc_peering_connections.go @@ -0,0 +1,66 @@ +package aws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" +) + +func dataSourceAwsVpcPeeringConnections() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsVpcPeeringConnectionsRead, + + Schema: map[string]*schema.Schema{ + "filter": ec2CustomFiltersSchema(), + "tags": tagsSchemaComputed(), + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func dataSourceAwsVpcPeeringConnectionsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + req := &ec2.DescribeVpcPeeringConnectionsInput{} + + req.Filters = append(req.Filters, buildEC2TagFilterList( + keyvaluetags.New(d.Get("tags").(map[string]interface{})).Ec2Tags(), + )...) + req.Filters = append(req.Filters, buildEC2CustomFilterList( + d.Get("filter").(*schema.Set), + )...) + if len(req.Filters) == 0 { + // Don't send an empty filters list; the EC2 API won't accept it. + req.Filters = nil + } + + resp, err := conn.DescribeVpcPeeringConnections(req) + if err != nil { + return err + } + if resp == nil || len(resp.VpcPeeringConnections) == 0 { + return fmt.Errorf("no matching VPC peering connections found") + } + + var ids []string + for _, pcx := range resp.VpcPeeringConnections { + ids = append(ids, aws.StringValue(pcx.VpcPeeringConnectionId)) + } + + d.SetId(resource.UniqueId()) + err = d.Set("ids", ids) + if err != nil { + return err + } + + return nil +} diff --git a/aws/data_source_aws_vpc_peering_connections_test.go b/aws/data_source_aws_vpc_peering_connections_test.go new file mode 100644 index 00000000000..5358966c7a3 --- /dev/null +++ b/aws/data_source_aws_vpc_peering_connections_test.go @@ -0,0 +1,80 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceAwsVpcPeeringConnections_basic(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsVpcPeeringConnectionsConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_vpc_peering_connections.test_by_filters", "ids.#", "2"), + ), + }, + }, + }) +} + +const testAccDataSourceAwsVpcPeeringConnectionsConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" + + tags = { + Name = "terraform-testacc-vpc-peering-connection-data-source-foo" + Type = "primary" + } +} + +resource "aws_vpc" "bar" { + cidr_block = "10.2.0.0/16" + + tags = { + Name = "terraform-testacc-vpc-peering-connection-data-source-bar" + Type = "secondary" + } +} + +resource "aws_vpc" "baz" { + cidr_block = "10.3.0.0/16" + + tags = { + Name = "terraform-testacc-vpc-peering-connection-data-source-baz" + Type = "secondary" + } +} + +resource "aws_vpc_peering_connection" "conn1" { + vpc_id = aws_vpc.foo.id + peer_vpc_id = aws_vpc.bar.id + auto_accept = true + + tags = { + Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-bar" + Environment = "test" + } +} + +resource "aws_vpc_peering_connection" "conn2" { + vpc_id = aws_vpc.foo.id + peer_vpc_id = aws_vpc.baz.id + auto_accept = true + + tags = { + Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-baz" + Environment = "test" + } +} + +data "aws_vpc_peering_connections" "test_by_filters" { + filter { + name = "vpc-peering-connection-id" + values = [aws_vpc_peering_connection.conn1.id, aws_vpc_peering_connection.conn2.id] + } +} +` diff --git a/aws/provider.go b/aws/provider.go index 34e5c292584..28e4e4627d1 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -338,6 +338,7 @@ func Provider() *schema.Provider { "aws_vpc_endpoint": dataSourceAwsVpcEndpoint(), "aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(), "aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(), + "aws_vpc_peering_connections": dataSourceAwsVpcPeeringConnections(), "aws_vpn_gateway": dataSourceAwsVpnGateway(), "aws_waf_ipset": dataSourceAwsWafIpSet(), "aws_waf_rule": dataSourceAwsWafRule(), @@ -353,7 +354,6 @@ func Provider() *schema.Provider { "aws_wafv2_web_acl": dataSourceAwsWafv2WebACL(), "aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(), "aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(), - // Adding the Aliases for the ALB -> LB Rename "aws_lb": dataSourceAwsLb(), "aws_alb": dataSourceAwsLb(), diff --git a/website/docs/d/vpc_peering_connections.html.markdown b/website/docs/d/vpc_peering_connections.html.markdown new file mode 100644 index 00000000000..6b1a89009b3 --- /dev/null +++ b/website/docs/d/vpc_peering_connections.html.markdown @@ -0,0 +1,57 @@ +--- +subcategory: "VPC" +layout: "aws" +page_title: "AWS: aws_vpc_peering_connections" +description: |- + Lists peering connections +--- + +# Data Source: aws_vpc_peering_connections + +Use this data source to get IDs of Amazon VPC peering connections +To get more details on each connection, use the data resource [aws_vpc_peering_connection](/docs/providers/aws/d/vpc_peering_connection.html) + +Note: To use this data source in a count, the resources should exist before trying to access +the data source, as noted in [issue 4149](https://github.com/hashicorp/terraform/issues/4149) + +## Example Usage + +```hcl +# Declare the data source +data "aws_vpc_peering_connections" "pcs" { + filter { + name = "requester-vpc-info.vpc-id" + values = [aws_vpc.foo.id] + } +} + +# get the details of each resource +data "aws_vpc_peering_connection" "pc" { + count = length(data.aws_vpc_peering_connections.pcs.ids) + id = data.aws_vpc_peering_connections.pcs.ids[count.index] +} +``` + +## Argument Reference + +The arguments of this data source act as filters for querying the available VPC peering connections. + +* `filter` - (Optional) Custom filter block as described below. + +* `tags` - (Optional) A mapping of tags, each pair of which must exactly match + a pair on the desired VPC Peering Connection. + +More complex filters can be expressed using one or more `filter` sub-blocks, +which take the following arguments: + +* `name` - (Required) The name of the field to filter by, as defined by + [the underlying AWS API](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcPeeringConnections.html). + +* `values` - (Required) Set of values that are accepted for the given field. + A VPC Peering Connection will be selected if any one of the given values matches. + +## Attributes Reference + +All of the argument attributes except `filter` are also exported as result attributes. + +* `ids` - The IDs of the VPC Peering Connections.