Virtual Private Cloud (VPC) -
VPC is a virtual network or data center inside AWS for one client.
It is logically isolated from other virtual n/w in the AWS cloud.
Max 5 VPC can be created in one region and 200 subnets in 1 VPC.
We can allocate max 5 Elastic IP.
Once we created VPC, DHCP, NACL and security group will be automatically created.
A VPC is confined to AWS region and does not extend between regions.
Once the VPC is created you can not change its CIDR(Classless Inter-domain routing) block range.
If you need a different CIDR size, create a new VPC.
The different subnets within VPC can not overlap.
Components of VPC-
Impled router and routing table
Internet Gateway
Security group
Network ACl
Virtual private gateway
Peering connection
Elastic IP
Types of VPC -
Default VPC
Custom VPC
Default VPC -
Created in each AWS region when an AWS account is created.
Has default CIDR, security group, NACL and route table settings.
Has an internet Gateway by default.
Custom VPC-
Is a VPC created by AWS account owner.
AWS users creating the custom VPC can decide the CIDR for VPC. security group,
Has its own default security group, Network ACL and route table.
Does not have internet gateway by default one needs to be created if needed.
Subnet →
It is a small part of VPC, it present in availability zone. It contains EC2 and database servers
There are two types of subnet:
Public Subnet -
If a subnets traffic is routed to an internet gateway the subnet is known as public subnet.
Public subnet is having internet connectivity through IGW.
It is connected with Internet gateway(connected to ISP and provides internet to public subnet) hence it is called as public subnet. Internet gateway works as a bidirectional. It gets access of internet but internet traffic comes inside
Default VPC has all subnets public
Router -> establish connections between subnets, it creates automatic when we create vpc
Private Subnet -
If a subnet does not have a route to the internet gateway the subnet is known as private subnet.
You can provide internet connectivity by adding NAT gateway/NAT instance.
This subnet is more secure than public subnet.
Internet gateway (IGW)-
An internet gateway is a virtual router that connects a VPC to the internet.
Default VPC is already attached with an IGW.
If you create a new VPC then you must attach the internet gateway in order to access the internet.
Ensure that your subnets route table points to the internet gateway.
NAT Gateway -
You can use a Network Address Translation Gateway to enable internet in a private subnet.
It is in public subnet but works for private subnet
It is unidirectional
It provides internet to private subnet from public subnet but no outside traffic can come in private subnet
You are charged for creating and using NAT gateway in your account .
To create NAT gateway, you must specify the public subnet in which the NAT
Gateway should resideYou must also specify Elastic IP address to associate with NAT gateway when you create it.
No need to assign public IP address to your private instance.
Deleting a NAT gateway disassociates its elastic IP address, but does not release the address from your account.
Internet Flow -> Router -> nat gateway-> internet
Route table-
To manage which connection you want in subnets. One by default local entry will add with same CIDR as VPC Ip when you create VPC.
For public add internet gateway
For private add NAT gateway
CIDR 0.0.0.0/0 -> All traffic
• CIDR -
Classless Inter-Domain Routing
192.168.10.0/26 -> It is class C & 26 is CIDR
255.255.255.0 -> it is subnet 3
Here to identify number of networks and host
255 is 11111111 & 0 is 00000000
Now divide 26/8 reminder is 2 like below
n is reminder
N = 8-n = 8-2 = 6
11111111 11111111 11111111 00000000
8 8 8 n=2, N =6
Q 1. How to find out the number of network?
2^n - Where n is total number of bits borrowed from host
2^2 =4
Q.2 How to find out the number IP address on each network?
2^N - Where N is total number of host bits
2^6 = 64
Q.3 How to find out the number of hosts in each network?
2^N -2 (Where N is total number of host bits)
\= 64-2
\= 62
We can create 62 EC2 instance with 62 IP addresses
Note- In every network, the first IP address is reserved for the network id and the last IP address is reserved for broadcast id.
Network-1
192.168.10.0/26
192.168.10.0 - Network ID
192.168.10.1 - First usable
-
-
-
192.168.10.62 - Last usable IP
192.168.10.63 - Broadcast ID
Netowork-2
192.168.10.64 - Network ID
192.168.10.65 - First usable IP
-
-
-
192.168.10.126 - Last usable IP
192.168.10.127- Broadcast ID
Netowork-3
192.168.10.128 - Network ID
192.168.10.129 - First usable IP
-
-
-
192.168.10.190 - Last usable IP
192.168.10.191- Broadcast ID
Netowork-4
192.168.10.192 - Network ID
192.168.10.193 - First usable IP
-
-
-
192.168.10.254 - Last usable IP
192.168.10.255- Broadcast ID
- Below IP addresses wastes and not in use
10.0.0.0 - Network Address
10.0.0.1 - Reserved by AWS VPC router
10.0.0.2 - Reserved by AWS IP address
DNS server.
10.0.0.3 - Reserved for future use
10.0.0.255 - Broadcast address
Flow to create VPC-
VPC
IGW
Subnet
Route table
Steps to create VPC-
Create a VPC with give CIDR.
Create a IGW and attached it to VPC.
Create Public & Private subnets.
Create a Public & Private route tables
Added IGW connectivity to Public routes.
Associates Public route with Public subnet and Private route with Private subnet.
Create EC2 instances in Public and Private subnets.
Take an access and verify internet connectivity.
Service Base - Infosys - Bank - USBANK -> WLTH(Business Unit)-> PCT (Platform Cloud Team)
Business demo for Product Base:
Create a VPC with CIDR value. - USBANK-WLTH-PCT-VPC (10.0.0.0/16)
Create IGW - USBANK-WLTH-PCT - IGW
Create a subnet with CIDR value. - USBANK-WLTH-PCT-SUBNET-PRIVATE-1 (CIDR) (10.0.1.0/24)
USBANK-WLTH-PCT-SUBNET -PUBLIC
1 (CIDR) (10.0.0.0/24)
Create a route table - USBANK-WLTH-PCT-PRIVATE-ROUTE, USBANK-WLTH-PCT-PUBLIC-ROUTE
Configure route table
Launch EC2 m/c in both subnets. USBANK-WLTH-PCT-PRIVATE-LINUX-1 ,
USBANK-WLTH-PCT -PUBLIC- LINUX-1
Take a access of public EC2 instance and verify internet connectivity - Verified
Take a access of private EC2 instance and verify internet connectivity - Verified - No
Internet connectivity
Now you can access public IP but cannot access Private IP because we need NAT gateway in Private route table
To access private IP from Public IP
Take access of Public EC2
On that try to access ssh Private EC2
Create new key pair with .pem
Copy key from downloads where you kept key
Paste in new file
Give permission chmod 400 filename
Try to ssh
Now you can access internet on private IP through public IP
- Create NAT Gateway and associate it - USBANK-WLTH-PCT-NAT - Yes, Internet connectivity
- Follow below steps to create VPC and get internet access using NAT gateway
- VPC - Create VPC
- VPC only - name - CIDR (10.0.0.0/16) - save
- Internet gateway- Create Internet gateway
- Name - Create internet gateway
- Attach to VPC - select specific VPC - Attach internet gateway
- Internet gateway created
Now create Private subnet
Subnets - Create subnet
- Select VPC
- Give name
- Give CIDR(10.0.1.0/24) - save
Now create Public subnet
Subnets - Create subnet
- Select VPC
- Give name - CIDR(10.0.0.0/24)
- Route table-create Route table
- Give name - select VPC -create route table
Associate subnet to route table
Select private route table - Edit subnet association
- Select private subnet- save
- Route table is created for private subnet
Create public route table
Name - select VPC - Create
- Edit route- 0.0.0.0/0 internet gateway- save
- Associate public route table with public subnet
- Two route tables are created as below
Now create two EC2 instances for public and private subnets
Private EC2 -> Edit network- select private subnet as below
- Public EC2 -> Edit network- select public subnet as below
Create NAT gateway for private subnet to get internet access from public subnet
NAT gateway - Create NAT gateway
- Name - Public subnet ID - Connectivity type Public
- Allocate Elastic IP -> one IP will generate automatically
- Add NAT gateway in private route table
- 0.0.0.0/0 - NAT gateway- save
Now connect on cli through private EC2 and you will get internet access
- Deletion flow
Terminate EC2 machines
Private Route table - delete entry of NAT
Delete NAT
Delete VPC
Release Elastic Ip
Q. Can you connect two different EC2 on different VPCs?
Ans : Yes, if both are having Public subnet we can access
And you want to communicate on Private IP you need to go with VPC peering.
VPC Tenancy
1. Default Tenancy
Shared hardware resources
No additional cost
Limited control over hardware resources.
Suitable for most workloads
It is mostly used
2. Dedicated Tenancy-
Dedicated hardware resources
Additional cost will apply
Full control over hardware resources.
Suitable for workloads requiring for high security, compliance or performance.
Q. Difference between Security group and NACL?
Router traffic first goes to NACL then security group
NACL is attached to network subnets
Security group is attached to EC2 group
One security group for one instance OR one security group to many instances
It will decide which ports need to be allowed
Inbound & Outbound in security group
Inbound → Traffic gets in EC2 machine through internet
It is traffic you selected
VPC - Security group- select any group
Outbound-> Traffic goes out from EC2 to internet
It is always all traffic
Inbound & Outbound in Network ACL
You can allow or deny traffic in inbound rule which was not available in Security group
Outbound traffic in NACL is by default Deny but in security group it was allowed
Q. What if same port is allowed and denied at same time?
Ans: In this case low rule number is prioritised by AWS.
Here traffic will be allowed
Q. You have allowed port 80 in NACL but not in security group, traffic will come at EC2?
Ans: No, traffic will come at subnet level only
Q. You have allowed port 80 in security group but not in NACL, traffic will come at EC2?
Ans: No, traffic will not get at subnet level so can’t reach to security group
Q. You have open port 80 in both security group and NACL, traffic will go to internet?
Ans: Traffic will go from security group but will block at NACL because outbound in NACL is deny by default
First need to allow outbound in NACL
Q. What is stateful and stateless? Which group is stateful and stateless?
Ans: Stateful simply means it remembers interactions
Stateless doesn’t remember interactions
Here security group is stateless because if it can flow traffic in both directions
But NACL does not allow to go traffic outside because outbound is by default Deny
Hence NACL is stateless
VPC Peering -
To get connectivity on private IP on same region
Follow below steps to create VPC peering
- Create 2 VPC with CIDR.
VPC A and VPC B as below
- Create two IGW A and B & attach to VPCs.
- Create Subnets in both VPC.
- Configure route table with IGW connectivity and associates subnets.
- Launch EC2 m/c in both VPC with Public IP.
- Create VPC peering and configure connection in both route tables
Test the connection over private IP.
Now connect EC2 instance A and over that connect B and test internet.
- Deletion flow
Delete both EC2
Remove entry from route table
Delete VPC peering
Delete both VPC