SubnetV2

class aws_cdk.aws_ec2_alpha.SubnetV2(scope, id, *, availability_zone, ipv4_cidr_block, subnet_type, vpc, assign_ipv6_address_on_creation=None, ipv6_cidr_block=None, route_table=None, subnet_name=None)

Bases: Resource

(experimental) The SubnetV2 class represents a subnet within a VPC (Virtual Private Cloud) in AWS.

It extends the Resource class and implements the ISubnet interface.

Instances of this class can be used to create and manage subnets within a VpcV2 instance. Subnets can be configured with specific IP address ranges (IPv4 and IPv6), availability zones, and subnet types (e.g., public, private, isolated).

Stability:

experimental

Resource:

AWS::EC2::Subnet

ExampleMetadata:

infused

Example:

my_vpc = VpcV2(self, "Vpc")
route_table = RouteTable(self, "RouteTable",
    vpc=my_vpc
)
subnet = SubnetV2(self, "Subnet",
    vpc=my_vpc,
    availability_zone="eu-west-2a",
    ipv4_cidr_block=IpCidr("10.0.0.0/24"),
    subnet_type=SubnetType.PRIVATE_ISOLATED
)

natgw = NatGateway(self, "NatGW",
    subnet=subnet,
    vpc=my_vpc,
    connectivity_type=NatConnectivityType.PRIVATE,
    private_ip_address="10.0.0.42"
)
Route(self, "NatGwRoute",
    route_table=route_table,
    destination="0.0.0.0/0",
    target={"gateway": natgw}
)

(experimental) Constructs a new SubnetV2 instance.

Parameters:
  • scope (Construct) – The parent Construct that this resource will be part of.

  • id (str) – The unique identifier for this resource.

  • availability_zone (str) – (experimental) Custom AZ for the subnet.

  • ipv4_cidr_block (IpCidr) – (experimental) ipv4 cidr to assign to this subnet. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html#cfn-ec2-subnet-cidrblock

  • subnet_type (SubnetType) – (experimental) The type of Subnet to configure. The Subnet type will control the ability to route and connect to the Internet. TODO: Add validation check subnetType when adding resources (e.g. cannot add NatGateway to private)

  • vpc (IVpcV2) – (experimental) VPC Prop.

  • assign_ipv6_address_on_creation (Optional[bool]) – (experimental) Indicates whether a network interface created in this subnet receives an IPv6 address. If you specify AssignIpv6AddressOnCreation, you must also specify Ipv6CidrBlock. Default: - undefined in case not provided as an input

  • ipv6_cidr_block (Optional[IpCidr]) – (experimental) Ipv6 CIDR Range for subnet. Default: - No Ipv6 address

  • route_table (Optional[IRouteTable]) – (experimental) Custom Route for subnet. Default: - a default route table created

  • subnet_name (Optional[str]) – (experimental) Subnet name. Default: - provisioned with an autogenerated name by CDK

Stability:

experimental

Methods

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:

policy (RemovalPolicy) –

Return type:

None

associate_network_acl(id, network_acl)

(experimental) Associate a Network ACL with this subnet.

Parameters:
  • id (str) – The unique identifier for this association.

  • network_acl (INetworkAcl) – The Network ACL to associate with this subnet. This allows controlling inbound and outbound traffic for instances in this subnet.

Stability:

experimental

Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

availability_zone

(experimental) The Availability Zone the subnet is located in.

Stability:

experimental

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

internet_connectivity_established

(experimental) Dependencies for internet connectivity This Property exposes the RouteTable-Subnet association so that other resources can depend on it.

Stability:

experimental

ipv4_cidr_block

(experimental) The IPv4 CIDR block for this subnet.

Stability:

experimental

ipv6_cidr_block

(experimental) The IPv6 CIDR Block for this subnet.

Stability:

experimental

network_acl

(experimental) Returns the Network ACL associated with this subnet.

Stability:

experimental

node

The tree node.

route_table

(experimental) Return the Route Table associated with this subnet.

Stability:

experimental

stack

The stack in which this resource is defined.

subnet_id

(experimental) The subnetId for this particular subnet.

Stability:

experimental

Attribute:

true

subnet_type

(experimental) The type of subnet (public or private) that this subnet represents.

Stability:

experimental

Attribute:

SubnetType

Static Methods

classmethod from_subnet_v2_attributes(scope, id, *, availability_zone, ipv4_cidr_block, subnet_id, subnet_type, ipv6_cidr_block=None, route_table_id=None, subnet_name=None)

(experimental) Import an existing subnet to the VPC.

Parameters:
  • scope (Construct) –

  • id (str) –

  • availability_zone (str) – (experimental) The Availability Zone this subnet is located in. Default: - No AZ information, cannot use AZ selection features

  • ipv4_cidr_block (str) – (experimental) The IPv4 CIDR block associated with the subnet. Default: - No CIDR information, cannot use CIDR filter features

  • subnet_id (str) – (experimental) The subnetId for this particular subnet.

  • subnet_type (SubnetType) – (experimental) The type of subnet (public or private) that this subnet represents.

  • ipv6_cidr_block (Optional[str]) – (experimental) The IPv4 CIDR block associated with the subnet. Default: - No CIDR information, cannot use CIDR filter features

  • route_table_id (Optional[str]) – (experimental) The ID of the route table for this particular subnet. Default: - No route table information, cannot create VPC endpoints

  • subnet_name (Optional[str]) – (experimental) Name of the given subnet. Default: - no subnet name

Stability:

experimental

Return type:

ISubnetV2

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters:

construct (IConstruct) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool