AWS Networking from scratch
Tags: aws, networking
Published: 2021-10-12 03:58:41

In this post, we are going to see the AWS networking foundations, networks, subnetworks, firewall, Internet Gateway, Access Control List, NAT, and other services. 

Building networks, segregating the network, configuring routing, and network permissions are architectural decisions important for communications between the systems. Compute Services (EC2, ECS, EKS), Database (RDS, Aurora, ElastiCache), and others allow us to choose the network, subnetwork, availability zone, and firewall for deployment and execution of the AWS service. It is our responsibility to set up the communications needed for the systems and protect them against security attacks that impact the availability and integrity

We start from inwards to towards with the services that are base to other services. Each service has a description, scope, limits, and an example to know their capabilities and boundaries. Let's start.
&nbsp;  


## Virtual Private Cloud (VPC)

![VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y4rqkktrpzbgrv173xu.png)

**Type:** Networking.
&nbsp;

**Scope:** Create a virtual private network with an IP addresses block (CIDR). It allows logical isolate the resources deployed in the virtual network from other services that are in the AWS cloud.
&nbsp;

**Limits:** 5 VPC per AWS region. You can adjust the limit by requesting it from AWS support.
&nbsp;

**Example:** To create a VPC you need an IPv4 or IPv6 CIDR block, this value depends on the number of subnetworks and hosts you need. We use CIDR block **192.168.0.0/24** to create VPC on the Ohio AWS region. When you create a VPC by default create Route Table and Network Access Control List (NACL).
&nbsp;


## Subnets

![Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3zbnrhg9tjpvfrdzo4f.png)

**Type:** Networking.
&nbsp;

**Scope:** Create virtual subnetwork inside VPC network to split the network and deploy systems in the subnetwork. You can choose the availability zone (AZ) of the AWS region to create the subnetwork and you can create more than one subnetwork in the same AZ.
&nbsp;

**Limits:** 200 subnets per VPC. You can adjust the limit by requesting it from AWS support.
&nbsp;

**Example:** To create a VPC you need an IPv4 CIDR block that is inside the IPv4 CIDR block of VPC. 

Taken 3 bits of VPC network address then we can create 8 subnets (2^3=8). The IPv4 CIDR block for each subnet are:

* 192.168.0.0/27
* 192.168.0.32/27
* 192.168.0.64/27
* 192.168.0.96/27
* 192.168.0.128/27
* 192.168.0.160/27
* 192.168.0.192/27

The other 5 bits are to create hosts, the number of hosts per subnet is 27 (2^5 = 32 - 5). AWS reserves 5 IP addresses in each subnet, the first 4 addresses and the last one.

For the **192.168.0.0/27** CIDR block the IP addresses reserved by AWS are:

* 192.168.0.0, Subnetwork address.
* 192.168.0.1, reserved by AWS for VPC Router.
* 192.168.0.2, reserved by AWS for Amazon-provided DNS.
* 192.168.0.3, reserved by AWS for future use.
* 192.168.0.31, Broadcast, AWS does not support Broadcast communication inside VPC.

For the **192.168.0.32/27** CIDR block the IP addresses reserved by AWS are:

* 192.168.0.32, Subnetwork address.
* 192.168.0.33, reserved by AWS for VPC Router.
* 192.168.0.34, reserved by AWS for Amazon-provided DNS.
* 192.168.0.35, reserved by AWS for future use.
* 192.168.0.63, Broadcast, AWS does not support Broadcast communication inside VPC.

We are going to use the **192.168.0.0/27** CIDR block for **Public Subnet** (Internet access) and **192.168.0.32/27** CIDR block for **Private Subnet** (Internal access) on availability zone us-east-2a.
&nbsp;


## Internet Gateway

![Internet Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3lpj5apsubig7c56tfz.png)

**Type:** Networking.
&nbsp;

**Scope:** Internet access gateway for the resources deployed in subnet and VPC. Also, it is a NAT for instances (EC2, RDS) that has a public IP address.
&nbsp;

**Limits:** 5 Internet Gateway per AWS Region. You can adjust the limit by requesting it from AWS support.
&nbsp;

**Example:** Create Internet Gateway for the **VPC 192.168.0.0/24**. Just can attach one Internet Gateway per VPC.
&nbsp;


## Route Table

![Route Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qa2hhpxbsbcaiqub1vjx.png)

**Type:** Networking.
&nbsp;

**Scope:** Create routing rules to address the communications from the resource inside subnet and VPC to destination. Route Table attaches to VPC applies to all subnets of the VPC. Route Table attach directly to subnet has priority over Route Table of VPC.
&nbsp;

**Limits:** 200 Route Tables per VPC and 50 routing rules by Route Table. You can adjust the limit by requesting it from AWS support.
&nbsp;

**Example:** Create and attach new Route Table on **Public Subnet (192.168.0.0/27)** with routing rules to Internet and VPC network destinations through Internet Gateway and local routing:

![Route Table Public Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb6b4g3rhvtsf9wsg7e2.png)

For **Private Subnet (192.168.0.32/27)** create a new Route Table and attach to it with routing rule to VPC network destination through local routing:
&nbsp;

![Route Table Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ayp9hyi4mdno4loju41.png)
&nbsp; 

## Network Access Control List (NACL)

![NACL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np9cprwx3ux7r7go2qf4.png)

**Type:** Security.
&nbsp;

