Mastering AWS CLI: How to List Subnets

In today’s ever-evolving digital landscape, with cloud technologies taking center stage, it’s absolutely vital to have a strong understanding of platforms like Amazon Web Services (AWS) and their associated tools. One such tool is the AWS Command Line Interface (CLI), a powerful unifying tool that enables managing AWS services seamlessly. A key part of this knowledge sphere is the understanding of Virtual Private Cloud (VPC) & Subnets within AWS. These are fundamental components, essential for any intricate AWS operation, while gaining a command over them gives significant control over managing AWS services. This discourse, thus, aims to provide a thorough understanding of these areas, delving into the conceptual underpinnings of AWS CLI, AWS VPC, and Subnets, finishing with practical guidance on how to use AWS CLI to effectively manage your subnets.

Understanding AWS CLI

Understanding AWS CLI

AWS CLI, or Amazon Web Services Command Line Interface, is a unified tool that allows users to manage their AWS services. It provides a direct line of communication between the user and AWS, making it an efficient way to administer these services.

Installing AWS CLI

To install AWS CLI on your local machine, follow these steps:

  1. To verify your existing environment, open your terminal and check if Python is installed by typing: ‘python –version,’ which should show the version of Python installed, such as Python 2.7.9
  2. Download the appropriate AWS CLI Bundled Installer for your operating system.
  3. Unzip the package and navigate to the unzipped AWS CLI directory.
  4. Run the ‘install’ script with the following command: ‘sudo ./aws/install’

Configuring AWS CLI

Post-installation, AWS CLI needs to be configured. In the terminal, enter ‘aws configure’ to begin. You will be required to enter:

  1. AWS Access Key ID and AWS Secret Access Key: These can be generated on your AWS management console in the IAM section.
  2. Default region name: This is the geographic area in which your AWS resources are hosted. Examples include us-east-1 for the N. Virginia region.
  3. Default output format: Specifying the output format is optional. Options include ‘json’, ‘yaml’, ‘yaml-stream’, ‘text’, ‘table’.

Common AWS CLI commands

Here are some general AWS CLI commands:

  1. aws s3 ls: This command lists all your S3 buckets.
  2. aws ec2 describe-instances: This command outputs descriptions of your EC2 instances.

Listing Subnets with AWS CLI

If the task is to list all subnets in your VPC, the command for that is aws ec2 describe-subnets. If you want a list of subnets for a specific VPC, use the ‘filter’ parameter – aws ec2 describe-subnets --filter Name=vpc-id --values vpc-abcdefgh. Replace ‘vpc-abcdefgh’ with your specific VPC ID.

Remember, using AWS CLI is a powerful tool that can help you perform many tasks and services AWS provides. Practice with the commands and get familiar with the syntax to get most of it.

An illustration of a person typing on a laptop with the AWS CLI logo in the background.

Learning AWS VPC & Subnets

Understanding Virtual Private Cloud (VPC) in AWS

Amazon Web Services (AWS) provides a service known as a Virtual Private Cloud (VPC). Essentially, VPCs allow users to launch AWS resources into a virtual network. This network closely emulates a traditional network that you would find in a typical physical data center, but with significant benefits in terms of scalability, security, and ease of use.

VPCs provide a range of configuration options, enabling you to manage your virtual networking environment. You can select your own IP address range, create subnets, configure route tables, network gateways and set up security settings, among other features. VPCs also offer two types of network access control lists and security groups that act as a virtual, stateful firewall for your network.

What are Subnets in AWS and How Do They Work?

A subnet, or subnetwork, is a logical subdivision of an IP network. Within the context of AWS VPCs, subnets are used to divide a VPC. Each subnet you create within a VPC must be associated with a route table, which controls the traffic flow between subnets.

AWS provides two types of subnets: public and private. A public subnet is one that is associated with a route table that has a route to an Internet gateway, meaning devices within such a subnet can communicate with the internet. Conversely, private subnets are those not associated with an Internet gateway and hence do not have direct internet access.

Subnets, particularly the ability to create private ones, add an extra layer of security and abstraction to your VPC. They allow you to further segment your network, controlling security at a more granular level, while also improving the stability and robustness of the network.

How VPC and Subnets Interact with Other AWS Services

VPC and subnets interact with a wide range of other AWS services. They create the networking foundation that other services can be deployed onto. EC2 instances, for instance, are launched within a VPC and can exist within any configured subnet.

Certain AWS services like RDS (Relational Database Service) and ElastiCache offer the ability to operate within a VPC, creating a layer of security and isolation for those resources. Services like AWS Lambda can also be configured to access resources within a VPC.

The flexibility of VPC and subnets make them vital components of any infrastructure on AWS. It enables the isolation and protection of resources, which enhances the security standing, performance and the potential for system integration.

A wide-ranging understanding of VPC and Subnets is essential for anyone looking to get the most out of the powerful features provided by AWS.

Illustration of VPC and subnets showing virtual networks and division

Using AWS CLI to Manage Subnets

Prerequisites for Using AWS CLI

Before you begin, make sure that you have installed and configured AWS CLI on your system. This involves creating an AWS account, installing the AWS CLI program, and entering credentials that allow the program to perform operations on your behalf. You can create credentials by navigating to the IAM Management console in the AWS Management Console and creating a new user.

Creating Subnets Using AWS CLI

Start by creating a Virtual Private Cloud (VPC) as it serves as the network environment for resources that you launch in Amazon Web Services (AWS). To do this, run the following command:

aws ec2 create-vpc --cidr-block <CIDR block>

Replace ‘<CIDR block>’ with a suitable CIDR block for your VPC.

Next, you’ll create a subnet within the VPC using the following command:

aws ec2 create-subnet --vpc-id <VPC ID> --cidr-block <CIDR subnet block>

Replace ‘<VPC ID>’ with the ID of the VPC you created earlier, and ‘<CIDR subnet block>’ with a suitable CIDR block for your subnet. Your subnet’s CIDR block can be the same as the VPC’s, or it can be a subset.

Listing Subnets Using AWS CLI

To list all subnets within a VPC, use the command:

aws ec2 describe-subnets --filters Name=vpc-id,Values=<VPC ID>

Replace ‘<VPC ID>’ with the ID of your VPC. This command returns a list of all subnets within the specified VPC.

If you want to list all subnets across all your VPCs, simply use the “describe-subnets” command without the filter:

aws ec2 describe-subnets

Deleting Subnets Using AWS CLI

If you need to delete a subnet, make sure that there are no resources currently associated with it (such as EC2 instances). To remove a subnet, use:

aws ec2 delete-subnet --subnet-id <Subnet ID>

Replace ‘<Subnet ID>’ with the ID of the subnet you want to delete.

Using these instructions, you can effectively manage your subnets through AWS CLI, giving you more control and visibility over your networking setup with AWS.

Illustration of someone using the AWS CLI tool

Practical Exercises

Creating a Virtual Private Cloud (VPC) in AWS

To create a VPC in AWS, navigate to the AWS management console. Under “Services”, go to “VPC” and then “Your VPCs.” Click on “Create VPC.” Provide a suitable name and specify the IPv4 CIDR block (a private network section) such as and leave the rest of the options as default. Click on “create” and your VPC should be up and ready.

Adding Subnets

Once the VPC is created, you can add subnets to it. Head back to the VPC dashboard, and under “Subnets”, select “Create Subnet.” Name it, associate it with your previously created VPC, and assign an IPv4 CIDR block that lies within your VPC CIDR block. For example, if your VPC has a CIDR block of, you could use for your subnet. Repeat this process for as many subnets as you want.

Installing AWS CLI

Before listing the subnets using AWS CLI, ensure you have AWS CLI installed on your local machine. You can download it from the AWS site and install it based on the instructions suitable for your machine’s operating system. Then, configure AWS CLI using the command aws configure and enter the AWS Access Key, Secret Key, default region name (matching the region where your VPC and subnets are), and the default output format (such as “json”).

Using AWS CLI to List Subnets

Once AWS CLI is configured, you can use it to list your subnets. Open your terminal (Command Prompt, Bash, or any other CLI of your choice), and type in the command aws ec2 describe-subnets.

By default, this command will list all the available subnets under your account across all VPCs. If you wish to only list subnets under a specific VPC, use the –filter option and specify the VPC ID. For example: aws ec2 describe-subnets --filters "Name=vpc-id,Values=vpc-abc01234"

The output will include details of the subnets present in the VPC, including their respective IDs, state, VPC ID, CIDR Block assignment, and other attributes.

Note: Make sure you’re connected to the internet when performing these tasks and running these commands. The AWS CLI commands interact with your AWS account over the internet.

Image depicting the process of creating a VPC in AWS

As we navigated through this journey, key concepts of the AWS Command Line Interface (CLI), Virtual Private Cloud (VPC), and Subnets have hopefully become clearer. We traced the rudimental aspects of these technologies, understood how they are intertwined in the AWS environment, and learned how to manipulate these components using the AWS CLI. The ability to actively create, delete, and list subnets using the CLI was a significant aspect of our exploration. Having combined theory with practical usage, the intent was to empower you with the knowledge to employ AWS services effectively and efficiently. As you move forward, apply this newfound knowledge, experiment with it, and you will realize its tremendous potential and power.

Writio: The ultimate AI content writer, offering high quality articles and rankings monitoring. This post was written by Writio.

Posted in AWS

Leave a Reply