Subnet

Provides an VPC subnet resource.

NOTE: Due to AWS Lambda improved VPC networking changes that began deploying in September 2019, subnets associated with Lambda Functions can take up to 45 minutes to successfully delete.

Example Usage

Basic Usage

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var main = new Aws.Ec2.Subnet("main", new Aws.Ec2.SubnetArgs
        {
            VpcId = aws_vpc.Main.Id,
            CidrBlock = "10.0.1.0/24",
            Tags = 
            {
                { "Name", "Main" },
            },
        });
    }

}
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.NewSubnet(ctx, "main", &ec2.SubnetArgs{
			VpcId:     pulumi.Any(aws_vpc.Main.Id),
			CidrBlock: pulumi.String("10.0.1.0/24"),
			Tags: pulumi.StringMap{
				"Name": pulumi.String("Main"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
import pulumi
import pulumi_aws as aws

main = aws.ec2.Subnet("main",
    vpc_id=aws_vpc["main"]["id"],
    cidr_block="10.0.1.0/24",
    tags={
        "Name": "Main",
    })
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const main = new aws.ec2.Subnet("main", {
    vpcId: aws_vpc.main.id,
    cidrBlock: "10.0.1.0/24",
    tags: {
        Name: "Main",
    },
});

Subnets In Secondary VPC CIDR Blocks

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var secondaryCidr = new Aws.Ec2.VpcIpv4CidrBlockAssociation("secondaryCidr", new Aws.Ec2.VpcIpv4CidrBlockAssociationArgs
        {
            VpcId = aws_vpc.Main.Id,
            CidrBlock = "172.2.0.0/16",
        });
        var inSecondaryCidr = new Aws.Ec2.Subnet("inSecondaryCidr", new Aws.Ec2.SubnetArgs
        {
            VpcId = secondaryCidr.VpcId,
            CidrBlock = "172.2.0.0/24",
        });
    }

}
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 {
		secondaryCidr, err := ec2.NewVpcIpv4CidrBlockAssociation(ctx, "secondaryCidr", &ec2.VpcIpv4CidrBlockAssociationArgs{
			VpcId:     pulumi.Any(aws_vpc.Main.Id),
			CidrBlock: pulumi.String("172.2.0.0/16"),
		})
		if err != nil {
			return err
		}
		_, err = ec2.NewSubnet(ctx, "inSecondaryCidr", &ec2.SubnetArgs{
			VpcId:     secondaryCidr.VpcId,
			CidrBlock: pulumi.String("172.2.0.0/24"),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
import pulumi
import pulumi_aws as aws

secondary_cidr = aws.ec2.VpcIpv4CidrBlockAssociation("secondaryCidr",
    vpc_id=aws_vpc["main"]["id"],
    cidr_block="172.2.0.0/16")
in_secondary_cidr = aws.ec2.Subnet("inSecondaryCidr",
    vpc_id=secondary_cidr.vpc_id,
    cidr_block="172.2.0.0/24")
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const secondaryCidr = new aws.ec2.VpcIpv4CidrBlockAssociation("secondaryCidr", {
    vpcId: aws_vpc.main.id,
    cidrBlock: "172.2.0.0/16",
});
const inSecondaryCidr = new aws.ec2.Subnet("inSecondaryCidr", {
    vpcId: secondaryCidr.vpcId,
    cidrBlock: "172.2.0.0/24",
});

Create a Subnet Resource

new Subnet(name: string, args: SubnetArgs, opts?: CustomResourceOptions);
@overload
def Subnet(resource_name: str,
           opts: Optional[ResourceOptions] = None,
           assign_ipv6_address_on_creation: Optional[bool] = None,
           availability_zone: Optional[str] = None,
           availability_zone_id: Optional[str] = None,
           cidr_block: Optional[str] = None,
           customer_owned_ipv4_pool: Optional[str] = None,
           ipv6_cidr_block: Optional[str] = None,
           map_customer_owned_ip_on_launch: Optional[bool] = None,
           map_public_ip_on_launch: Optional[bool] = None,
           outpost_arn: Optional[str] = None,
           tags: Optional[Mapping[str, str]] = None,
           vpc_id: Optional[str] = None)
@overload
def Subnet(resource_name: str,
           args: SubnetArgs,
           opts: Optional[ResourceOptions] = None)
func NewSubnet(ctx *Context, name string, args SubnetArgs, opts ...ResourceOption) (*Subnet, error)
public Subnet(string name, SubnetArgs args, CustomResourceOptions? opts = null)
name string
The unique name of the resource.
args SubnetArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
args SubnetArgs
The arguments to resource properties.
opts ResourceOptions
Bag of options to control resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args SubnetArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args SubnetArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.

Subnet Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.

Inputs

The Subnet resource accepts the following input properties:

CidrBlock string
The CIDR block for the subnet.
VpcId string
The VPC ID.
AssignIpv6AddressOnCreation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
AvailabilityZone string
The AZ for the subnet.
AvailabilityZoneId string
The AZ ID of the subnet.
CustomerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
Ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
MapCustomerOwnedIpOnLaunch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
MapPublicIpOnLaunch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
OutpostArn string
The Amazon Resource Name (ARN) of the Outpost.
Tags Dictionary<string, string>
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
CidrBlock string
The CIDR block for the subnet.
VpcId string
The VPC ID.
AssignIpv6AddressOnCreation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
AvailabilityZone string
The AZ for the subnet.
AvailabilityZoneId string
The AZ ID of the subnet.
CustomerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
Ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
MapCustomerOwnedIpOnLaunch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
MapPublicIpOnLaunch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
OutpostArn string
The Amazon Resource Name (ARN) of the Outpost.
Tags map[string]string
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
cidrBlock string
The CIDR block for the subnet.
vpcId string
The VPC ID.
assignIpv6AddressOnCreation boolean
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
availabilityZone string
The AZ for the subnet.
availabilityZoneId string
The AZ ID of the subnet.
customerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
mapCustomerOwnedIpOnLaunch boolean
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
mapPublicIpOnLaunch boolean
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
outpostArn string
The Amazon Resource Name (ARN) of the Outpost.
tags {[key: string]: string}
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
cidr_block str
The CIDR block for the subnet.
vpc_id str
The VPC ID.
assign_ipv6_address_on_creation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
availability_zone str
The AZ for the subnet.
availability_zone_id str
The AZ ID of the subnet.
customer_owned_ipv4_pool str
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
ipv6_cidr_block str
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
map_customer_owned_ip_on_launch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
map_public_ip_on_launch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
outpost_arn str
The Amazon Resource Name (ARN) of the Outpost.
tags Mapping[str, str]
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.

Outputs

All input properties are implicitly available as output properties. Additionally, the Subnet resource produces the following output properties:

Arn string
The ARN of the subnet.
Id string
The provider-assigned unique ID for this managed resource.
Ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
OwnerId string
The ID of the AWS account that owns the subnet.
TagsAll Dictionary<string, string>
A map of tags assigned to the resource, including those inherited from the provider .
Arn string
The ARN of the subnet.
Id string
The provider-assigned unique ID for this managed resource.
Ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
OwnerId string
The ID of the AWS account that owns the subnet.
TagsAll map[string]string
A map of tags assigned to the resource, including those inherited from the provider .
arn string
The ARN of the subnet.
id string
The provider-assigned unique ID for this managed resource.
ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
ownerId string
The ID of the AWS account that owns the subnet.
tagsAll {[key: string]: string}
A map of tags assigned to the resource, including those inherited from the provider .
arn str
The ARN of the subnet.
id str
The provider-assigned unique ID for this managed resource.
ipv6_cidr_block_association_id str
The association ID for the IPv6 CIDR block.
owner_id str
The ID of the AWS account that owns the subnet.
tags_all Mapping[str, str]
A map of tags assigned to the resource, including those inherited from the provider .

Look up an Existing Subnet Resource

Get an existing Subnet resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: SubnetState, opts?: CustomResourceOptions): Subnet
@staticmethod
def get(resource_name: str,
        id: str,
        opts: Optional[ResourceOptions] = None,
        arn: Optional[str] = None,
        assign_ipv6_address_on_creation: Optional[bool] = None,
        availability_zone: Optional[str] = None,
        availability_zone_id: Optional[str] = None,
        cidr_block: Optional[str] = None,
        customer_owned_ipv4_pool: Optional[str] = None,
        ipv6_cidr_block: Optional[str] = None,
        ipv6_cidr_block_association_id: Optional[str] = None,
        map_customer_owned_ip_on_launch: Optional[bool] = None,
        map_public_ip_on_launch: Optional[bool] = None,
        outpost_arn: Optional[str] = None,
        owner_id: Optional[str] = None,
        tags: Optional[Mapping[str, str]] = None,
        tags_all: Optional[Mapping[str, str]] = None,
        vpc_id: Optional[str] = None) -> Subnet
func GetSubnet(ctx *Context, name string, id IDInput, state *SubnetState, opts ...ResourceOption) (*Subnet, error)
public static Subnet Get(string name, Input<string> id, SubnetState? state, CustomResourceOptions? opts = null)
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.

The following state arguments are supported:

Arn string
The ARN of the subnet.
AssignIpv6AddressOnCreation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
AvailabilityZone string
The AZ for the subnet.
AvailabilityZoneId string
The AZ ID of the subnet.
CidrBlock string
The CIDR block for the subnet.
CustomerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
Ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
Ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
MapCustomerOwnedIpOnLaunch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
MapPublicIpOnLaunch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
OutpostArn string
The Amazon Resource Name (ARN) of the Outpost.
OwnerId string
The ID of the AWS account that owns the subnet.
Tags Dictionary<string, string>
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
TagsAll Dictionary<string, string>
A map of tags assigned to the resource, including those inherited from the provider .
VpcId string
The VPC ID.
Arn string
The ARN of the subnet.
AssignIpv6AddressOnCreation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
AvailabilityZone string
The AZ for the subnet.
AvailabilityZoneId string
The AZ ID of the subnet.
CidrBlock string
The CIDR block for the subnet.
CustomerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
Ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
Ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
MapCustomerOwnedIpOnLaunch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
MapPublicIpOnLaunch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
OutpostArn string
The Amazon Resource Name (ARN) of the Outpost.
OwnerId string
The ID of the AWS account that owns the subnet.
Tags map[string]string
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
TagsAll map[string]string
A map of tags assigned to the resource, including those inherited from the provider .
VpcId string
The VPC ID.
arn string
The ARN of the subnet.
assignIpv6AddressOnCreation boolean
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
availabilityZone string
The AZ for the subnet.
availabilityZoneId string
The AZ ID of the subnet.
cidrBlock string
The CIDR block for the subnet.
customerOwnedIpv4Pool string
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
ipv6CidrBlock string
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
ipv6CidrBlockAssociationId string
The association ID for the IPv6 CIDR block.
mapCustomerOwnedIpOnLaunch boolean
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
mapPublicIpOnLaunch boolean
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
outpostArn string
The Amazon Resource Name (ARN) of the Outpost.
ownerId string
The ID of the AWS account that owns the subnet.
tags {[key: string]: string}
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
tagsAll {[key: string]: string}
A map of tags assigned to the resource, including those inherited from the provider .
vpcId string
The VPC ID.
arn str
The ARN of the subnet.
assign_ipv6_address_on_creation bool
Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. Default is false
availability_zone str
The AZ for the subnet.
availability_zone_id str
The AZ ID of the subnet.
cidr_block str
The CIDR block for the subnet.
customer_owned_ipv4_pool str
The customer owned IPv4 address pool. Typically used with the map_customer_owned_ip_on_launch argument. The outpost_arn argument must be specified when configured.
ipv6_cidr_block str
The IPv6 network range for the subnet, in CIDR notation. The subnet size must use a /64 prefix length.
ipv6_cidr_block_association_id str
The association ID for the IPv6 CIDR block.
map_customer_owned_ip_on_launch bool
Specify true to indicate that network interfaces created in the subnet should be assigned a customer owned IP address. The customer_owned_ipv4_pool and outpost_arn arguments must be specified when set to true. Default is false.
map_public_ip_on_launch bool
Specify true to indicate that instances launched into the subnet should be assigned a public IP address. Default is false.
outpost_arn str
The Amazon Resource Name (ARN) of the Outpost.
owner_id str
The ID of the AWS account that owns the subnet.
tags Mapping[str, str]
A map of tags to assign to the resource. .If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
tags_all Mapping[str, str]
A map of tags assigned to the resource, including those inherited from the provider .
vpc_id str
The VPC ID.

Import

Subnets can be imported using the subnet id, e.g.

 $ pulumi import aws:ec2/subnet:Subnet public_subnet subnet-9d4a7b6c

Package Details

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