**Scope:** Network firewall to control inbound and outbound communication to resources deployed in the subnet using rules. The rule specifies communication protocol, port, source/destination, and allow or deny communication. The rules are sorted by number, have precedence, and are stateless, you need to create inbound and outbound rules to requests and responses.
&nbsp;

**Limits:** 200 NACL per VPC and 20 rules to IPv4 or IPV6 per NACL. You can adjust the limit by requesting it from AWS support.
&nbsp;
 
**Example:** For **Public Subnet (192.168.0.0/27)** create and attach NACL with inbound and outbound rules to allow internet access (All traffic).
&nbsp;

![NACL Inbound Public Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8w5ki5foopsl8sw1fzj.png)

![NACL Outbound Public Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1bfatpj2n80fbrcjp71.png)

For **Private Subnet (192.168.0.32/27)** create and attach NACL with inbound and outbound rules to allow communications from and to **Public Subnet (192.168.0.0/27)**.
&nbsp;

![NACL Inbound Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzr9j5jqwau3tanmwod5.png)

![NACL Outbound Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/we4qrt7mzn38cznlyoec.png)
&nbsp;

## Security Group

![Security Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flz88yf1dwdhb7go72oj.png)

**Type:** Security.
&nbsp;

**Scope:** Instance firewall to control inbound and outbound communication to instances (EC2, RDS) using rules. The rule specifies communication protocol, port, and source/destination. The rules are stateful you just need to create inbound/outbound rules for requests received/generated by the instance and is not necessary to create rules for the response.
&nbsp;

**Limits:** 2500 Security Groups per AWS Region and 60 inbound and outbound rules per Security Group. You can adjust the limit by requesting it from AWS support.
&nbsp; 

**Example:** For EC2 instance access over internet using HTTP and SSH protocols, deploy on **Public Subnet (192.168.0.0/27)**, create and attach Security Group with inbound rules with sources anywhere and ip address (your ip).

![Security Group EC2 Public Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l90a3k7svrvpvsxl6m8z.png)

For EC2 instance to internal access only (no internet access), deploy on **Private Subnet (192.168.0.32/27)**, create and attach Security Group with an inbound rule that allows communication from Security Group EC2 Instance of **Public Subnet (192.168.0.0/27)** over TCP protocol and port 22.

![Security Group EC2 Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1bix9tl23xqlszsmw3r.png)

For RDS instance to internal access only (no Internet access), deploy on **Private Subnet (192.168.0.32/27)**, create and attach Security Group with an inbound rule that allows communication from Security Group EC2 Instance of **Public Subnet (192.168.0.0/27)** and Security Group EC2 Instance of **Private Subnet (192.168.0.32/27)**, both of them over TCP protocol and port 3306.

![Security Group RDS Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g19gtkq0p51c6a3rstdq.png)

The EC2 instance on **Public Subnet** is a Bastion Host (Bridge) to connect to EC2 and RDS instances on **Private Subnet** witout internet access.
&nbsp;  

## NAT Instance & NAT Gateway

![NAT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xj9toa1gp3x574bxzue.png)

**Type:** Networking
&nbsp;

**Scope:** Internet access from instances on **Private Subnet** (without Internet Access) through exchange of ip addresses. Taken the ip address of communication packages send by instances and replace it with the ip address of NAT.
&nbsp;

**NAT Instance:** It is EC2 instances with preconfigured Amazon Machine Image (AMI) and elastic ip address or public ip. The bandwidth depends on the bandwidth of the instance type.
&nbsp;

**NAT Gateway:** Is a NAT service managed by AWS with high availability and elastic ip. Scale up to 45 Gbps in bandwidth. It can handle communication to internet, between VPCs and between VPC and on-premise corporate network.
&nbsp;

**Limits NAT Instance:** Apply EC2 limits.
&nbsp;

**Limits NAT Gateway:** 5 NAT Gateway per availability zone of AWS region. You can adjust the limit by requesting it to AWS support.
&nbsp;

**Example:**
&nbsp;

**NAT Instance:** Create EC2 instance with NAT AMI and deploy on **Public Subnet (192.168.0.0/27)** with internet access through Internet Gateway. 

On Route Table of **Private Subnet (192.168.0.32/27)** add routing rule to NAT instance when destination is internet.

![Route Table Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhtrc5whsv7tev5lwk06.png)

NAT Gateway: Create NAT Gateway and deploy on **Public Subnet (192.168.0.0/27)** with internet access through Internet Gateway. 

On Route Table of **Private Subnet (192.168.0.32/27)** add routing rule to NAT Gateway when destination is internet.

![Route Table Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mkfd0k7w7ukfixuswkf.png)

Update NACL of **Private Subnet (192.168.0.32/27)** with inbound and outbound rules that allow internet communications (Request and Response).

![NACL Inbound Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1nk15sptcaadly23p6k.png)

![NACL Outbound Private Subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqgqbbelbtmy0s23osjx.png)

Route Table and NACL of **Public Subnet (192.168.0.0/27)** without changes.
&nbsp;

## Conclusion

In this post we saw the main networking and security aws services to build a network and allow or deny the communication between the systems deployed on it. We also build a solution to protect the instances by restricting access to and from the internet with network and instance Firewall and allowing connection through Bastion Host and NAT.
&nbsp;

## References

[VPC Limites](https://docs.aws.amazon.com/vpc/latest/userguide/amazon-vpc-limits.html)
[NAT Comparison](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-comparison.html)