class SubnetV2 (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.ec2.Alpha.SubnetV2 |
Go | github.com/aws/aws-cdk-go/awsec2alpha/v2#SubnetV2 |
Java | software.amazon.awscdk.services.ec2.alpha.SubnetV2 |
Python | aws_cdk.aws_ec2_alpha.SubnetV2 |
TypeScript (source) | @aws-cdk/aws-ec2-alpha ยป SubnetV2 |
Implements
IConstruct
, IDependable
, IResource
, ISubnet
, ISubnet
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).
Example
const myVpc = new VpcV2(this, 'Vpc');
const routeTable = new RouteTable(this, 'RouteTable', {
vpc: myVpc,
});
const subnet = new SubnetV2(this, 'Subnet', {
vpc: myVpc,
availabilityZone: 'eu-west-2a',
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
subnetType: SubnetType.PRIVATE_ISOLATED });
const natgw = new NatGateway(this, 'NatGW', {
subnet: subnet,
vpc: myVpc,
connectivityType: NatConnectivityType.PRIVATE,
privateIpAddress: '10.0.0.42',
});
new Route(this, 'NatGwRoute', {
routeTable,
destination: '0.0.0.0/0',
target: { gateway: natgw },
});
Initializer
new SubnetV2(scope: Construct, id: string, props: SubnetV2Props)
Parameters
- scope
Construct
โ The parent Construct that this resource will be part of. - id
string
โ The unique identifier for this resource. - props
Subnet
โ The configuration properties for the subnet.V2 Props
Constructs a new SubnetV2 instance.
Construct Props
Name | Type | Description |
---|---|---|
availability | string | Custom AZ for the subnet. |
ipv4 | Ip | ipv4 cidr to assign to this subnet. |
subnet | Subnet | The type of Subnet to configure. |
vpc | IVpc | VPC Prop. |
assign | boolean | Indicates whether a network interface created in this subnet receives an IPv6 address. |
ipv6 | Ip | Ipv6 CIDR Range for subnet. |
route | IRoute | Custom Route for subnet. |
subnet | string | Subnet name. |
availabilityZone
Type:
string
Custom AZ for the subnet.
ipv4CidrBlock
Type:
Ip
ipv4 cidr to assign to this subnet.
subnetType
Type:
Subnet
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
Type:
IVpc
VPC Prop.
assignIpv6AddressOnCreation?
Type:
boolean
(optional, default: undefined in case not provided as an input)
Indicates whether a network interface created in this subnet receives an IPv6 address.
If you specify AssignIpv6AddressOnCreation, you must also specify Ipv6CidrBlock.
ipv6CidrBlock?
Type:
Ip
(optional, default: No Ipv6 address)
Ipv6 CIDR Range for subnet.
routeTable?
Type:
IRoute
(optional, default: a default route table created)
Custom Route for subnet.
subnetName?
Type:
string
(optional, default: provisioned with an autogenerated name by CDK)
Subnet name.
Properties
Name | Type | Description |
---|---|---|
availability | string | The Availability Zone the subnet is located in. |
env | Resource | The environment this resource belongs to. |
internet | IDependable | Dependencies for internet connectivity This Property exposes the RouteTable-Subnet association so that other resources can depend on it. |
ipv4 | string | The IPv4 CIDR block for this subnet. |
network | INetwork | Returns the Network ACL associated with this subnet. |
node | Node | The tree node. |
route | IRoute | Return the Route Table associated with this subnet. |
stack | Stack | The stack in which this resource is defined. |
subnet | string | The subnetId for this particular subnet. |
ipv6 | string | The IPv6 CIDR Block for this subnet. |
subnet | Subnet | The type of subnet (public or private) that this subnet represents. |
availabilityZone
Type:
string
The Availability Zone the subnet is located in.
env
Type:
Resource
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.
internetConnectivityEstablished
Type:
IDependable
Dependencies for internet connectivity This Property exposes the RouteTable-Subnet association so that other resources can depend on it.
ipv4CidrBlock
Type:
string
The IPv4 CIDR block for this subnet.
networkAcl
Type:
INetwork
Returns the Network ACL associated with this subnet.
node
Type:
Node
The tree node.
routeTable
Type:
IRoute
Return the Route Table associated with this subnet.
stack
Type:
Stack
The stack in which this resource is defined.
subnetId
Type:
string
The subnetId for this particular subnet.
ipv6CidrBlock?
Type:
string
(optional)
The IPv6 CIDR Block for this subnet.
subnetType?
Type:
Subnet
(optional)
The type of subnet (public or private) that this subnet represents.
Methods
Name | Description |
---|---|
apply | Apply the given removal policy to this resource. |
associate | Associate a Network ACL with this subnet. |
to | Returns a string representation of this construct. |
static from | Import an existing subnet to the VPC. |
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
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
).
NetworkAcl(id, networkAcl)
associatepublic associateNetworkAcl(id: string, networkAcl: INetworkAcl): void
Parameters
- id
string
โ The unique identifier for this association. - networkAcl
INetwork
โ The Network ACL to associate with this subnet.Acl
Associate a Network ACL with this subnet.
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
SubnetV2Attributes(scope, id, attrs)
static frompublic static fromSubnetV2Attributes(scope: Construct, id: string, attrs: SubnetV2Attributes): ISubnetV2
Parameters
- scope
Construct
- id
string
- attrs
Subnet
V2 Attributes
Returns
Import an existing subnet to the VPC.