Skip to content

Data Source: awsVpc

awsVpc provides details about a specific VPC.

This resource can prove useful when a module accepts a vpc id as an input variable and needs to, for example, determine the CIDR block of that VPC.

Example Usage

The following example shows how one might accept a VPC id as a variable and use this data source to obtain the data necessary to create a subnet within it.

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";
/*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
You can read more about this at https://cdk.tf/variables*/
const vpcId = new cdktf.TerraformVariable(this, "vpc_id", {});
const dataAwsVpcSelected = new aws.dataAwsVpc.DataAwsVpc(this, "selected", {
  id: vpcId.value,
});
new aws.subnet.Subnet(this, "example", {
  availabilityZone: "us-west-2a",
  cidrBlock: `\${cidrsubnet(${dataAwsVpcSelected.cidrBlock}, 4, 1)}`,
  vpcId: dataAwsVpcSelected.id,
});

Argument Reference

The arguments of this data source act as filters for querying the available VPCs in the current region. The given filters must match exactly one VPC whose data will be exported as attributes.

  • cidrBlock - (Optional) Cidr block of the desired VPC.

  • dhcpOptionsId - (Optional) DHCP options id of the desired VPC.

  • default - (Optional) Boolean constraint on whether the desired VPC is the default VPC for the region.

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

  • id - (Optional) ID of the specific VPC to retrieve.

  • state - (Optional) Current state of the desired VPC. Can be either "pending" or "available".

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

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

All of the argument attributes except filter blocks are also exported as result attributes. This data source will complete the data by populating any fields that are not included in the configuration with the data for the selected VPC.

The following attribute is additionally exported:

  • arn - ARN of VPC
  • enableDnsSupport - Whether or not the VPC has DNS support
  • enableNetworkAddressUsageMetrics - Whether Network Address Usage metrics are enabled for your VPC
  • enableDnsHostnames - Whether or not the VPC has DNS hostname support
  • instanceTenancy - Allowed tenancy of instances launched into the selected VPC. May be any of "default", "dedicated", or "host".
  • ipv6AssociationId - Association ID for the IPv6 CIDR block.
  • ipv6CidrBlock - IPv6 CIDR block.
  • mainRouteTableId - ID of the main route table associated with this VPC.
  • ownerId - ID of the AWS account that owns the VPC.

cidrBlockAssociations is also exported with the following attributes:

  • associationId - Association ID for the IPv4 CIDR block.
  • cidrBlock - CIDR block for the association.
  • state - State of the association.

Timeouts

Configuration options:

  • read - (Default 20M)