# PublicSubnet

Represents a public VPC subnet resource.

Extends: Construct › Resource › Subnet › PublicSubnet
Implements: IResource, IPublicSubnet, ISubnet, IEnvironmentAware, ISubnetRef, IConstruct, IDependable

- Source: [`aws-ec2/lib/vpc.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts#L2546)
- Type last changed: v2.272.0 · 30 Sep 2026

## Example

```ts
// 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
  assignIpv6AddressOnCreation: false,
  ipv6CidrBlock: 'ipv6CidrBlock',
  mapPublicIpOnLaunch: false,
});
```

## Initializer

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

## Construct Props

- `assignIpv6AddressOnCreation`: boolean = false — Indicates whether a network interface created in this subnet receives an IPv6 address.

  If you specify AssignIpv6AddressOnCreation, you must also specify Ipv6CidrBlock.

- `availabilityZone`: string (required) — The availability zone for the subnet.

- `cidrBlock`: string (required) — The CIDR notation for this subnet.

- `ipv6CidrBlock`: string = no IPv6 CIDR block. — The IPv6 CIDR block.

  If you specify AssignIpv6AddressOnCreation, you must also specify Ipv6CidrBlock.

- `mapPublicIpOnLaunch`: boolean = true in Subnet.Public, false in Subnet.Private or Subnet.Isolated. — Controls if a public IP is associated to an instance at launch.

  **Default:** true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.

- `vpcId`: string (required) — The VPC which this subnet is part of.

## Properties

- `availabilityZone`: string (required) — The Availability Zone the subnet is located in.

- `dependencyElements`: [IDependable](/cdk/api/v2/docs/constructs.IDependable.html)[] (required) — Parts of this VPC subnet.

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) (required) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `internetConnectivityEstablished`: [IDependable](/cdk/api/v2/docs/constructs.IDependable.html) (required) — Dependable that can be depended upon to force internet connectivity established on the VPC.

- `ipv4CidrBlock`: string (required) — The IPv4 CIDR block for this subnet.

- `networkAcl`: [INetworkAcl](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.INetworkAcl.html) (required) — 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`: [Node](/cdk/api/v2/docs/constructs.Node.html) (required) — The tree node.

- `PROPERTY_INJECTION_ID`: string (required) (static) — Uniquely identifies this class.

- `routeTable`: [IRouteTable](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IRouteTable.html) (required) — The routeTableId attached to this subnet.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) (required) — The stack in which this resource is defined.

- `subnetAvailabilityZone`: string (required)

- `subnetId`: string (required) — The subnetId for this particular subnet.

- `subnetIpv6CidrBlocks`: string[] (required)

- `subnetNetworkAclAssociationId`: string (required)

- `subnetOutpostArn`: string (required) — The Amazon Resource Name (ARN) of the Outpost for this subnet (if one exists).

- `subnetRef`: [SubnetReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.SubnetReference.html) (required) — A reference to a Subnet resource.

- `subnetVpcId`: string (required)

## Methods

- `addDefaultInternetRoute(gatewayId: string, gatewayAttachment: IDependable): void` — Create a default route that points to a passed IGW, with a dependency on the IGW's attachment to the VPC.

  **Parameters**

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

- `addDefaultNatRoute(natGatewayId: string): void` — Adds an entry to this subnets route table that points to the passed NATGatewayId.

  **Parameters**

  - `natGatewayId` — The ID of the NAT gateway.

- `addIpv6DefaultEgressOnlyInternetRoute(gatewayId: string): void` — Create a default IPv6 route that points to a passed EIGW.

  **Parameters**

  - `gatewayId` — the logical ID (ref) of the gateway attached to your VPC.

- `addIpv6DefaultInternetRoute(gatewayId: string, gatewayAttachment?: IDependable): void` — Create a default IPv6 route that points to a passed IGW, with a dependency on the IGW's attachment to the VPC.

  **Parameters**

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

- `addIpv6Nat64Route(natGatewayId: string): void` — Adds an entry to this subnets route table that points to the passed NATGatewayId.

  Uses the known 64:ff9b::/96 prefix.

  **Parameters**

  - `natGatewayId` — The ID of the NAT gateway.

- `addNatGateway(eipAllocationId?: string, options?: AddNatGatewayOptions): CfnNatGateway` — Creates a new managed NAT gateway attached to this public subnet.

  Also adds the EIP for the managed NAT.

  **Parameters**

  - `eipAllocationId` — Allocation ID of an Elastic IP address to assign to the NAT gateway.
  - `options` — Additional options for the NAT gateway.

  **Returns:** A ref to the NAT Gateway ID

- `addRoute(id: string, options: AddRouteOptions): void` — Adds an entry to this subnets route table.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — 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: string, networkAcl: INetworkAcl): void` — Associate a Network ACL with this subnet.

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

- `fromSubnetAttributes(scope: Construct, id: string, attrs: SubnetAttributes): ISubnet` (static)

- `fromSubnetId(scope: Construct, id: string, subnetId: string): ISubnet` (static) — Import existing subnet from id.

- `isConstruct(x: any): boolean` (static) — 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 object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `isVpcSubnet(x: any): boolean` (static)

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
