aws-cdk-lib.aws_ec2.PublicSubnet

class PublicSubnet (construct)

LanguageType name
.NETAmazon.CDK.AWS.EC2.PublicSubnet
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsec2#PublicSubnet
Javasoftware.amazon.awscdk.services.ec2.PublicSubnet
Pythonaws_cdk.aws_ec2.PublicSubnet
TypeScript (source)aws-cdk-lib » aws_ec2 » PublicSubnet

Implements IConstruct, IDependable, IResource, ISubnet, IPublicSubnet

Represents a public VPC subnet resource.

Example

// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_ec2 as ec2 } from 'aws-cdk-lib';
const publicSubnet = new ec2.PublicSubnet(this, 'MyPublicSubnet', {
  availabilityZone: 'availabilityZone',
  cidrBlock: 'cidrBlock',
  vpcId: 'vpcId',

  // the properties below are optional
  mapPublicIpOnLaunch: false,
});

Initializer

new PublicSubnet(scope: Construct, id: string, props: PublicSubnetProps)

Parameters

  • scope Construct
  • id string
  • props PublicSubnetProps

Construct Props

NameTypeDescription
availabilityZonestringThe availability zone for the subnet.
cidrBlockstringThe CIDR notation for this subnet.
vpcIdstringThe VPC which this subnet is part of.
mapPublicIpOnLaunch?booleanControls if a public IP is associated to an instance at launch.

availabilityZone

Type: string

The availability zone for the subnet.


cidrBlock

Type: string

The CIDR notation for this subnet.


vpcId

Type: string

The VPC which this subnet is part of.


mapPublicIpOnLaunch?

Type: boolean (optional, default: true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.)

Controls if a public IP is associated to an instance at launch.

Properties

NameTypeDescription
availabilityZonestringThe Availability Zone the subnet is located in.
dependencyElementsIDependable[]Parts of this VPC subnet.
envResourceEnvironmentThe environment this resource belongs to.
internetConnectivityEstablishedIDependableDependable that can be depended upon to force internet connectivity established on the VPC.
ipv4CidrBlockstringThe IPv4 CIDR block for this subnet.
networkAclINetworkAclNetwork ACL associated with this Subnet.
nodeNodeThe tree node.
routeTableIRouteTableThe routeTableId attached to this subnet.
stackStackThe stack in which this resource is defined.
subnetAvailabilityZonestring
subnetIdstringThe subnetId for this particular subnet.
subnetIpv6CidrBlocksstring[]
subnetNetworkAclAssociationIdstring
subnetOutpostArnstringThe Amazon Resource Name (ARN) of the Outpost for this subnet (if one exists).
subnetVpcIdstring

availabilityZone

Type: string

The Availability Zone the subnet is located in.


dependencyElements

Type: IDependable[]

Parts of this VPC subnet.


env

Type: ResourceEnvironment

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

Dependable that can be depended upon to force internet connectivity established on the VPC.


ipv4CidrBlock

Type: string

The IPv4 CIDR block for this subnet.


networkAcl

Type: INetworkAcl

Network ACL associated with this Subnet.

Upon creation, this is the default ACL which allows all traffic, except explicit DENY entries that you add.

You can replace it with a custom ACL which denies all traffic except the explicit ALLOW entries that you add by creating a NetworkAcl object and calling associateNetworkAcl().


node

Type: Node

The tree node.


routeTable

Type: IRouteTable

The routeTableId attached to this subnet.


stack

Type: Stack

The stack in which this resource is defined.


subnetAvailabilityZone

Type: string


subnetId

Type: string

The subnetId for this particular subnet.


subnetIpv6CidrBlocks

Type: string[]


subnetNetworkAclAssociationId

Type: string


subnetOutpostArn

Type: string

The Amazon Resource Name (ARN) of the Outpost for this subnet (if one exists).


subnetVpcId

Type: string

Methods

NameDescription
addDefaultInternetRoute(gatewayId, gatewayAttachment)Create a default route that points to a passed IGW, with a dependency on the IGW's attachment to the VPC.
addDefaultNatRoute(natGatewayId)Adds an entry to this subnets route table that points to the passed NATGatewayId.
addNatGateway(eipAllocationId?)Creates a new managed NAT gateway attached to this public subnet.
addRoute(id, options)Adds an entry to this subnets route table.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
associateNetworkAcl(id, networkAcl)Associate a Network ACL with this subnet.
toString()Returns a string representation of this construct.
static fromPublicSubnetAttributes(scope, id, attrs)

addDefaultInternetRoute(gatewayId, gatewayAttachment)

public addDefaultInternetRoute(gatewayId: string, gatewayAttachment: IDependable): void

Parameters

  • gatewayId string — the logical ID (ref) of the gateway attached to your VPC.
  • gatewayAttachment IDependable — the gateway attachment construct to be added as a dependency.

Create a default route that points to a passed IGW, with a dependency on the IGW's attachment to the VPC.


addDefaultNatRoute(natGatewayId)

public addDefaultNatRoute(natGatewayId: string): void

Parameters

  • natGatewayId string — The ID of the NAT gateway.

Adds an entry to this subnets route table that points to the passed NATGatewayId.


addNatGateway(eipAllocationId?)

public addNatGateway(eipAllocationId?: string): CfnNatGateway

Parameters

  • eipAllocationId string

Returns

  • CfnNatGateway

Creates a new managed NAT gateway attached to this public subnet.

Also adds the EIP for the managed NAT.


addRoute(id, options)

public addRoute(id: string, options: AddRouteOptions): void

Parameters

  • id string
  • options AddRouteOptions

Adds an entry to this subnets route table.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

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).


associateNetworkAcl(id, networkAcl)

public associateNetworkAcl(id: string, networkAcl: INetworkAcl): void

Parameters

  • id string
  • networkAcl INetworkAcl

Associate a Network ACL with this subnet.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromPublicSubnetAttributes(scope, id, attrs)

public static fromPublicSubnetAttributes(scope: Construct, id: string, attrs: PublicSubnetAttributes): IPublicSubnet

Parameters

  • scope Construct
  • id string
  • attrs PublicSubnetAttributes

Returns

  • IPublicSubnet