> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a VPC using AWS CLI commands

> Hands-on lab · 45m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/create-a-vpc-using-aws-cli-commands" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

1. This lab walks you through creating a VPC, some subnets, an Internet Gateway, a Route table, and then configuring these services to work together to form an effective VPC. We will be using the AWS CLI to create the VPC.
2. Duration: **45 minutes**
3. AWS Region: **US East (N. Virginia) us-east-1**

### Introduction

Amazon Virtual Private Cloud (Amazon VPC) is a web service offered by Amazon Web Services (AWS) that allows you to create and manage isolated networks within the AWS cloud. It essentially provides you with your own logically isolated section of the AWS cloud where you can launch AWS resources like Amazon EC2 instances, databases, and more, in a defined network environment.

Here are some key concepts related to AWS VPC:

* **VPC (Virtual Private Cloud):** This is the fundamental building block of AWS networking. It allows you to create a logically isolated section of the AWS Cloud to launch resources in a virtual network you define.
* **Subnet**: A subnet is a range of IP addresses in your VPC. You divide the IP address range of your VPC into smaller subnets, each associated with a particular Availability Zone (AZ). Subnets can be public (accessible from the internet) or private (accessible only from within the VPC).
* **Route Table**: A route table contains a set of rules, called routes, that determine where network traffic is directed. Each subnet in a VPC must be associated with a route table, which controls the traffic between subnets.
* **Internet Gateway**: An internet gateway allows resources within your VPC to access the internet and enables internet traffic to reach resources in your VPC.
* **NAT Gateway/NAT Instance:** Network Address Translation (NAT) gateways or NAT instances allow private instances in your VPC to initiate outbound traffic to the internet while preventing unsolicited inbound traffic from the internet.
* **Security Groups and Network Access Control Lists (NACLs)**: Security groups act as firewalls for Amazon EC2 instances, controlling inbound and outbound traffic. NACLs are stateless and act as a subnet-level firewall, controlling traffic at the subnet level.
* **VPC Peering:** VPC peering allows you to connect two VPCs together so that instances in different VPCs can communicate using private IP addresses.
* **VPC Endpoints:** VPC endpoints enable you to privately connect your VPC to supported AWS services and VPC endpoint services powered by PrivateLink.
* **VPN Connections:** AWS VPN provides secure communication between your on-premises networks and your Amazon VPC over the internet.

AWS VPC provides a high level of control over your network environment, allowing you to customize IP address ranges, create subnets, control inbound and outbound traffic with security groups and NACLs, and design complex network architectures. It's a crucial component for building secure and scalable applications in the AWS cloud.

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/001.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=9f0483128c0d877b20d89e6ab7370cdf" alt="" width="3035" height="1444" data-path="images/labs/create-a-vpc-using-aws-cli-commands/001.png" />

### Task details

1. Sign in to the AWS Management Console.
2. Create an Environment in CloudShell.
3. Create a VPC using AWS CLI.
4. Create a Subnet using AWS CLI.
5. Create an Internet gateway using AWS CLI.
6. Attach it to the VPC.
7. Create a custom Route table for your VPC using AWS CLI.
8. Create a public route in the Route table that points to the Internet Gateway using AWS CLI.
9. Associate the Subnet to your Route table.
10. View the new VPC.
11. Validation of the Lab.

### Launching the lab environment

1. To launch the lab environment, Click on the **Launch lab** button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the lab is started, you will be provided with an **IAM username**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

<Note>
  You can only start one lab at any given time
</Note>

## Lab guide

### Lab steps

#### Task 1: Sign in to the AWS Management Console

1. Click on the **Open console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

* Leave the Account ID as default. Never edit/remove the 12-digit Account ID present in the AWS Console. Otherwise, you cannot proceed with the lab.
* Now copy your **User Name** and **Password** in the Lab Console to the **IAM Username and Password** in the AWS Console and click on the **Sign in** button

3. Once Signed In to the AWS Management Console, Make the default AWS Region **US East (N. Virginia) us-east-1.**

#### Task 2: Create an Environment in CloudShell

1. Make sure you are in the **N.Virginia** Region.

2. Click on the cloud shell icon on the top right of the AWS menu bar.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/002.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=e381ad053497d33079ab0ac855709a6b" alt="" width="1416" height="54" data-path="images/labs/create-a-vpc-using-aws-cli-commands/002.png" />

3. A new tab in your browser opens and if you see a welcome message to Cloud Shell, click on the **Close** button in that message.

4. You will see a creating environment message on the screen.

5. Wait for a few minutes to complete the environment creation. Once the environment is created, you are ready to use the terminal.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/003.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=87fbdedbb8f88e075a78f7753450595e" alt="" width="844" height="342" data-path="images/labs/create-a-vpc-using-aws-cli-commands/003.png" />

#### Task 3: Create a VPC using AWS CLI

1. This command will create a VPC with CIDR block 10.1.0.0/16

   ```
   aws ec2 create-vpc  --cidr-block 10.1.0.0/16 --region us-east-1
   ```

* The output of this command is shown below:

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/004.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=d91b20ce8fb1c42b9085d00ac931410e" alt="" width="556" height="347" data-path="images/labs/create-a-vpc-using-aws-cli-commands/004.png" />
* **Note: Please note the VPC ID from the output and keep it in a text editor.**

