0
0
AWScloud~5 mins

CIDR blocks and IP addressing in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create networks in the cloud, you need to decide which IP addresses devices will use. CIDR blocks help you define a range of IP addresses in a simple way. This lets your cloud resources talk to each other without conflicts.
When setting up a new virtual private cloud (VPC) in AWS to isolate your resources.
When you want to divide your network into smaller parts called subnets for better organization.
When you need to control which IP addresses can access your servers.
When connecting multiple networks and avoiding overlapping IP addresses.
When planning how many devices or servers you want to fit in your network.
Config File - vpc.yaml
vpc.yaml
Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: my-vpc
  MySubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: us-east-1a
      Tags:
        - Key: Name
          Value: my-subnet

This file creates a VPC with a CIDR block of 10.0.0.0/16, which means it can hold many IP addresses.

It also creates a subnet inside that VPC with a smaller CIDR block 10.0.1.0/24, which is a smaller range inside the VPC.

This setup helps organize your network and control IP address allocation.

Commands
This command creates a new VPC with the IP address range 10.0.0.0 to 10.0.255.255, allowing many devices inside.
Terminal
aws ec2 create-vpc --cidr-block 10.0.0.0/16
Expected OutputExpected
{ "Vpc": { "VpcId": "vpc-0abcd1234efgh5678", "State": "pending", "CidrBlock": "10.0.0.0/16", "IsDefault": false } }
--cidr-block - Defines the IP address range for the VPC using CIDR notation
This command creates a subnet inside the VPC with a smaller IP range, allowing better organization and control.
Terminal
aws ec2 create-subnet --vpc-id vpc-0abcd1234efgh5678 --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
Expected OutputExpected
{ "Subnet": { "SubnetId": "subnet-0123abcd4567efgh8", "VpcId": "vpc-0abcd1234efgh5678", "CidrBlock": "10.0.1.0/24", "AvailabilityZone": "us-east-1a", "State": "pending" } }
--vpc-id - Specifies which VPC the subnet belongs to
--cidr-block - Defines the IP address range for the subnet
--availability-zone - Sets the physical location for the subnet
This command checks the details of the VPC to confirm the CIDR block and status.
Terminal
aws ec2 describe-vpcs --vpc-ids vpc-0abcd1234efgh5678
Expected OutputExpected
{ "Vpcs": [ { "VpcId": "vpc-0abcd1234efgh5678", "State": "available", "CidrBlock": "10.0.0.0/16", "IsDefault": false } ] }
--vpc-ids - Filters the output to show only the specified VPC
This command checks the details of the subnet to confirm its CIDR block and availability zone.
Terminal
aws ec2 describe-subnets --subnet-ids subnet-0123abcd4567efgh8
Expected OutputExpected
{ "Subnets": [ { "SubnetId": "subnet-0123abcd4567efgh8", "VpcId": "vpc-0abcd1234efgh5678", "CidrBlock": "10.0.1.0/24", "AvailabilityZone": "us-east-1a", "State": "available" } ] }
--subnet-ids - Filters the output to show only the specified subnet
Key Concept

If you remember nothing else from this pattern, remember: CIDR blocks define the size and range of IP addresses your cloud network can use.

Common Mistakes
Using overlapping CIDR blocks for multiple VPCs or subnets
Overlapping IP ranges cause network conflicts and communication failures between resources.
Plan and assign unique, non-overlapping CIDR blocks for each VPC and subnet.
Choosing too small a CIDR block that cannot hold enough IP addresses
You will run out of IP addresses and cannot add more resources to the network.
Estimate the number of devices and choose a CIDR block large enough to fit them.
Not specifying the correct VPC ID when creating subnets
The subnet will not be created in the intended VPC, causing confusion and errors.
Always double-check and use the correct VPC ID when creating subnets.
Summary
Create a VPC with a CIDR block to define the IP address range for your cloud network.
Create subnets inside the VPC with smaller CIDR blocks to organize and separate resources.
Use AWS CLI commands to create and verify your VPC and subnets with their CIDR blocks.