Create VPC

First you are going to create new VPC for all resources that will created further in the labs.

Add Amazon.CDK.AWS.EC2 Nuget package to the project:

EC2 Nuget

Add following import statement to the NorthwindCdkStack.cs.

using Amazon.CDK.AWS.EC2;

Replace the generated placeholder code with the following:

namespace NorthwindCdk
{
    public class NorthwindCdkStack : Stack
    {
        internal NorthwindCdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var vpc = new Vpc(this, "LabVpc", new VpcProps
            {
                MaxAzs = 2
            });
        }
    }
}

Vpc construct creates a VPC that spans a whole region and has two availability zones.

MaxAzs = 2

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).

Save changes and compile the project.

Run cdk diff to take a look at the changes that are going to be deployed.

cdk diff

The CDK CLI requires you to be in the same directory as your cdk.json file.

The output should look like this:

Stack NorthwindCdkStack
Conditions
[+] Condition CDKMetadataAvailable: {"Fn::Or":[{"Fn::Or":[{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-northeast-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-northeast-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-south-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-southeast-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ap-southeast-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"ca-central-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"cn-north-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"cn-northwest-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-central-1"]}]},{"Fn::Or":[{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-north-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"eu-west-3"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"me-south-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"sa-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-east-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-east-2"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-west-1"]},{"Fn::Equals":[{"Ref":"AWS::Region"},"us-west-2"]}]}]}

Resources
[+] AWS::EC2::VPC LabVpc LabVpc17F821B7
[+] AWS::EC2::Subnet LabVpc/PublicSubnet1/Subnet LabVpcPublicSubnet1SubnetBE5AC483
[+] AWS::EC2::RouteTable LabVpc/PublicSubnet1/RouteTable LabVpcPublicSubnet1RouteTable5D022469
[+] AWS::EC2::SubnetRouteTableAssociation LabVpc/PublicSubnet1/RouteTableAssociation LabVpcPublicSubnet1RouteTableAssociationFE601F86
[+] AWS::EC2::Route LabVpc/PublicSubnet1/DefaultRoute LabVpcPublicSubnet1DefaultRouteD07C003C
[+] AWS::EC2::EIP LabVpc/PublicSubnet1/EIP LabVpcPublicSubnet1EIP720BC466
[+] AWS::EC2::NatGateway LabVpc/PublicSubnet1/NATGateway LabVpcPublicSubnet1NATGateway629C5A67
[+] AWS::EC2::Subnet LabVpc/PublicSubnet2/Subnet LabVpcPublicSubnet2Subnet17E729C0
[+] AWS::EC2::RouteTable LabVpc/PublicSubnet2/RouteTable LabVpcPublicSubnet2RouteTableA796C712
[+] AWS::EC2::SubnetRouteTableAssociation LabVpc/PublicSubnet2/RouteTableAssociation LabVpcPublicSubnet2RouteTableAssociationD6683741
[+] AWS::EC2::Route LabVpc/PublicSubnet2/DefaultRoute LabVpcPublicSubnet2DefaultRouteC6E99F9A
[+] AWS::EC2::EIP LabVpc/PublicSubnet2/EIP LabVpcPublicSubnet2EIP492ACDE2
[+] AWS::EC2::NatGateway LabVpc/PublicSubnet2/NATGateway LabVpcPublicSubnet2NATGateway5542A2D8
[+] AWS::EC2::Subnet LabVpc/PrivateSubnet1/Subnet LabVpcPrivateSubnet1SubnetD99F912F
[+] AWS::EC2::RouteTable LabVpc/PrivateSubnet1/RouteTable LabVpcPrivateSubnet1RouteTable69E4F745
[+] AWS::EC2::SubnetRouteTableAssociation LabVpc/PrivateSubnet1/RouteTableAssociation LabVpcPrivateSubnet1RouteTableAssociationA08CAA0A
[+] AWS::EC2::Route LabVpc/PrivateSubnet1/DefaultRoute LabVpcPrivateSubnet1DefaultRouteF9242CE2
[+] AWS::EC2::Subnet LabVpc/PrivateSubnet2/Subnet LabVpcPrivateSubnet2Subnet6DFF0C14
[+] AWS::EC2::RouteTable LabVpc/PrivateSubnet2/RouteTable LabVpcPrivateSubnet2RouteTable3AFF2454
[+] AWS::EC2::SubnetRouteTableAssociation LabVpc/PrivateSubnet2/RouteTableAssociation LabVpcPrivateSubnet2RouteTableAssociationAF61693D
[+] AWS::EC2::Route LabVpc/PrivateSubnet2/DefaultRoute LabVpcPrivateSubnet2DefaultRouteE9E90533
[+] AWS::EC2::InternetGateway LabVpc/IGW LabVpcIGW82336A21
[+] AWS::EC2::VPCGatewayAttachment LabVpc/VPCGW LabVpcVPCGW932EA6D0

