Create VPC and Nat Gateway

Create VPC and NAT Gateway

  1. Open your CDK project and create a new stack named VpcStack.java.

    Step 1

  2. Inherit from Stack from the awscdk library and create a constructor.

    Step 2

  3. Define the VPC property and create a getter method.

    • Add the property in the VpcStack.java class:
      private final Vpc vpc;
      
    • Then, create a getter method to access this VPC for other resources:
      public Vpc getVpc() {
          return vpc;
      }
      

    Step 3

  4. Initialize the VPC object in the constructor of VpcStack.java with the following code:

    this.vpc = new Vpc(this, "Vpc", VpcProps.builder().build());
    

Architect

  1. Next, name the VPC as ECommerceVPC and specify the number of Availability Zones as 2 using the following code:

    .vpcName("ECommerceVPC")
    .maxAzs(2)
    

Architect

  1. If you want your VPC to be public and not use a NAT Gateway, you can add the following code:

    .natGateways(0)
    

You may choose not to use a NAT Gateway in a lab environment to save costs, but avoid doing this in a production environment.

Architect