# Setting Up Virtual Private Cloud

Hello folks.

Today, we'll be setting up our Virtual Private Cloud, a.k.a. VPC.

Let's get started right away.

---

### Overview

In this setup, we will create a VPC with an Internet Gateway for internet access. We'll configure route tables for traffic management. Inside the VPC, we'll have public subnets for instances directly accessible from the internet and private subnets for instances needing internet access but not directly reachable. To enable internet access for private instances, we'll deploy a NAT Gateway in the public subnet.

---

### Task 1: Creating a VPC

On Services, search for VPC and select **VPC**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716861722916/e75707d8-386e-4825-8d45-f04a2b880a22.png align="center")

A Virtual Private Cloud (VPC) in AWS is a customizable network environment that allows you to isolate and securely manage your AWS resources.

Click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716862095136/3adcae33-7f6e-4ed0-9b67-4ea7091fdf3c.png align="center")

Provide a suitable name for your VPC.

Input the given CIDR in the IPv4 CIDR block.

For the `10.0.0.0/16` network, the first two octets are reserved for the network portion, while the remaining two octets are for host addresses. This allows for 2^16 = 65,536 total IP addresses.

Click on `Create VPC`.

We've created our VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716862897458/1c02f114-098d-4964-b188-48f980a8b701.png align="center")

The other one is default, created by AWS.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716892751236/00deaad5-d42d-48fb-9e75-a196640aa416.png align="center")

---

### Task 2: Creating an Internet Gateway

An Internet Gateway (IGW) allows your VPC to connect to the internet, enabling resources within the VPC, to send and receive internet traffic.

On the VPC dashboard, select **Internet gateways**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716889809637/dd3694a8-ea01-4609-9611-f7714fdfe06f.png align="center")

Click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716864949431/aeb7f523-b0b0-49ec-a3f1-180c45b7255c.png align="center")

Provide a name for your internet gateway and `Create internet gateway`.

Now, it's time to attach the Internet Gateway (IG) to the VPC.

We can do this using either of the options.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716865049090/bd1964a6-fb1d-493b-8645-6d4d4e0fce52.png align="center")

Select your VPC and `Attach internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716865294193/c360321f-f492-4634-b03c-f268d254f554.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716892890637/d3fe941d-8600-4af2-a65f-4bc97010adbb.png align="center")

---

### Task 3: Creating subnets

A subnet is a segment of a VPC's IP address range where you can place resources such as EC2 instances. Subnets allow you to **group resources based on security and operational needs**, and can be either public (with internet access) or private (without direct internet access).

On VPC dashboard, on **Subnets**, click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716866572282/74b01a66-8e82-41de-9aa7-bceb1414940f.png align="center")

Select your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716866638308/ca334cc8-3569-421a-8d32-3ca87122d244.png align="center")

Provide suitable name for your subnets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716867057395/8e7634b8-7def-4ed8-87e5-7d44ec2bcc96.png align="center")

Our first subnet, `publicSubnet`, has a CIDR value of 10.0.1.0/24.

It will have a total of 2^8 = 256 addresses.

Also, for this project, we're only choosing a single availability zone.

Click on `Add new subnet` and create a private subnet.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716867507021/55eced24-2b31-479e-9db5-0832b8965451.png align="center")

Our second subnet, `privateSubnet`, has a CIDR value of 10.0.2.0/24.

It will have a total of 2^8 = 256 addresses.

Click on `Create subnet`.

We've successfully created 2 subnets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716867300365/3794c106-9733-40ba-be93-a1bcbe6290e8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716893460595/744b89a6-3c61-426e-be15-20fe08b5375d.png align="center")

---

### Task 4: Creating a NAT Gateway

A NAT Gateway allows instances in a private subnet to initiate outbound traffic to the internet, while preventing inbound traffic from reaching those instances directly. It provides a way for these instances to access the internet while remaining private.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716871983671/82199b81-6ff3-4f9a-99dc-b0479ea2f268.png align="center")

On VPC dashboard, search for **Nat gateways** and click on `Create NAT gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716872847702/2fadee99-d3f5-464f-9274-a0ede500335c.png align="center")

Provide a suitable name and select your private subnet.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716872793523/3c905ed6-5652-4681-9f5e-c19bdccc66b1.png align="center")

For public connectivity, instances in private subnets can connect to the internet through a public NAT gateway, but they cannot receive unsolicited inbound connections from the internet. You create a public NAT gateway in a public subnet and associate an elastic IP address with it.

