getSubnet

aws.ec2.Subnet 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

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var config = new Config();
        var subnetId = config.RequireObject<dynamic>("subnetId");
        var selected = Output.Create(Aws.Ec2.GetSubnet.InvokeAsync(new Aws.Ec2.GetSubnetArgs
        {
            Id = subnetId,
        }));
        var subnet = new Aws.Ec2.SecurityGroup("subnet", new Aws.Ec2.SecurityGroupArgs
        {
            VpcId = selected.Apply(selected => selected.VpcId),
            Ingress = 
            {
                new Aws.Ec2.Inputs.SecurityGroupIngressArgs
                {
                    CidrBlocks = 
                    {
                        selected.Apply(selected => selected.CidrBlock),
                    },
                    FromPort = 80,
                    ToPort = 80,
                    Protocol = "tcp",
                },
            },
        });
    }

}
package main

import (
	"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/ec2"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		cfg := config.New(ctx, "")
		subnetId := cfg.RequireObject("subnetId")
		opt0 := subnetId
		selected, err := ec2.LookupSubnet(ctx, &ec2.LookupSubnetArgs{
			Id: &opt0,
		}, nil)
		if err != nil {
			return err
		}
		_, err = ec2.NewSecurityGroup(ctx, "subnet", &ec2.SecurityGroupArgs{
			VpcId: pulumi.String(selected.VpcId),
			Ingress: ec2.SecurityGroupIngressArray{
				&ec2.SecurityGroupIngressArgs{
					CidrBlocks: pulumi.StringArray{
						pulumi.String(selected.CidrBlock),
					},
					FromPort: pulumi.Int(80),
					ToPort:   pulumi.Int(80),
					Protocol: pulumi.String("tcp"),
				},
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
import pulumi
import pulumi_aws as aws

config = pulumi.Config()
subnet_id = config.require_object("subnetId")
selected = aws.ec2.get_subnet(id=subnet_id)
subnet = aws.ec2.SecurityGroup("subnet",
    vpc_id=selected.vpc_id,
    ingress=[aws.ec2.SecurityGroupIngressArgs(
        cidr_blocks=[selected.cidr_block],
        from_port=80,
        to_port=80,
        protocol="tcp",
    )])
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const config = new pulumi.Config();
const subnetId = config.requireObject("subnetId");
const selected = aws.ec2.getSubnet({
    id: subnetId,
});
const subnet = new aws.ec2.SecurityGroup("subnet", {
    vpcId: selected.then(selected => selected.vpcId),
    ingress: [{
        cidrBlocks: [selected.then(selected => selected.cidrBlock)],
        fromPort: 80,
        toPort: 80,
        protocol: "tcp",
    }],
});

Filter Example

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var selected = Output.Create(Aws.Ec2.GetSubnet.InvokeAsync(new Aws.Ec2.GetSubnetArgs
        {
            Filters = 
            {
                new Aws.Ec2.Inputs.GetSubnetFilterArgs
                {
                    Name = "tag:Name",
                    Values = 
                    {
                        "yakdriver",
                    },
                },
            },
        }));
    }

}
package main

import (
	"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/ec2"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := ec2.LookupSubnet(ctx, &ec2.LookupSubnetArgs{
			Filters: []ec2.GetSubnetFilter{
				ec2.GetSubnetFilter{
					Name: "tag:Name",
					Values: []string{
						"yakdriver",
					},
				},
			},
		}, nil)
		if err != nil {
			return err
		}
		return nil
	})
}
import pulumi
import pulumi_aws as aws

selected = aws.ec2.get_subnet(filters=[aws.ec2.GetSubnetFilterArgs(
    name="tag:Name",
    values=["yakdriver"],
)])
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const selected = pulumi.output(aws.ec2.getSubnet({
    filters: [{
        name: "tag:Name",
        values: ["yakdriver"],
    }],
}, { async: true }));

Using getSubnet

