Class Vpc
Define an AWS Virtual Private Cloud.
Inherited Members
Namespace: Amazon.CDK.AWS.EC2
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public class Vpc : Resource, IVpc, IResource, IVPCRef, IConstruct, IDependable, IEnvironmentAware
Syntax (vb)
Public Class Vpc Inherits Resource Implements IVpc, IResource, IVPCRef, IConstruct, IDependable, IEnvironmentAware
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
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 {
IpAddresses = IpAddresses.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(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. |
| PROPERTY_INJECTION_ID | Uniquely identifies this class. |
| 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 | Define an AWS Virtual Private Cloud. |
| VpcDefaultNetworkAcl | Define an AWS Virtual Private Cloud. |
| VpcDefaultSecurityGroup | Define an AWS Virtual Private Cloud. |
| VpcId | Identifier for this VPC. |
| VpcIpv6CidrBlocks | Define an AWS Virtual Private Cloud. |
| VpcRef | A reference to a VPC resource. |
| VpnGatewayId | Returns the id of the VPN Gateway (if enabled). |
Methods
| AddClientVpnEndpoint(string, IClientVpnEndpointOptions) | Adds a new client VPN 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. |
| 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 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(Construct, string, IVpcProps?)
Vpc creates a VPC that spans a whole region.
public Vpc(Construct scope, string id, IVpcProps? props = null)
Parameters
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
string[]
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
DEFAULT_CIDR_RANGE
The default CIDR range used when creating VPCs.
public static string DEFAULT_CIDR_RANGE { get; }
Property Value
Remarks
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.
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
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
DnsSupportEnabled
Indicates if DNS support is enabled for this VPC.
public virtual bool DnsSupportEnabled { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
IncompleteSubnetDefinition
If this is set to true, don't error out on trying to select subnets.
protected virtual bool IncompleteSubnetDefinition { get; set; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
InternetConnectivityEstablished
Dependencies for internet connectivity.
public virtual IDependable InternetConnectivityEstablished { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
InternetGatewayId
Internet Gateway for the VPC.
public virtual string? InternetGatewayId { get; }
Property Value
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[]
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
PROPERTY_INJECTION_ID
Uniquely identifies this class.
public static string PROPERTY_INJECTION_ID { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
PrivateSubnets
List of private subnets in this VPC.
public virtual ISubnet[] PrivateSubnets { get; }
Property Value
ISubnet[]
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
PublicSubnets
List of public subnets in this VPC.
public virtual ISubnet[] PublicSubnets { get; }
Property Value
ISubnet[]
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
VpcArn
Arn of this VPC.
public virtual string VpcArn { get; }
Property Value
Remarks
Attribute: true
VpcCidrBlock
CIDR range for this VPC.
public virtual string VpcCidrBlock { get; }
Property Value
Remarks
Attribute: true
VpcCidrBlockAssociations
Define an AWS Virtual Private Cloud.
public virtual string[] VpcCidrBlockAssociations { get; }
Property Value
string[]
Remarks
Attribute: true
VpcDefaultNetworkAcl
Define an AWS Virtual Private Cloud.
public virtual string VpcDefaultNetworkAcl { get; }
Property Value
Remarks
Attribute: true
VpcDefaultSecurityGroup
Define an AWS Virtual Private Cloud.
public virtual string VpcDefaultSecurityGroup { get; }
Property Value
Remarks
Attribute: true
VpcId
Identifier for this VPC.
public virtual string VpcId { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
VpcIpv6CidrBlocks
Define an AWS Virtual Private Cloud.
public virtual string[] VpcIpv6CidrBlocks { get; }
Property Value
string[]
Remarks
Attribute: true
VpcRef
A reference to a VPC resource.
public virtual IVPCReference VpcRef { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
VpnGatewayId
Returns the id of the VPN Gateway (if enabled).
public virtual string? VpnGatewayId { get; }
Property Value
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
Methods
AddClientVpnEndpoint(string, IClientVpnEndpointOptions)
Adds a new client VPN endpoint to this VPC.
public virtual ClientVpnEndpoint AddClientVpnEndpoint(string id, IClientVpnEndpointOptions options)
Parameters
- id string
- options IClientVpnEndpointOptions
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
AddFlowLog(string, IFlowLogOptions?)
Adds a new flow log to this VPC.
public virtual FlowLog AddFlowLog(string id, IFlowLogOptions? options = null)
Parameters
- id string
- options IFlowLogOptions
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
AddGatewayEndpoint(string, IGatewayVpcEndpointOptions)
Adds a new gateway endpoint to this VPC.
public virtual GatewayVpcEndpoint AddGatewayEndpoint(string id, IGatewayVpcEndpointOptions options)
Parameters
- id string
- options IGatewayVpcEndpointOptions
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
AddInterfaceEndpoint(string, IInterfaceVpcEndpointOptions)
Adds a new interface endpoint to this VPC.
public virtual InterfaceVpcEndpoint AddInterfaceEndpoint(string id, IInterfaceVpcEndpointOptions options)
Parameters
- id string
- options IInterfaceVpcEndpointOptions
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
AddVpnConnection(string, IVpnConnectionOptions)
Adds a new VPN connection to this VPC.
public virtual VpnConnection AddVpnConnection(string id, IVpnConnectionOptions options)
Parameters
- id string
- options IVpnConnectionOptions
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
EnableVpnGateway(IEnableVpnGatewayOptions)
Adds a VPN Gateway to this VPC.
public virtual void EnableVpnGateway(IEnableVpnGatewayOptions options)
Parameters
- options IEnableVpnGatewayOptions
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
FromLookup(Construct, string, IVpcLookupOptions)
Import an existing VPC by querying the AWS environment this stack is deployed to.
public static IVpc FromLookup(Construct scope, string id, IVpcLookupOptions options)
Parameters
- scope Construct
- id 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 Construct
- id 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[]
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused
SelectSubnets(ISubnetSelection?)
Returns IDs of selected subnets.
public virtual ISelectedSubnets SelectSubnets(ISubnetSelection? selection = null)
Parameters
- selection ISubnetSelection
Returns
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 {
IpAddresses = IpAddresses.Cidr("10.0.0.0/16")
});
// Iterate the private subnets
var selection = vpc.SelectSubnets(new SubnetSelection {
SubnetType = SubnetType.PRIVATE_WITH_EGRESS
});
for (var subnet in selection.Subnets)
{
}
Resource: AWS::EC2::VPC
ExampleMetadata: infused