Data Source: awsSubnetIds
awsSubnetIds
provides a set of ids for a vpc_id
This resource can be useful for getting back a set of subnet ids for a vpc.
\~> NOTE: The awsSubnetIds
data source has been deprecated and will be removed in a future version. Use the awsSubnets
data source instead.
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 dataAwsSubnetIdsExample = new aws.dataAwsSubnetIds.DataAwsSubnetIds(
this,
"example",
{
vpcId: "${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", dataAwsSubnetIdsExample.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 dataAwsSubnetIdsPrivate = new aws.dataAwsSubnetIds.DataAwsSubnetIds(
this,
"private",
{
tags: {
Tier: "Private",
},
vpcId: "${var.vpc_id}",
}
);
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", dataAwsSubnetIdsPrivate.ids);
Argument Reference
-
vpcId
- (Required) VPC ID that you want to filter from. -
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.dataAwsSubnetIds.DataAwsSubnetIds(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
- Set of all the subnet ids found. This data source will fail if none are found.
Timeouts
read
- (Default20M
)