Skip to content

Data Source: awsSubnet

awsSubnet provides details about a specific VPC subnet.

This resource can prove useful when a module accepts a subnet ID as an input variable and needs to, for example, determine the ID of the VPC that the subnet belongs to.

Example Usage

The following example shows how one might accept a subnet ID as a variable and use this data source to obtain the data necessary to create a security group that allows connections from hosts in that subnet.

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 subnetId = new cdktf.TerraformVariable(this, "subnet_id", {});
const dataAwsSubnetSelected = new aws.dataAwsSubnet.DataAwsSubnet(
  this,
  "selected",
  {
    id: subnetId.value,
  }
);
new aws.securityGroup.SecurityGroup(this, "subnet", {
  ingress: [
    {
      cidrBlocks: [dataAwsSubnetSelected.cidrBlock],
      fromPort: 80,
      protocol: "tcp",
      toPort: 80,
    },
  ],
  vpcId: dataAwsSubnetSelected.vpcId,
});

Filter Example

If you want to match against tag name, use:

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.dataAwsSubnet.DataAwsSubnet(this, "selected", {
  filter: [
    {
      name: "tag:Name",
      values: ["yakdriver"],
    },
  ],
});

Argument Reference

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

The following arguments are optional:

  • availabilityZone - (Optional) Availability zone where the subnet must reside.
  • availabilityZoneId - (Optional) ID of the Availability Zone for the subnet. This argument is not supported in all regions or partitions. If necessary, use availabilityZone instead.
  • cidrBlock - (Optional) CIDR block of the desired subnet.
  • defaultForAz - (Optional) Whether the desired subnet must be the default subnet for its associated availability zone.
  • filter - (Optional) Configuration block. Detailed below.
  • id - (Optional) ID of the specific subnet to retrieve.
  • ipv6CidrBlock - (Optional) IPv6 CIDR block of the desired subnet.
  • state - (Optional) State that the desired subnet must have.
  • tags - (Optional) Map of tags, each pair of which must exactly match a pair on the desired subnet.
  • vpcId - (Optional) ID of the VPC that the desired subnet belongs to.

filter

This block allows for complex filters. You can use one or more filter blocks.

The following arguments are required:

  • 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 subnet will be selected if any one of the given values matches.

Attributes Reference

In addition to the attributes above, the following attributes are exported:

  • arn - ARN of the subnet.
  • assignIpv6AddressOnCreation - Whether an IPv6 address is assigned on creation.
  • availableIpAddressCount - Available IP addresses of the subnet.
  • customerOwnedIpv4Pool - Identifier of customer owned IPv4 address pool.
  • enableDns64 - Whether DNS queries made to the Amazon-provided DNS Resolver in this subnet return synthetic IPv6 addresses for IPv4-only destinations.
  • enableResourceNameDnsAaaaRecordOnLaunch - Indicates whether to respond to DNS queries for instance hostnames with DNS AAAA records.
  • enableResourceNameDnsARecordOnLaunch - Indicates whether to respond to DNS queries for instance hostnames with DNS A records.
  • ipv6CidrBlockAssociationId - Association ID of the IPv6 CIDR block.
  • ipv6Native - Whether this is an IPv6-only subnet.
  • mapCustomerOwnedIpOnLaunch - Whether customer owned IP addresses are assigned on network interface creation.
  • mapPublicIpOnLaunch - Whether public IP addresses are assigned on instance launch.
  • outpostArn - ARN of the Outpost.
  • ownerId - ID of the AWS account that owns the subnet.
  • privateDnsHostnameTypeOnLaunch - The type of hostnames assigned to instances in the subnet at launch.

Timeouts

Configuration options:

  • read - (Default 20M)