getSubnets function
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 pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";
const example = aws.ec2.getSubnets({
filters: [{
name: "vpc-id",
values: [vpcId],
}],
});
const exampleGetSubnet = example.then(example => std.toset({
input: example.ids,
})).then(invoke => .reduce((__obj, [__key, __value]) => ({ ...__obj, [String(__key)]: aws.ec2.getSubnet({
id: __value,
}) }), {}));
export const subnetCidrBlocks = exampleGetSubnet.apply(exampleGetSubnet => Object.values(exampleGetSubnet).map(s => (s.cidrBlock)));
import pulumi
import pulumi_aws as aws
import pulumi_std as std
example = aws.ec2.get_subnets(filters=[{
"name": "vpc-id",
"values": [vpc_id],
}])
example_get_subnet = {str(__key): aws.ec2.get_subnet(id=__value) for __key, __value in enumerate(std.toset(input=example.ids).result)}
pulumi.export("subnetCidrBlocks", [s.cidr_block for s in example_get_subnet.values()])
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
using Std = Pulumi.Std;
return await Deployment.RunAsync(() =>
{
var example = Aws.Ec2.GetSubnets.Invoke(new()
{
Filters = new[]
{
new Aws.Ec2.Inputs.GetSubnetsFilterInputArgs
{
Name = "vpc-id",
Values = new[]
{
vpcId,
},
},
},
});
var exampleGetSubnet = Std.Toset.Invoke(new()
{
Input = example.Apply(getSubnetsResult => getSubnetsResult.Ids),
}).Apply(invoke => );
return new Dictionary<string, object?>
{
["subnetCidrBlocks"] = exampleGetSubnet.Apply(exampleGetSubnet => (exampleGetSubnet).Values.Select(s =>
{
return s.CidrBlock;
}).ToList()),
};
});
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
std = {
source = "pulumi/std"
}
}
}
data "aws_ec2_getsubnets" "example" {
filters {
name = "vpc-id"
values = [vpcId]
}
}
data "aws_ec2_getsubnet" "invoke_1" {
for_each = toset(data.aws_ec2_getsubnets.example.ids)
id = each.value
}
locals {
exampleGetSubnet = {for __key, __value in toset(data.aws_ec2_getsubnets.example.ids) : __key => data.aws_ec2_getsubnet.invoke_1[__key]}
}
output "subnetCidrBlocks" {
value = [for s in local.exampleGetSubnet : s.cidrBlock]
}
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 aws.ec2.Instance resource
can loop through the subnets, putting instances across availability zones.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
export = async () => {
const _private = await aws.ec2.getSubnets({
filters: [{
name: "vpc-id",
values: [vpcId],
}],
tags: {
Tier: "Private",
},
});
const app: aws.ec2.Instance[] = [];
for (const range of Object.entries(.reduce((__obj, entry) => ({ ...__obj, [entry]: entry }), {})).sort().map(([k, v]) => ({key: k, value: v}))) {
app.push(new aws.ec2.Instance(`app-${range.key}`, {
ami: ami,
instanceType: aws.ec2.InstanceType.T2_Micro,
subnetId: range.value,
}));
}
}
import pulumi
from typing import Any
import pulumi_aws as aws
private = aws.ec2.get_subnets(filters=[{
"name": "vpc-id",
"values": [vpc_id],
}],
tags={
"Tier": "Private",
})
app: list[aws.ec2.Instance] = []
for app_range in [{"key": k, "value": v} for [k, v] in enumerate({entry: entry for entry in private.ids})]:
app.append(aws.ec2.Instance(f"app-{app_range['key']}",
ami=ami,
instance_type=aws.ec2.InstanceType.T2_MICRO,
subnet_id=app_range["value"]))
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(async() =>
{
var @private = await Aws.Ec2.GetSubnets.InvokeAsync(new()
{
Filters = new[]
{
new Aws.Ec2.Inputs.GetSubnetsFilterArgs
{
Name = "vpc-id",
Values = new[]
{
vpcId,
},
},
},
Tags =
{
{ "Tier", "Private" },
},
});
var app = new List<Aws.Ec2.Instance>();
foreach (var range in .Select(pair => new { pair.Key, pair.Value }))
{
app.Add(new Aws.Ec2.Instance($"app-{range.Key}", new()
{
Ami = ami,
InstanceType = Aws.Ec2.InstanceType.T2_Micro,
SubnetId = range.Value,
}));
}
});
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
data "aws_ec2_getsubnets" "private" {
filters {
name = "vpc-id"
values = [vpcId]
}
tags = {
"Tier" = "Private"
}
}
resource "aws_ec2_instance" "app" {
for_each = {for entry in data.aws_ec2_getsubnets.private.ids : entry => entry}
ami = ami
instance_type = "t2.micro"
subnet_id = each.value
}
resources:
app:
type: aws:ec2:Instance
properties:
ami: ${ami}
instanceType: t2.micro
subnetId: ${range.value}
options: {}
variables:
private:
fn::invoke:
function: aws:ec2:getSubnets
arguments:
filters:
- name: vpc-id
values:
- ${vpcId}
tags:
Tier: Private
args Arguments passed to this invoke. Arguments for getSubnets.
options Invoke options controlling this call.
Implementation
Future<GetSubnetsResult> getSubnets(
GetSubnetsArgs args, {
pulumi.InvokeOptions? options,
}) async {
final deployment = pulumi.Deployment.instance;
final result = await deployment.invoke<Map<String, dynamic>>(
'aws:ec2/getSubnets:getSubnets',
args.toMap(),
options: pulumi.toDeploymentInvokeOptions(options),
);
return GetSubnetsResult.fromMap(result);
}