Data Source: awsSubnets
This resource can be useful for getting back a set of subnet IDs.
Example Usage
The following shows outputting all CIDR blocks for every subnet ID in a VPC.
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 dataAwsSubnetsExample = new aws.dataAwsSubnets.DataAwsSubnets(
this,
"example",
{
filter: [
{
name: "vpc-id",
values: ["${var.vpc_id}"],
},
],
}
);
const dataAwsSubnetExample = new aws.dataAwsSubnet.DataAwsSubnet(
this,
"example_1",
{
id: "${each.value}",
}
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
dataAwsSubnetExample.overrideLogicalId("example");
/*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.*/
dataAwsSubnetExample.addOverride(
"for_each",
`\${toset(${dataAwsSubnetsExample.ids})}`
);
new cdktf.TerraformOutput(this, "subnet_cidr_blocks", {
value: [`\${[for s in ${dataAwsSubnetExample} : s.cidr_block]}`],
});
The following example retrieves a set of all subnets in a VPC with a custom tag of tier
set to a value of "Private" so that the awsInstance
resource can loop through the subnets, putting instances across availability zones.
/*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 dataAwsSubnetsPrivate = new aws.dataAwsSubnets.DataAwsSubnets(
this,
"private",
{
filter: [
{
name: "vpc-id",
values: ["${var.vpc_id}"],
},
],
tags: {
Tier: "Private",
},
}
);
const awsInstanceApp = new aws.instance.Instance(this, "app", {
ami: "${var.ami}",
instanceType: "t2.micro",
subnetId: "${each.value}",
});
/*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.*/
awsInstanceApp.addOverride(
"for_each",
`\${toset(${dataAwsSubnetsPrivate.ids})}`
);
Argument Reference
filter
- (Optional) Custom filter block as described below.tags
- (Optional) Map of tags, each pair of which must exactly match a pair on the desired subnets.
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. For example, if matching against tagname
, 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.dataAwsSubnets.DataAwsSubnets(this, "selected", {
filter: [
{
name: "tag:Name",
values: [""],
},
],
});
values
- (Required) Set of values that are accepted for the given field. Subnet IDs will be selected if any one of the given values match.
Attributes Reference
ids
- List of all the subnet ids found.
Timeouts
read
- (Default20M
)