Class Vpc
- All Implemented Interfaces:
IResource
,IVpc
,software.amazon.jsii.JsiiSerializable
,software.constructs.IConstruct
,software.constructs.IDependable
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();
-
Nested Class Summary
Nested classes/interfaces inherited from class software.amazon.jsii.JsiiObject
software.amazon.jsii.JsiiObject.InitializationMode
Nested classes/interfaces inherited from interface software.constructs.IConstruct
software.constructs.IConstruct.Jsii$Default
Nested classes/interfaces inherited from interface software.amazon.awscdk.IResource
IResource.Jsii$Default
Nested classes/interfaces inherited from interface software.amazon.awscdk.services.ec2.IVpc
IVpc.Jsii$Default, IVpc.Jsii$Proxy
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The default CIDR range used when creating VPCs.static final List<SubnetConfiguration>
The default subnet configuration.static final List<SubnetConfiguration>
The default subnet configuration if natGateways specified to be 0. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddClientVpnEndpoint
(String id, ClientVpnEndpointOptions options) Adds a new client VPN endpoint to this VPC.addFlowLog
(String id) Adds a new flow log to this VPC.addFlowLog
(String id, FlowLogOptions options) Adds a new flow log to this VPC.addGatewayEndpoint
(String id, GatewayVpcEndpointOptions options) Adds a new gateway endpoint to this VPC.addInterfaceEndpoint
(String id, InterfaceVpcEndpointOptions options) Adds a new interface endpoint to this VPC.addVpnConnection
(String id, VpnConnectionOptions options) Adds a new VPN connection to this VPC.void
enableVpnGateway
(EnableVpnGatewayOptions options) Adds a VPN Gateway to this VPC.static IVpc
fromLookup
(software.constructs.Construct scope, String id, VpcLookupOptions options) Import an existing VPC by querying the AWS environment this stack is deployed to.static IVpc
fromVpcAttributes
(software.constructs.Construct scope, String id, VpcAttributes attrs) Import a VPC by supplying all attributes directly.AZs for this VPC.Indicates if instances launched in this VPC will have public DNS hostnames.Indicates if DNS support is enabled for this VPC.protected Boolean
If this is set to true, don't error out on trying to select subnets.software.constructs.IDependable
Dependencies for internet connectivity.Internet Gateway for the VPC.List of isolated subnets in this VPC.List of private subnets in this VPC.List of public subnets in this VPC.Arn of this VPC.CIDR range for this VPC.getVpcId()
Identifier for this VPC.Returns the id of the VPN Gateway (if enabled).Return the subnets appropriate for the placement strategy.selectSubnetObjects
(SubnetSelection selection) Return the subnets appropriate for the placement strategy.Returns IDs of selected subnets.selectSubnets
(SubnetSelection selection) Returns IDs of selected subnets.protected void
If this is set to true, don't error out on trying to select subnets.Methods inherited from class software.amazon.awscdk.Resource
applyRemovalPolicy, generatePhysicalName, getEnv, getPhysicalName, getResourceArnAttribute, getResourceNameAttribute, getStack, isOwnedResource, isResource
Methods inherited from class software.constructs.Construct
getNode, isConstruct, toString
Methods inherited from class software.amazon.jsii.JsiiObject
jsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSet
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface software.constructs.IConstruct
getNode
Methods inherited from interface software.amazon.awscdk.IResource
applyRemovalPolicy, getEnv, getStack
Methods inherited from interface software.amazon.jsii.JsiiSerializable
$jsii$toJson
-
Field Details
-
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
The default subnet configuration.1 Public and 1 Private subnet per AZ evenly split
-
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
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 thecdk 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 aFn.importValue()
orCfnParameter
to represent a list of subnet IDs) sometimes accidentally works. It happens to work for constructs that need a list of subnets (likeAutoScalingGroup
andeks.Cluster
) but it does not work for constructs that need individual subnets (likeInstance
). 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 interfaceIVpc
- 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 interfaceIVpc
- Parameters:
id
- This parameter is required.options
-
-
addFlowLog
Adds a new flow log to this VPC.- Specified by:
addFlowLog
in interfaceIVpc
- 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 interfaceIVpc
- 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 interfaceIVpc
- 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 interfaceIVpc
- Parameters:
id
- This parameter is required.options
- This parameter is required.
-
enableVpnGateway
Adds a VPN Gateway to this VPC.- Specified by:
enableVpnGateway
in interfaceIVpc
- 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
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 interfaceIVpc
- Parameters:
selection
-
-
selectSubnets
Returns IDs of selected subnets.- Specified by:
selectSubnets
in interfaceIVpc
-
getAvailabilityZones
AZs for this VPC.- Specified by:
getAvailabilityZones
in interfaceIVpc
-
getDnsHostnamesEnabled
Indicates if instances launched in this VPC will have public DNS hostnames. -
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 interfaceIVpc
-
getIsolatedSubnets
List of isolated subnets in this VPC.- Specified by:
getIsolatedSubnets
in interfaceIVpc
-
getPrivateSubnets
List of private subnets in this VPC.- Specified by:
getPrivateSubnets
in interfaceIVpc
-
getPublicSubnets
List of public subnets in this VPC.- Specified by:
getPublicSubnets
in interfaceIVpc
-
getVpcArn
Arn of this VPC. -
getVpcCidrBlock
CIDR range for this VPC.- Specified by:
getVpcCidrBlock
in interfaceIVpc
-
getVpcCidrBlockAssociations
-
getVpcDefaultNetworkAcl
-
getVpcDefaultSecurityGroup
-
getVpcId
Identifier for this VPC. -
getVpcIpv6CidrBlocks
-
getInternetGatewayId
Internet Gateway for the VPC.Note that in case the VPC is configured only with ISOLATED subnets, this attribute will be
undefined
. -
getVpnGatewayId
Returns the id of the VPN Gateway (if enabled).- Specified by:
getVpnGatewayId
in interfaceIVpc
-
getIncompleteSubnetDefinition
If this is set to true, don't error out on trying to select subnets. -
setIncompleteSubnetDefinition
If this is set to true, don't error out on trying to select subnets.
-