Skip to content

Data Source: awsVpcs

This resource can be useful for getting back a list of VPC Ids for a region.

The following example retrieves a list of VPC Ids with a custom tag of service set to a value of "production".

Example Usage

The following shows outputting all VPC Ids.

import * as cdktf from "cdktf";
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const dataAwsVpcsFoo = new aws.dataAwsVpcs.DataAwsVpcs(this, "foo", {
  tags: {
    service: "production",
  },
});
const cdktfTerraformOutputFoo = new cdktf.TerraformOutput(this, "foo_1", {
  value: dataAwsVpcsFoo.ids,
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
cdktfTerraformOutputFoo.overrideLogicalId("foo");

An example use case would be interpolate the awsVpcs output into count of an aws_flow_log resource.

import * as cdktf from "cdktf";
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const dataAwsVpcsFoo = new aws.dataAwsVpcs.DataAwsVpcs(this, "foo", {});
const cdktfTerraformOutputFoo = new cdktf.TerraformOutput(this, "foo_1", {
  value: dataAwsVpcsFoo.ids,
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
cdktfTerraformOutputFoo.overrideLogicalId("foo");
const dataAwsVpcFoo = new aws.dataAwsVpc.DataAwsVpc(this, "foo_2", {
  id: `\${tolist(${dataAwsVpcsFoo.ids})[count.index]}`,
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
dataAwsVpcFoo.overrideLogicalId("foo");
/*In most cases loops should be handled in the programming language context and 
not inside of the Terraform context. If you are looping over something external, e.g. a variable or a file input
you should consider using a for loop. If you are looping over something only known to Terraform, e.g. a result of a data source
you need to keep this like it is.*/
dataAwsVpcFoo.addOverride("count", `\${length(${dataAwsVpcsFoo.ids})}`);
const awsFlowLogTestFlowLog = new aws.flowLog.FlowLog(this, "test_flow_log", {
  vpcId: `\${${dataAwsVpcFoo.fqn}[count.index].id}`,
});
/*In most cases loops should be handled in the programming language context and 
not inside of the Terraform context. If you are looping over something external, e.g. a variable or a file input
you should consider using a for loop. If you are looping over something only known to Terraform, e.g. a result of a data source
you need to keep this like it is.*/
awsFlowLogTestFlowLog.addOverride("count", `\${length(${dataAwsVpcsFoo.ids})}`);

Argument Reference

  • tags - (Optional) Map of tags, each pair of which must exactly match a pair on the desired vpcs.

  • filter - (Optional) Custom filter block as described below.

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.

  • values - (Required) Set of values that are accepted for the given field. A VPC will be selected if any one of the given values matches.

Attributes Reference

  • id - AWS Region.
  • ids - List of all the VPC Ids found.

Timeouts

Configuration options:

  • read - (Default 20M)