function getSubnet(args: GetSubnetArgs, opts?: InvokeOptions): Promise<GetSubnetResult>
def get_subnet(availability_zone: Optional[str] = None,
               availability_zone_id: Optional[str] = None,
               cidr_block: Optional[str] = None,
               default_for_az: Optional[bool] = None,
               filters: Optional[Sequence[GetSubnetFilter]] = None,
               id: Optional[str] = None,
               ipv6_cidr_block: Optional[str] = None,
               state: Optional[str] = None,
               tags: Optional[Mapping[str, str]] = None,
               vpc_id: Optional[str] = None,
               opts: Optional[InvokeOptions] = None) -> GetSubnetResult
func LookupSubnet(ctx *Context, args *LookupSubnetArgs, opts ...InvokeOption) (*LookupSubnetResult, error)

Note: This function is named LookupSubnet in the Go SDK.

public static class GetSubnet {
    public static Task<GetSubnetResult> InvokeAsync(GetSubnetArgs args, InvokeOptions? opts = null)
}

The following arguments are supported:

AvailabilityZone string
Availability zone where the subnet must reside.
AvailabilityZoneId string
ID of the Availability Zone for the subnet.
CidrBlock string
CIDR block of the desired subnet.
DefaultForAz bool
Whether the desired subnet must be the default subnet for its associated availability zone.
Filters List<GetSubnetFilter>
Configuration block. Detailed below.
Id string
ID of the specific subnet to retrieve.
Ipv6CidrBlock string
IPv6 CIDR block of the desired subnet.
State string
State that the desired subnet must have.
Tags Dictionary<string, string>
Map of tags, each pair of which must exactly match a pair on the desired subnet.
VpcId string
ID of the VPC that the desired subnet belongs to.
AvailabilityZone string
Availability zone where the subnet must reside.
AvailabilityZoneId string
ID of the Availability Zone for the subnet.
CidrBlock string
CIDR block of the desired subnet.
DefaultForAz bool
Whether the desired subnet must be the default subnet for its associated availability zone.
Filters []GetSubnetFilter
Configuration block. Detailed below.
Id string
ID of the specific subnet to retrieve.
Ipv6CidrBlock string
IPv6 CIDR block of the desired subnet.
State string
State that the desired subnet must have.
Tags map[string]string
Map of tags, each pair of which must exactly match a pair on the desired subnet.
VpcId string
ID of the VPC that the desired subnet belongs to.
availabilityZone string
Availability zone where the subnet must reside.
availabilityZoneId string
ID of the Availability Zone for the subnet.
cidrBlock string
CIDR block of the desired subnet.
defaultForAz boolean
Whether the desired subnet must be the default subnet for its associated availability zone.
filters GetSubnetFilter[]
Configuration block. Detailed below.
id string
ID of the specific subnet to retrieve.
ipv6CidrBlock string
IPv6 CIDR block of the desired subnet.
state string
State that the desired subnet must have.
tags {[key: string]: string}
Map of tags, each pair of which must exactly match a pair on the desired subnet.
vpcId string
ID of the VPC that the desired subnet belongs to.
availability_zone str
Availability zone where the subnet must reside.
availability_zone_id str
ID of the Availability Zone for the subnet.
cidr_block str
CIDR block of the desired subnet.
default_for_az bool
Whether the desired subnet must be the default subnet for its associated availability zone.
filters Sequence[GetSubnetFilter]
Configuration block. Detailed below.
id str
ID of the specific subnet to retrieve.
ipv6_cidr_block str
IPv6 CIDR block of the desired subnet.
state str
State that the desired subnet must have.
tags Mapping[str, str]
Map of tags, each pair of which must exactly match a pair on the desired subnet.
vpc_id str
ID of the VPC that the desired subnet belongs to.

getSubnet Result

The following output properties are available:

