TRANSIT GATEWAY
Published: 2022-07-08 12:57:51

**How to connect multiple VPC's together??**
There are 2 ways to connect multiple VPC's together:-
-> VPC PEERING
-> TRANSIT GATEWAY

**What is Transit Gateway??**
Transit Gateway is a service that enables us to connect their VPCs together as well as connect VPCs and their on-premises networks to a single gateway. It is used to establish communication in much simpler way. 

**What is VPC peering?**
A VPC peering connection is a networking connection between several VPCs that enables us to route traffic between them using private IPv4 addresses or IPv6 addresses.

**Why Transit Gateway is preferred over VPC Peering??**
Drawback is if number of network increases then, our VPC peering also increases. Then it creates confusion.
There is a formula to establish peering connection:-
[n(n-1)]/2, where n represents number of networks/VPCs.

So, if we have 6 networks then according to the formula we are required to have 15 peering connections which is time consuming and creates confusion.

So, for this reason transit gateway is preferred over VPC peering.

**DIAGRAM**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w6ppc6di988wx1y7su1.png)
 

**How to establish connection among VPC's using transit gateway??**
Step 1- Create 3 VPC's.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nynlk75xfryadszw5t27.png)
Step 2- Create SUBNETS for respective VPC's
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ao33x5826sthjlk7cikv.png)
Step 3- Make the subnet of VPC-1 as public subnet and make an Internet gateway and attach it to VPC-1 as we need to test the connection is communicating with other VPC's or not.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpsk000rpptvrha1nkv0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uph8rrzopfmql6dt6t1h.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9plo7jch1cffad7271b.png)
STEP 4- Create route tables for each subnet.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7qqaihcg6lqi4gprpjz.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0ghzp8idvewji048sqv.png) 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyeekxb14rni1tvvquyg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5suqbz8n6e55hulmlz7.png)
  
Now, create two more route tables for VPC-2 and VPC-3, then associate respective subnets.

STEP 5- 
Now we want to test VPC-1 can communicate with VPC-2 and VPC-3 so for that we need to launch servers in respective VPC's.
**For VPC-2 and VPC-3 the servers are not required to have a public IP address.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/by9iso24ktoefh8fiyve.png)

STEP-6
We need Transit gateway to establish connections among the VPC's,
So, go to transit gateway,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrhtavvcnolt999n32m2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrfj5ang0i91218ooqfh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkxblgl839azdfd6017k.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhpi83fiyu4qvb57xmhy.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21lcjavx56mpvj2v55gr.png)

STEP 7- Go to Transit Gateway Attachments,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt3n58zldczjwutpg325.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2mf65uwsndo6snljnd9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwncy628br2znzkxk2l2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92aud649eob7q3w2m9dd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6po14bjd3pfcr2l9r2bv.png)

Follow the above steps and make transit-gateway attachments for VPC-2 and VPC-3.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58w2wrr5mzo8vkwdsnxl.png)
And wait for the attachments' state to be **Available** .

STEP 8- Go to Route table of VPC-1 and click on edit routes.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtxsir45a6y3imsfmryc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nls5ygodn4hc7l91kjop.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahd4rmcgp41tbcnqjt5a.png)

Now follow, the above steps and edit routes of VPC-2 and VPC-3.
->In route table of VPC-2, add CIDR range of VPC-1 and VPC-3.
->In route table of VPC-3, add CIDR range of VPC-1 and VPC-2.
Then click on save changes.  

Now, we will use the server connected to VPC-1 (which is connected to internet gateway). From this server we will try to ping the other 2 servers. If pinging is successful then the established connection is working perfectly.
So, connect server of VPC-1 in Xshell.
then,
-> sudo su
-> cd
Then type ping 8.8.8.8 (to check it is connected to internet or not).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oncuwinnlooxt0uajd5r.png)
Now, 
->type ping (private IP of server of VPC-2)
->type ping (private IP of server of VPC-3)

If we see pinging occurs then we are successful in building a connection between VPC-1 and other two VPCs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68ksvke43e15fwrltyo3.png)
We can see the pinging is happening so, the connection is successful.