On the other hand, the Private connectivity type allows instances in private subnets to connect to other VPCs or your on-premises network. Unlike the Public NAT gateway, a Private NAT gateway does not use an Elastic IP address.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716881924407/deac57d6-5143-4e04-b357-c31662130f1c.png align="center")

Click on `Allocate Elastic IP`.

An Elastic IP (EIP) is a static IPv4 address associated with your AWS account, not a specific resource. It's ideal for scenarios requiring a persistent public IP that can be easily reassigned to another instance during failures, updates, or scaling.

Click on `Create NAT gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716882849030/01fde8de-eccf-41ef-a8bb-3acf4c7d76d5.png align="center")

It takes some time to configure.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716882907986/6eb73bdf-1c79-40ff-9c60-15963ed3989a.png align="center")

Alright, let's move on.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716894949626/0ab5173b-2bf1-4ae1-99dd-746f96a35359.png align="center")

---

### Task 5: Creating route tables

Route tables direct network traffic within your VPC. They contain a set of rules (routes) that determine where network traffic from your subnet or gateway is directed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716866062121/f1dea02b-e0d1-46e2-b229-6da00d233e4f.png align="center")

On VPC dashboard, select **Route tables** and click on `Create route table`.

Since we have two subnets, one private and one public, for different purposes, we need to create two route tables.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716867758203/17c4a10b-b0a5-49a3-a94f-064143be5d6e.png align="center")

Provide name for your Route table and select your VPC.

Click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716868264251/885220f1-6221-484e-a707-41dfd8da7977.png align="center")

After creating route table, on **Subnet associations**, click on `Edit subnet associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716868835746/f4437a40-75d4-427b-9130-97c830230224.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716867958627/0649fce3-2023-4b77-9307-11d1f9fc7e1d.png align="center")

Select your Public Subnet and click on `Save associations`.

Now we need to add routes so that our public subnet can access the Internet.

Click on `Edit routes` and add the following configurations on `Add route`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716868757129/f54d1984-b967-440f-ac3e-db84dab0757e.png align="center")

So, we have added a rule to access the destination `0.0.0.0/0` using the Internet Gateway. In routing tables, a destination of 0.0.0.0/0 means all IP addresses, effectively representing the entire internet.

Click on `Save changes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883774342/83220b66-7b7d-44c8-8c96-e081b8476195.png align="center")

Now, for private subnets, let's create a route table.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883040564/87ac0e8b-4835-4c76-b9b9-7c49438eb9c2.png align="center")

Provide a suitable name and assign your VPC.

Click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883359455/2deb2345-403b-4872-a4a0-220603579c0b.png align="center")

Click on `Edit subnet associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883392821/22acf2df-221f-44ea-8c92-447fdc8db57e.png align="center")

