java.lang.Object
software.amazon.jsii.JsiiObject
software.constructs.Construct
software.amazon.awscdk.Resource
software.amazon.awscdk.services.ec2.Vpc
All Implemented Interfaces:
IResource, IVpc, software.amazon.jsii.JsiiSerializable, software.constructs.IConstruct, software.constructs.IDependable

@Generated(value="jsii-pacmak/1.104.0 (build e79254c)", date="2024-11-27T16:50:56.733Z") @Stability(Stable) public class Vpc extends Resource implements IVpc
Define an AWS Virtual Private Cloud.

See the package-level documentation of this package for an overview of the various dimensions in which you can configure your VPC.

For example:

 Vpc vpc = Vpc.Builder.create(this, "TheVPC")
         .ipAddresses(IpAddresses.cidr("10.0.0.0/16"))
         .build();
 // Iterate the private subnets
 SelectedSubnets selection = vpc.selectSubnets(SubnetSelection.builder()
         .subnetType(SubnetType.PRIVATE_WITH_EGRESS)
         .build());
 for (Object subnet : selection.getSubnets()) {
 }
 

Example:

 import software.amazon.awscdk.services.ec2.*;
 Vpc vpc = Vpc.Builder.create(this, "Vpc")
         .ipAddresses(IpAddresses.cidr("10.0.0.0/16"))
         .build();
 VpcConnector vpcConnector = VpcConnector.Builder.create(this, "VpcConnector")
         .vpc(vpc)
         .vpcSubnets(vpc.selectSubnets(SubnetSelection.builder().subnetType(SubnetType.PUBLIC).build()))
         .vpcConnectorName("MyVpcConnector")
         .build();
 Service.Builder.create(this, "Service")
         .source(Source.fromEcrPublic(EcrPublicProps.builder()
                 .imageConfiguration(ImageConfiguration.builder().port(8000).build())
                 .imageIdentifier("public.ecr.aws/aws-containers/hello-app-runner:latest")
                 .build()))
         .vpcConnector(vpcConnector)
         .build();
 
  • Field Details

    • DEFAULT_CIDR_RANGE

      @Stability(Stable) public static final String DEFAULT_CIDR_RANGE
      The default CIDR range used when creating VPCs.

      This can be overridden using VpcProps when creating a VPCNetwork resource. e.g. new VpcResource(this, { cidr: '192.168.0.0./16' })

      Note this is specific to the IPv4 CIDR.

    • DEFAULT_SUBNETS

      @Stability(Stable) public static final List<SubnetConfiguration> DEFAULT_SUBNETS
      The default subnet configuration.

      1 Public and 1 Private subnet per AZ evenly split

    • DEFAULT_SUBNETS_NO_NAT

      @Stability(Stable) public static final List<SubnetConfiguration> DEFAULT_SUBNETS_NO_NAT
      The default subnet configuration if natGateways specified to be 0.

      1 Public and 1 Isolated Subnet per AZ evenly split

  • Constructor Details

    • Vpc

      protected Vpc(software.amazon.jsii.JsiiObjectRef objRef)
    • Vpc

      protected Vpc(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
    • Vpc

      @Stability(Stable) public Vpc(@NotNull software.constructs.Construct scope, @NotNull String id, @Nullable VpcProps props)
      Vpc creates a VPC that spans a whole region.

      It will automatically divide the provided VPC CIDR range, and create public and private subnets per Availability Zone. Network routing for the public subnets will be configured to allow outbound access directly via an Internet Gateway. Network routing for the private subnets will be configured to allow outbound access via a set of resilient NAT Gateways (one per AZ).

      Parameters:
      scope - This parameter is required.
      id - This parameter is required.
      props -
    • Vpc

      @Stability(Stable) public Vpc(@NotNull software.constructs.Construct scope, @NotNull String id)
      Vpc creates a VPC that spans a whole region.

      It will automatically divide the provided VPC CIDR range, and create public and private subnets per Availability Zone. Network routing for the public subnets will be configured to allow outbound access directly via an Internet Gateway. Network routing for the private subnets will be configured to allow outbound access via a set of resilient NAT Gateways (one per AZ).

      Parameters:
      scope - This parameter is required.
      id - This parameter is required.
  • Method Details

    • fromLookup

      @Stability(Stable) @NotNull public static IVpc fromLookup(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull VpcLookupOptions options)
      Import an existing VPC by querying the AWS environment this stack is deployed to.

      This function only needs to be used to use VPCs not defined in your CDK application. If you are looking to share a VPC between stacks, you can pass the Vpc object between stacks and use it as normal.

      Calling this method will lead to a lookup when the CDK CLI is executed. You can therefore not use any values that will only be available at CloudFormation execution time (i.e., Tokens).

      The VPC information will be cached in cdk.context.json and the same VPC will be used on future runs. To refresh the lookup, you will have to evict the value from the cache using the cdk context command. See https://docs.aws.amazon.com/cdk/latest/guide/context.html for more information.

      Parameters:
      scope - This parameter is required.
      id - This parameter is required.
      options - This parameter is required.
    • fromVpcAttributes

      @Stability(Stable) @NotNull public static IVpc fromVpcAttributes(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull VpcAttributes attrs)
      Import a VPC by supplying all attributes directly.

      NOTE: using fromVpcAttributes() with deploy-time parameters (like a Fn.importValue() or CfnParameter to represent a list of subnet IDs) sometimes accidentally works. It happens to work for constructs that need a list of subnets (like AutoScalingGroup and eks.Cluster) but it does not work for constructs that need individual subnets (like Instance). See https://github.com/aws/aws-cdk/issues/4118 for more information.

      Prefer to use Vpc.fromLookup() instead.

      Parameters:
      scope - This parameter is required.
      id - This parameter is required.
      attrs - This parameter is required.
    • addClientVpnEndpoint

      @Stability(Stable) @NotNull public ClientVpnEndpoint addClientVpnEndpoint(@NotNull String id, @NotNull ClientVpnEndpointOptions options)
      Adds a new client VPN endpoint to this VPC.

      Specified by:
      addClientVpnEndpoint in interface IVpc
      Parameters:
      id - This parameter is required.
      options - This parameter is required.
    • addFlowLog

      @Stability(Stable) @NotNull public FlowLog addFlowLog(@NotNull String id, @Nullable FlowLogOptions options)
      Adds a new flow log to this VPC.

      Specified by:
      addFlowLog in interface IVpc
      Parameters:
      id - This parameter is required.
      options -
    • addFlowLog

      @Stability(Stable) @NotNull public FlowLog addFlowLog(@NotNull String id)
      Adds a new flow log to this VPC.

      Specified by:
      addFlowLog in interface IVpc
      Parameters:
      id - This parameter is required.
    • addGatewayEndpoint

      @Stability(Stable) @NotNull public GatewayVpcEndpoint addGatewayEndpoint(@NotNull String id, @NotNull GatewayVpcEndpointOptions options)
      Adds a new gateway endpoint to this VPC.

      Specified by:
      addGatewayEndpoint in interface IVpc
      Parameters:
      id - This parameter is required.
      options - This parameter is required.
    • addInterfaceEndpoint

      @Stability(Stable) @NotNull public InterfaceVpcEndpoint addInterfaceEndpoint(@NotNull String id, @NotNull InterfaceVpcEndpointOptions options)
      Adds a new interface endpoint to this VPC.

      Specified by:
      addInterfaceEndpoint in interface IVpc
      Parameters:
      id - This parameter is required.
      options - This parameter is required.
    • addVpnConnection

      @Stability(Stable) @NotNull public VpnConnection addVpnConnection(@NotNull String id, @NotNull VpnConnectionOptions options)
      Adds a new VPN connection to this VPC.

      Specified by:
      addVpnConnection in interface IVpc
      Parameters:
      id - This parameter is required.
      options - This parameter is required.
    • enableVpnGateway

      @Stability(Stable) public void enableVpnGateway(@NotNull EnableVpnGatewayOptions options)
      Adds a VPN Gateway to this VPC.

      Specified by:
      enableVpnGateway in interface IVpc
      Parameters:
      options - This parameter is required.
    • selectSubnetObjects

      @Stability(Stable) @NotNull protected List<ISubnet> selectSubnetObjects(@Nullable SubnetSelection selection)
      Return the subnets appropriate for the placement strategy.

      Parameters:
      selection -
    • selectSubnetObjects

      @Stability(Stable) @NotNull protected List<ISubnet> selectSubnetObjects()
      Return the subnets appropriate for the placement strategy.
    • selectSubnets

      @Stability(Stable) @NotNull public SelectedSubnets selectSubnets(@Nullable SubnetSelection selection)
      Returns IDs of selected subnets.

      Specified by:
      selectSubnets in interface IVpc
      Parameters:
      selection -
    • selectSubnets

      @Stability(Stable) @NotNull public SelectedSubnets selectSubnets()
      Returns IDs of selected subnets.
      Specified by:
      selectSubnets in interface IVpc
    • getAvailabilityZones

      @Stability(Stable) @NotNull public List<String> getAvailabilityZones()
      AZs for this VPC.
      Specified by:
      getAvailabilityZones in interface IVpc
    • getDnsHostnamesEnabled

      @Stability(Stable) @NotNull public Boolean getDnsHostnamesEnabled()
      Indicates if instances launched in this VPC will have public DNS hostnames.
    • getDnsSupportEnabled

      @Stability(Stable) @NotNull public Boolean getDnsSupportEnabled()
      Indicates if DNS support is enabled for this VPC.
    • getInternetConnectivityEstablished

      @Stability(Stable) @NotNull public software.constructs.IDependable getInternetConnectivityEstablished()
      Dependencies for internet connectivity.
      Specified by:
      getInternetConnectivityEstablished in interface IVpc
    • getIsolatedSubnets

      @Stability(Stable) @NotNull public List<ISubnet> getIsolatedSubnets()
      List of isolated subnets in this VPC.
      Specified by:
      getIsolatedSubnets in interface IVpc
    • getPrivateSubnets

      @Stability(Stable) @NotNull public List<ISubnet> getPrivateSubnets()
      List of private subnets in this VPC.
      Specified by:
      getPrivateSubnets in interface IVpc
    • getPublicSubnets

      @Stability(Stable) @NotNull public List<ISubnet> getPublicSubnets()
      List of public subnets in this VPC.
      Specified by:
      getPublicSubnets in interface IVpc
    • getVpcArn

      @Stability(Stable) @NotNull public String getVpcArn()
      Arn of this VPC.
      Specified by:
      getVpcArn in interface IVpc
    • getVpcCidrBlock

      @Stability(Stable) @NotNull public String getVpcCidrBlock()
      CIDR range for this VPC.
      Specified by:
      getVpcCidrBlock in interface IVpc
    • getVpcCidrBlockAssociations

      @Stability(Stable) @NotNull public List<String> getVpcCidrBlockAssociations()
    • getVpcDefaultNetworkAcl

      @Stability(Stable) @NotNull public String getVpcDefaultNetworkAcl()
    • getVpcDefaultSecurityGroup

      @Stability(Stable) @NotNull public String getVpcDefaultSecurityGroup()
    • getVpcId

      @Stability(Stable) @NotNull public String getVpcId()
      Identifier for this VPC.
      Specified by:
      getVpcId in interface IVpc
    • getVpcIpv6CidrBlocks

      @Stability(Stable) @NotNull public List<String> getVpcIpv6CidrBlocks()
    • getInternetGatewayId

      @Stability(Stable) @Nullable public String getInternetGatewayId()
      Internet Gateway for the VPC.

      Note that in case the VPC is configured only with ISOLATED subnets, this attribute will be undefined.

    • getVpnGatewayId

      @Stability(Stable) @Nullable public String getVpnGatewayId()
      Returns the id of the VPN Gateway (if enabled).
      Specified by:
      getVpnGatewayId in interface IVpc
    • getIncompleteSubnetDefinition

      @Stability(Stable) @NotNull protected Boolean getIncompleteSubnetDefinition()
      If this is set to true, don't error out on trying to select subnets.
    • setIncompleteSubnetDefinition

      @Stability(Stable) protected void setIncompleteSubnetDefinition(@NotNull Boolean value)
      If this is set to true, don't error out on trying to select subnets.