DEV Community

Cover image for Step 1: Create a VPC and subnets
Sebastian Torres
Sebastian Torres

Posted on

Step 1: Create a VPC and subnets

Create an IPv4-enabled VPC and subnets using the AWS CLI

The following example uses AWS CLI commands to create a nondefault VPC with an IPv4 CIDR block, and two publics subnets in the VPC, After you've created the VPC and subnets, you can launch two instances in the public subnets and connect to it. To begin, you must first install and configure the AWS CLI. For more information, see Installing the AWS CLI

You will create the following AWS resources:

  • A VPC
  • Two subnets
  • An Internet gateway
  • A route table
  • Two EC2 instances
  • An application Load Balancer

Tasks

  • Step 1: Create a VPC and subnets
  • Step 2: Make your subnets public
  • Step 3: Launch the instances into your subnet
  • Step 4: Create your load balancer

Step 1: Create a VPC and subnets

The first is to create a VPC and two subnets. This example uses CIDR block 10.0.0.0/16 for the VPC, but you can choose a different CIDR block. For more information, see VPC sizing.

To create a VPC and subnets using the AWS CLI

  • Create a VPC with a 10.0.0.0/16 CIDR block using the following create-vpc command.
$ aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text
Enter fullscreen mode Exit fullscreen mode

The command returns the ID of the new VPC. The following is an example.

vpc-2f09a348
Enter fullscreen mode Exit fullscreen mode
  • Using the VPC ID from the previous step, create a subnet with a 10.0.1.0/24 CIDR block using the following create-subnet command. Also, specify the Availability Zone of each of the subnets to make sure they are not the same. This is important for the process of creating the application load balancer later on.
$ aws ec2 create-subnet --availability-zone us-east-1a --vpc-id vpc-2f09a348 --cidr-block 10.0.1.0/24
Enter fullscreen mode Exit fullscreen mode
  • Create a second subnet in your VPC with a 10.0.0.0/24 CIDR block.
$ aws ec2 create-subnet --availability-zone use-east-1b --vpc-id vpc-2f09a348 --cidr-block 10.0.0.0/24
Enter fullscreen mode Exit fullscreen mode

Top comments (0)