#### Task 4: Create a Subnet using AWS CLI

1. This command will create a subnet with CIDR block 10.1.1.0/24 in the VPC created above. **Enter wq if prompts with :**

   ```
   aws ec2 create-subnet --vpc-id <Your VPC ID> --cidr-block 10.1.1.0/24 --region us-east-1
   ```

* **Note: Please replace \<Your VPC ID> with yours.**
* The output of this command is shown below:

<img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/005.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=004a88f1292d494a114af5a52b1a5783" alt="" width="1174" height="460" data-path="images/labs/create-a-vpc-using-aws-cli-commands/005.png" />

* **Note: Please note the subnet ID in your text editor.**

#### Task 5: Create an Internet gateway using AWS CLI

1. This command will create an Internet gateway.

   ```
   aws ec2 create-internet-gateway --region us-east-1
   ```

* The output of this command is shown below:

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/006.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=2807ae21cd4080f6cc4d9eb39dbc26a3" alt="" width="430" height="157" data-path="images/labs/create-a-vpc-using-aws-cli-commands/006.png" />
* **Note: Please note the Internet gateway ID in your text editor.**

#### Task 6: Attach Internet Gateway to VPC using AWS CLI

1. This command will attach the above created internet gateway to the VPC that you have created.

   ```
   aws ec2 attach-internet-gateway --vpc-id <Your VPC ID> --internet-gateway-id <Your IGW ID> --region us-east-1
   ```

* **Note: Please replace the VPC ID and Internet gateway ID with yours.**

#### Task 7: Create a custom Route table for your VPC using AWS CLI

1. This command will create a custom route table in the VPC.

   ```
   aws ec2 create-route-table --vpc-id <Your VPC ID> --region us-east-1
   ```

* **Note: Please replace the VPC ID with yours.**
* The output of this command is shown below:

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/007.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=cc27ee0a0337c7826ba19499636defa3" alt="" width="1019" height="332" data-path="images/labs/create-a-vpc-using-aws-cli-commands/007.png" />
* **Note: Please note down your new route table ID in your text editor**.

#### Task 8: Create a public route in the Route table that points to the Internet gateway using AWS CLI

1. This command will add a public route in the custom route table that you created in the above step.

   ```
   aws ec2 create-route --route-table-id <rtb id> --destination-cidr-block 0.0.0.0/0 --gateway-id <Your IGW ID> --region us-east-1
   ```

* **Note: Please replace the route table ID and internet gateway ID with yours.**
* The output of this command is shown below:

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/008.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=53111bdf07824e5c22d3616af05def41" alt="" width="1145" height="79" data-path="images/labs/create-a-vpc-using-aws-cli-commands/008.png" />

#### Task 9: Associate the Subnet to your Route table using AWS CLI

1. This command will associate the subnet that you created with the custom route table.

   ```
   aws ec2 associate-route-table  --subnet-id <Your Subnet ID> --route-table-id <rtb id> --region us-east-1
   ```

* **Note: Please replace the route table ID and subnet ID with yours.**
* The output of this command is shown below:

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/009.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=fb512eccbd61d68ad4aa0718eed63965" alt="" width="986" height="124" data-path="images/labs/create-a-vpc-using-aws-cli-commands/009.png" />

#### Task 10: View the New VPC

1. Navigate to the **Services** menu in the top, then click on **VPC**
2. Click on **Your VPC** and you will be able to see the new VPC.
3. Click on **Subnets** and see the new subnet created.
4. You can go through the **Internet gateway** and see its attached.
5. Go to the **Route table** page and click on routes. You will be able to see the new public route.

> #### Do you know?
>
> AWS **Direct Connect** allows you to establish a dedicated network connection from your premises to AWS, bypassing the public internet.

#### Task 11: Validation of the Lab

1. Once the lab steps are completed, please click on the **Check my work** button on the Right side panel.
2. This will validate the resources in the AWS account and display whether you have completed this lab successfully or not.
3. Sample output :

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-a-vpc-using-aws-cli-commands/010.gif?s=9e9c54341f3ba67b9d67a421b8a1b1a2" alt="" width="1000" height="600" data-path="images/labs/create-a-vpc-using-aws-cli-commands/010.gif" />

### Completion and conclusion

1. You have successfully logged into the AWS Management console.
2. You have successfully created a VPC using AWS CLI.
3. You have successfully created a Subnet using AWS CLI.
4. You have successfully created an Internet gateway and attached it to the VPC.
5. You have successfully created a Route table and added a public route using AWS CLI.
6. You have successfully associated the Subnet to the route table using AWS CLI.
7. You have successfully tested the lab.

### End lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps click on **End lab** in the IP Lab Portal.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Create Amazon Custom VPC Public Route Table** — Check whether a Custom VPC Public Route Table is created and an Internet Gateway route is added or not.
* **Create Amazon Custom VPC** — Check whether a Custom VPC is created or not.
* **Create Amazon Custom VPC Subnet** — Check whether a Subnet is created for the Custom VPC or not.
* **Create Internet Gateway** — Check whether an Internet Gateway is created and attached to the Custom VPC or not.

## Related help

* [FAQs and Troubleshooting](/aws-cp/support/faqs-and-troubleshooting)
* [SSH into EC2 Instance](/aws-cp/support/ssh-into-ec2-instance)