Tick your Private Subnet and click on `Save associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883444488/eb2eec70-e083-4313-8c61-5f1a19b154c7.png align="center")

Now, time to add route. Click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883498988/cf8974db-e2a7-4412-a024-7bfeb43481e8.png align="center")

Select your NAT Gateway and click on `Save changes`.

This configuration allows instances in the private subnet to access the internet through the NAT Gateway.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716883588733/11613900-b88f-4e75-b43a-18310d4a21e0.png align="center")

Alright, we have now associated an Internet Gateway with the public subnet and a NAT Gateway with the private subnet.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716895050992/55755288-882d-413f-b19b-f2b223c4b1c8.png align="center")

---

### Task 6: Creating EC2 Instances

Now, it's time to create EC2.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716884051210/d90ec9ec-ab9c-4a77-87cf-3ef4240b3b5f.png align="center")

Search for **EC2**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716884132822/df24ec02-dc24-4acd-a991-7d8eca38c050.png align="center")

Click on `Launch instance`.

Let's create Public instance first.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716884974153/38c363d7-f01a-496c-82dc-cc1ec8e642b4.png align="center")

Provide a suitable name. I chose Ubuntu as AMI.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716885024760/4f65f98e-b941-447b-822c-8d37c8611a31.png align="center")

Select a free tier instance if available and create a new key pair or use your existing one.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716885180035/fb93ba59-077c-4862-a906-92713dab4d7f.png align="center")

Edit Network settings. Select your VPC, select the public subnet, and enable Auto-assign public IP.

Assigning a public IP allows an instance to communicate directly over the internet.

Leave all other configurations as they are and click `Launch instance`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716885452385/15a2e0c4-7a05-41f5-bbf4-65806663ebc8.png align="center")

Alright, let's go for Private instance now. Click on `Launch instances`.

Provide name, select an AMI, select the free-tire instance.

We can use our previously created key pair or create a new one.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716885713217/16d9df0f-edfc-432d-974e-5db5c928ad7a.png align="center")

Edit Network settings. Select your VPC, select Private subnet and leave the Auto-assign public IP as default, i.e., `Disable` mode.

A private instance does not require a public IP address, as it is not intended to communicate directly over the internet.

Leave all other configurations as they are and click `Launch instance`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886127195/df780adf-0f79-4b0c-a060-54127a7b1986.png align="center")

Alright, we've created two instances: one public and one private, each residing in different subnets, and that sums up our project.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716895420598/df2b25f7-8096-4426-a121-732fdba5ed76.png align="center")

---

### Conclusion

To sum up, we created a Virtual Private Cloud (VPC) with two subnets in one Availability Zone. The public subnet is for resources needing direct internet access, like public EC2 instances, connected to an Internet Gateway (IG). The private subnet, for more secure resources like private EC2 instances, is connected to a Network Address Translation (NAT) Gateway. We also configured route tables to manage traffic flow.

---

### Task 7: Clean up

Now, it's time to clean up the resources.

1. **Terminating EC2 instances.**
    
    Select your instances and, on **Instance state**, click Terminate instance.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886357755/746ada4d-c948-422d-9325-3e9445f4a9c3.png align="center")
    
    Click on `Terminate`.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886444879/29f5d99c-63cb-4172-a05b-d827bee57fd3.png align="center")
    
2. **Deleting route tables.**
    
    Head up to VPC Dashboard by searching **VPC** on the Search box.
    
    1. On Route tables, select your one route table and click on `Edit subnet associations`. We've to disassociate subnets first to delete the route table.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886848116/3f22d37f-9f5a-4bac-9c43-f71f62cefdb7.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886943673/a7557c26-a697-4bda-b819-74511149b128.png align="center")
        
        Uncheck the box and click on `Save associations`. And the same for another route table. Select route table, on **Subnet associations**, click on `Edit subnet associations`.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887058044/008e212c-4070-436b-9b33-b65d6000ff15.png align="center")
        
        Uncheck the box and click on `Save associations`.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716886738248/a6b8f20e-67a7-4e57-aa9c-42e8eb645f23.png align="center")
        
        Now, select both of your route tables and on Actions menu, click on `Delete route table`.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887259327/a6660d1f-9857-4bdc-81f6-97309aae69ac.png align="center")
        
        Type *delete* and click on `Delete`.
        
3. **Release Elastic IP.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887990159/44b195b1-230a-4487-b380-dc8fd3783dae.png align="center")
    
    On **Elastic IPs**, select your elastic IP and click on `Release Elastic IP addresses`.
    
4. **Deleting NAT Gateway.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887776802/d86b8be7-bea5-4996-b732-6f0770b4130f.png align="center")
    
    Select your NAT Gateway and on **Actions panel**, click on **Delete NAT gateway**.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887853850/bde5d7fd-0c20-48e0-9390-637da84bbfed.png align="center")
    
5. **Detaching Internet Gateway.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887659752/942a62d0-114f-479c-a767-c569677cd182.png align="center")
    
    Select your Internet Gateway and, on **Actions panel**, click `Detach from VPC`.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716888258312/c101f9c4-0773-4897-8285-836211047622.png align="center")
    
    After detaching, we have to delete the internet gateway.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716888321913/00d92bf9-6d96-407b-adcd-631b649c5a4e.png align="center")
    
6. **Deleting subnets.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716887362727/52bc5dfb-9ad5-40e8-9265-8634ff588f38.png align="center")
    
    Select both of your subnets and on Actions menu, click on `Delete subnet`.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716888421839/6f2b0c29-42c1-4c46-8465-b3e1c7d4c3f4.png align="center")
    
7. **Deleting VPC.**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716888483887/aa3d56aa-da4d-4387-a2f6-7fc8529f1cb6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716888513901/469f0eb8-b653-42aa-bd8b-fe31389021cc.png align="center")

Alright, now, we've cleaned up our used resources.

---

See you in the next one!