As you can see, few lines of C# code are going to produce 24 resources for the VPC with two availability zones, such as Subnets, Route Tables, NAT Gateways, Elastic IPs and other.

Now it’s time to deploy the project using cdk deploy command.

cdk deploy

It will take few minutes to create VPC and other resources.

Once deployment is complete, you will se the following output:

NorthwindCdkStack: deploying...
NorthwindCdkStack: creating CloudFormation changeset...
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::InternetGateway             | LabVpc/IGW (LabVpcIGW82336A21)
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::EIP                         | LabVpc/PublicSubnet1/EIP (LabVpcPublicSubnet1EIP720BC466)
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::VPC                         | LabVpc (LabVpc17F821B7)
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata                    | CDKMetadata
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::EIP                         | LabVpc/PublicSubnet2/EIP (LabVpcPublicSubnet2EIP492ACDE2)
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::InternetGateway             | LabVpc/IGW (LabVpcIGW82336A21) Resource creation Initiated
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::VPC                         | LabVpc (LabVpc17F821B7) Resource creation Initiated
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::EIP                         | LabVpc/PublicSubnet2/EIP (LabVpcPublicSubnet2EIP492ACDE2) Resource creation Initiated
 0/25 | 6:11:53 AM | CREATE_IN_PROGRESS   | AWS::EC2::EIP                         | LabVpc/PublicSubnet1/EIP (LabVpcPublicSubnet1EIP720BC466) Resource creation Initiated
 0/25 | 6:11:54 AM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata                    | CDKMetadata Resource creation Initiated
 1/25 | 6:11:55 AM | CREATE_COMPLETE      | AWS::CDK::Metadata                    | CDKMetadata
 2/25 | 6:12:09 AM | CREATE_COMPLETE      | AWS::EC2::InternetGateway             | LabVpc/IGW (LabVpcIGW82336A21)
 ..........
 21/25 | 6:14:05 AM | CREATE_IN_PROGRESS   | AWS::EC2::Route                       | LabVpc/PrivateSubnet1/DefaultRoute (LabVpcPrivateSubnet1DefaultRouteF9242CE2)
 21/25 | 6:14:06 AM | CREATE_IN_PROGRESS   | AWS::EC2::Route                       | LabVpc/PrivateSubnet1/DefaultRoute (LabVpcPrivateSubnet1DefaultRouteF9242CE2) Resource creation Initiated
 22/25 | 6:14:21 AM | CREATE_COMPLETE      | AWS::EC2::Route                       | LabVpc/PrivateSubnet1/DefaultRoute (LabVpcPrivateSubnet1DefaultRouteF9242CE2)
 23/25 | 6:14:49 AM | CREATE_COMPLETE      | AWS::EC2::NatGateway                  | LabVpc/PublicSubnet2/NATGateway (LabVpcPublicSubnet2NATGateway5542A2D8)
 23/25 | 6:14:51 AM | CREATE_IN_PROGRESS   | AWS::EC2::Route                       | LabVpc/PrivateSubnet2/DefaultRoute (LabVpcPrivateSubnet2DefaultRouteE9E90533)
 23/25 | 6:14:52 AM | CREATE_IN_PROGRESS   | AWS::EC2::Route                       | LabVpc/PrivateSubnet2/DefaultRoute (LabVpcPrivateSubnet2DefaultRouteE9E90533) Resource creation Initiated
 24/25 | 6:15:07 AM | CREATE_COMPLETE      | AWS::EC2::Route                       | LabVpc/PrivateSubnet2/DefaultRoute (LabVpcPrivateSubnet2DefaultRouteE9E90533)

 ✅  NorthwindCdkStack

Stack ARN:
arn:aws:cloudformation:eu-west-1:404486542784:stack/NorthwindCdkStack/f78fb1c0-8ed1-11ea-8a83-0a0af0d573f8

Now your VPC is ready and you can create resources in it.