Arn string
ARN of the subnet.
AssignIpv6AddressOnCreation bool
Whether an IPv6 address is assigned on creation.
AvailabilityZone string
AvailabilityZoneId string
AvailableIpAddressCount int
Available IP addresses of the subnet.
CidrBlock string
CustomerOwnedIpv4Pool string
Identifier of customer owned IPv4 address pool.
DefaultForAz bool
Id string
Ipv6CidrBlock string
Ipv6CidrBlockAssociationId string
Association ID of the IPv6 CIDR block.
MapCustomerOwnedIpOnLaunch bool
Whether customer owned IP addresses are assigned on network interface creation.
MapPublicIpOnLaunch bool
Whether public IP addresses are assigned on instance launch.
OutpostArn string
ARN of the Outpost.
OwnerId string
ID of the AWS account that owns the subnet.
State string
Tags Dictionary<string, string>
VpcId string
Filters List<GetSubnetFilter>
Arn string
ARN of the subnet.
AssignIpv6AddressOnCreation bool
Whether an IPv6 address is assigned on creation.
AvailabilityZone string
AvailabilityZoneId string
AvailableIpAddressCount int
Available IP addresses of the subnet.
CidrBlock string
CustomerOwnedIpv4Pool string
Identifier of customer owned IPv4 address pool.
DefaultForAz bool
Id string
Ipv6CidrBlock string
Ipv6CidrBlockAssociationId string
Association ID of the IPv6 CIDR block.
MapCustomerOwnedIpOnLaunch bool
Whether customer owned IP addresses are assigned on network interface creation.
MapPublicIpOnLaunch bool
Whether public IP addresses are assigned on instance launch.
OutpostArn string
ARN of the Outpost.
OwnerId string
ID of the AWS account that owns the subnet.
State string
Tags map[string]string
VpcId string
Filters []GetSubnetFilter
arn string
ARN of the subnet.
assignIpv6AddressOnCreation boolean
Whether an IPv6 address is assigned on creation.
availabilityZone string
availabilityZoneId string
availableIpAddressCount number
Available IP addresses of the subnet.
cidrBlock string
customerOwnedIpv4Pool string
Identifier of customer owned IPv4 address pool.
defaultForAz boolean
id string
ipv6CidrBlock string
ipv6CidrBlockAssociationId string
Association ID of the IPv6 CIDR block.
mapCustomerOwnedIpOnLaunch boolean
Whether customer owned IP addresses are assigned on network interface creation.
mapPublicIpOnLaunch boolean
Whether public IP addresses are assigned on instance launch.
outpostArn string
ARN of the Outpost.
ownerId string
ID of the AWS account that owns the subnet.
state string
tags {[key: string]: string}
vpcId string
filters GetSubnetFilter[]
arn str
ARN of the subnet.
assign_ipv6_address_on_creation bool
Whether an IPv6 address is assigned on creation.
availability_zone str
availability_zone_id str
available_ip_address_count int
Available IP addresses of the subnet.
cidr_block str
customer_owned_ipv4_pool str
Identifier of customer owned IPv4 address pool.
default_for_az bool
id str
ipv6_cidr_block str
ipv6_cidr_block_association_id str
Association ID of the IPv6 CIDR block.
map_customer_owned_ip_on_launch bool
Whether customer owned IP addresses are assigned on network interface creation.
map_public_ip_on_launch bool
Whether public IP addresses are assigned on instance launch.
outpost_arn str
ARN of the Outpost.
owner_id str
ID of the AWS account that owns the subnet.
state str
tags Mapping[str, str]
vpc_id str
filters Sequence[GetSubnetFilter]

Supporting Types

GetSubnetFilter

Name string
The name of the field to filter by, as defined by the underlying AWS API.
Values List<string>
Set of values that are accepted for the given field. A subnet will be selected if any one of the given values matches.
Name string
The name of the field to filter by, as defined by the underlying AWS API.
Values []string
Set of values that are accepted for the given field. A subnet will be selected if any one of the given values matches.
name string
The name of the field to filter by, as defined by the underlying AWS API.
values string[]
Set of values that are accepted for the given field. A subnet will be selected if any one of the given values matches.
name str
The name of the field to filter by, as defined by the underlying AWS API.
values Sequence[str]
Set of values that are accepted for the given field. A subnet will be selected if any one of the given values matches.

Package Details

Repository
https://github.com/pulumi/pulumi-aws
License
Apache-2.0
Notes
This Pulumi package is based on the aws Terraform Provider.