Class Vpc
Define an AWS Virtual Private Cloud.
Inherited Members
Namespace: Amazon.CDK.AWS.EC2
Assembly: Amazon.CDK.AWS.EC2.dll
Syntax (csharp)
public class Vpc : Resource, IVpc, IResource, IConstruct, IDependable
Syntax (vb)
Public Class Vpc
Inherits Resource
Implements IVpc, IResource, IConstruct, IDependable
Remarks
See the package-level documentation of this package for an overview of the various dimensions in which you can configure your VPC.
For example:
var vpc = new Vpc(this, "TheVPC", new VpcProps {
Cidr = "10.0.0.0/16"
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_NAT
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
Examples
using Amazon.CDK.AWS.EC2;
var vpc = new Vpc(this, "Vpc", new VpcProps {
Cidr = "10.0.0.0/16"
});
var vpcConnector = new VpcConnector(this, "VpcConnector", new VpcConnectorProps {
Vpc = vpc,
VpcSubnets = vpc.SelectSubnets(new SubnetSelection { SubnetType = SubnetType.PUBLIC }),
VpcConnectorName = "MyVpcConnector"
});
new Service(this, "Service", new ServiceProps {
Source = Source.FromEcrPublic(new EcrPublicProps {
ImageConfiguration = new ImageConfiguration { Port = 8000 },
ImageIdentifier = "public.ecr.aws/aws-containers/hello-app-runner:latest"
}),
VpcConnector = vpcConnector
});
Synopsis
Constructors
Vpc(ByRefValue) | Used by jsii to construct an instance of this class from a Javascript-owned object reference |
Vpc(DeputyBase.DeputyProps) | Used by jsii to construct an instance of this class from DeputyProps |
Vpc(Construct, String, IVpcProps) | Vpc creates a VPC that spans a whole region. |
Properties
AvailabilityZones | AZs for this VPC. |
DEFAULT_CIDR_RANGE | The default CIDR range used when creating VPCs. |
DEFAULT_SUBNETS | The default subnet configuration. |
DEFAULT_SUBNETS_NO_NAT | The default subnet configuration if natGateways specified to be 0. |
DnsHostnamesEnabled | Indicates if instances launched in this VPC will have public DNS hostnames. |
DnsSupportEnabled | Indicates if DNS support is enabled for this VPC. |
IncompleteSubnetDefinition | If this is set to true, don't error out on trying to select subnets. |
InternetConnectivityEstablished | Dependencies for internet connectivity. |
InternetGatewayId | Internet Gateway for the VPC. |
IsolatedSubnets | List of isolated subnets in this VPC. |
NatDependencies | (deprecated) Dependencies for NAT connectivity. |
PrivateSubnets | List of private subnets in this VPC. |
PublicSubnets | List of public subnets in this VPC. |
VpcArn | Arn of this VPC. |
VpcCidrBlock | CIDR range for this VPC. |
VpcCidrBlockAssociations | |
VpcDefaultNetworkAcl | |
VpcDefaultSecurityGroup | |
VpcId | Identifier for this VPC. |
VpcIpv6CidrBlocks | |
VpnGatewayId | Returns the id of the VPN Gateway (if enabled). |
Methods
AddClientVpnEndpoint(String, IClientVpnEndpointOptions) | Adds a new client VPN endpoint to this VPC. |
AddDynamoDbEndpoint(String, ISubnetSelection[]) | (deprecated) Adds a new DynamoDB gateway endpoint to this VPC. |
AddFlowLog(String, IFlowLogOptions) | Adds a new flow log to this VPC. |
AddGatewayEndpoint(String, IGatewayVpcEndpointOptions) | Adds a new gateway endpoint to this VPC. |
AddInterfaceEndpoint(String, IInterfaceVpcEndpointOptions) | Adds a new interface endpoint to this VPC. |
AddS3Endpoint(String, ISubnetSelection[]) | (deprecated) Adds a new S3 gateway endpoint to this VPC. |
AddVpnConnection(String, IVpnConnectionOptions) | Adds a new VPN connection to this VPC. |
EnableVpnGateway(IEnableVpnGatewayOptions) | Adds a VPN Gateway to this VPC. |
FromLookup(Construct, String, IVpcLookupOptions) | Import an existing VPC from by querying the AWS environment this stack is deployed to. |
FromVpcAttributes(Construct, String, IVpcAttributes) | Import a VPC by supplying all attributes directly. |
SelectSubnetObjects(ISubnetSelection) | Return the subnets appropriate for the placement strategy. |
SelectSubnets(ISubnetSelection) | Returns IDs of selected subnets. |
Constructors
Vpc(ByRefValue)
Used by jsii to construct an instance of this class from a Javascript-owned object reference
protected Vpc(ByRefValue reference)
Parameters
- reference Amazon.JSII.Runtime.Deputy.ByRefValue
The Javascript-owned object reference
Vpc(DeputyBase.DeputyProps)
Used by jsii to construct an instance of this class from DeputyProps
protected Vpc(DeputyBase.DeputyProps props)
Parameters
- props Amazon.JSII.Runtime.Deputy.DeputyBase.DeputyProps
The deputy props
Vpc(Construct, String, IVpcProps)
Vpc creates a VPC that spans a whole region.
public Vpc(Construct scope, string id, IVpcProps props = null)
Parameters
- scope Constructs.Construct
- id System.String
- props IVpcProps
Remarks
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).
Properties
AvailabilityZones
AZs for this VPC.
public virtual string[] AvailabilityZones { get; }
Property Value
System.String[]
DEFAULT_CIDR_RANGE
The default CIDR range used when creating VPCs.
public static string DEFAULT_CIDR_RANGE { get; }
Property Value
System.String
Remarks
This can be overridden using VpcProps when creating a VPCNetwork resource. e.g. new VpcResource(this, { cidr: '192.168.0.0./16' })
DEFAULT_SUBNETS
The default subnet configuration.
public static ISubnetConfiguration[] DEFAULT_SUBNETS { get; }
Property Value
Remarks
1 Public and 1 Private subnet per AZ evenly split
DEFAULT_SUBNETS_NO_NAT
The default subnet configuration if natGateways specified to be 0.
public static ISubnetConfiguration[] DEFAULT_SUBNETS_NO_NAT { get; }
Property Value
Remarks
1 Public and 1 Isolated Subnet per AZ evenly split
DnsHostnamesEnabled
Indicates if instances launched in this VPC will have public DNS hostnames.
public virtual bool DnsHostnamesEnabled { get; }
Property Value
System.Boolean
DnsSupportEnabled
Indicates if DNS support is enabled for this VPC.
public virtual bool DnsSupportEnabled { get; }
Property Value
System.Boolean
IncompleteSubnetDefinition
If this is set to true, don't error out on trying to select subnets.
protected virtual bool IncompleteSubnetDefinition { get; set; }
Property Value
System.Boolean
InternetConnectivityEstablished
Dependencies for internet connectivity.
public virtual IDependable InternetConnectivityEstablished { get; }
Property Value
InternetGatewayId
Internet Gateway for the VPC.
public virtual string InternetGatewayId { get; }
Property Value
System.String
Remarks
Note that in case the VPC is configured only
with ISOLATED subnets, this attribute will be undefined
.
IsolatedSubnets
List of isolated subnets in this VPC.
public virtual ISubnet[] IsolatedSubnets { get; }
Property Value
ISubnet[]
NatDependencies
(deprecated) Dependencies for NAT connectivity.
protected virtual IConstruct[] NatDependencies { get; }
Property Value
Remarks
Stability: Deprecated
PrivateSubnets
List of private subnets in this VPC.
public virtual ISubnet[] PrivateSubnets { get; }
Property Value
ISubnet[]
PublicSubnets
List of public subnets in this VPC.
public virtual ISubnet[] PublicSubnets { get; }
Property Value
ISubnet[]
VpcArn
Arn of this VPC.
public virtual string VpcArn { get; }
Property Value
System.String
Remarks
Attribute: true
VpcCidrBlock
CIDR range for this VPC.
public virtual string VpcCidrBlock { get; }
Property Value
System.String
Remarks
Attribute: true
VpcCidrBlockAssociations
public virtual string[] VpcCidrBlockAssociations { get; }
Property Value
System.String[]
Remarks
Attribute: true
VpcDefaultNetworkAcl
public virtual string VpcDefaultNetworkAcl { get; }
Property Value
System.String
Remarks
Attribute: true
VpcDefaultSecurityGroup
public virtual string VpcDefaultSecurityGroup { get; }
Property Value
System.String
Remarks
Attribute: true
VpcId
Identifier for this VPC.
public virtual string VpcId { get; }
Property Value
System.String
VpcIpv6CidrBlocks
public virtual string[] VpcIpv6CidrBlocks { get; }
Property Value
System.String[]
Remarks
Attribute: true
VpnGatewayId
Returns the id of the VPN Gateway (if enabled).
public virtual string VpnGatewayId { get; }
Property Value
System.String
Methods
AddClientVpnEndpoint(String, IClientVpnEndpointOptions)
Adds a new client VPN endpoint to this VPC.
public virtual ClientVpnEndpoint AddClientVpnEndpoint(string id, IClientVpnEndpointOptions options)
Parameters
- id System.String
- options IClientVpnEndpointOptions
Returns
AddDynamoDbEndpoint(String, ISubnetSelection[])
(deprecated) Adds a new DynamoDB gateway endpoint to this VPC.
public virtual GatewayVpcEndpoint AddDynamoDbEndpoint(string id, ISubnetSelection[] subnets = null)
Parameters
- id System.String
- subnets ISubnetSelection[]
Returns
Remarks
Stability: Deprecated
AddFlowLog(String, IFlowLogOptions)
Adds a new flow log to this VPC.
public virtual FlowLog AddFlowLog(string id, IFlowLogOptions options = null)
Parameters
- id System.String
- options IFlowLogOptions
Returns
AddGatewayEndpoint(String, IGatewayVpcEndpointOptions)
Adds a new gateway endpoint to this VPC.
public virtual GatewayVpcEndpoint AddGatewayEndpoint(string id, IGatewayVpcEndpointOptions options)
Parameters
- id System.String
- options IGatewayVpcEndpointOptions
Returns
AddInterfaceEndpoint(String, IInterfaceVpcEndpointOptions)
Adds a new interface endpoint to this VPC.
public virtual InterfaceVpcEndpoint AddInterfaceEndpoint(string id, IInterfaceVpcEndpointOptions options)
Parameters
- id System.String
- options IInterfaceVpcEndpointOptions
Returns
AddS3Endpoint(String, ISubnetSelection[])
(deprecated) Adds a new S3 gateway endpoint to this VPC.
public virtual GatewayVpcEndpoint AddS3Endpoint(string id, ISubnetSelection[] subnets = null)
Parameters
- id System.String
- subnets ISubnetSelection[]
Returns
Remarks
Stability: Deprecated
AddVpnConnection(String, IVpnConnectionOptions)
Adds a new VPN connection to this VPC.
public virtual VpnConnection AddVpnConnection(string id, IVpnConnectionOptions options)
Parameters
- id System.String
- options IVpnConnectionOptions
Returns
EnableVpnGateway(IEnableVpnGatewayOptions)
Adds a VPN Gateway to this VPC.
public virtual void EnableVpnGateway(IEnableVpnGatewayOptions options)
Parameters
- options IEnableVpnGatewayOptions
FromLookup(Construct, String, IVpcLookupOptions)
Import an existing VPC from by querying the AWS environment this stack is deployed to.
public static IVpc FromLookup(Construct scope, string id, IVpcLookupOptions options)
Parameters
- scope Constructs.Construct
- id System.String
- options IVpcLookupOptions
Returns
Remarks
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.
FromVpcAttributes(Construct, String, IVpcAttributes)
Import a VPC by supplying all attributes directly.
public static IVpc FromVpcAttributes(Construct scope, string id, IVpcAttributes attrs)
Parameters
- scope Constructs.Construct
- id System.String
- attrs IVpcAttributes
Returns
Remarks
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.
SelectSubnetObjects(ISubnetSelection)
Return the subnets appropriate for the placement strategy.
protected virtual ISubnet[] SelectSubnetObjects(ISubnetSelection selection = null)
Parameters
- selection ISubnetSelection
Returns
ISubnet[]
SelectSubnets(ISubnetSelection)
Returns IDs of selected subnets.
public virtual ISelectedSubnets SelectSubnets(ISubnetSelection selection = null)
Parameters
- selection ISubnetSelection
Returns