# Transit Gateway

Hello Everyone,

Today we will be exploring Transit Gateway.

A Transit Gateway is a network transit hub that enables you to interconnect your VPCs, VPN connections, and Direct Connect gateways through a central point, simplifying your network architecture.

Let's get started.

---

### Overview

We will be creating three VPCs, each with a subnet and an instance within that subnet. We'll set up a Transit Gateway to connect these three VPCs. Once all three VPCs are connected, we will SSH into our EC2 instances and perform some task to verify if they can communicate with instances in the other VPCs.

---

### Task 1: Creating a VPC.

Search for **VPC** in the search box and on **VPC dashboard**, click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594159104/2516d407-2c3c-4629-835f-b03f83be99b3.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594246641/6e4e5eb8-0949-479b-9d6d-aa145d80f048.png align="center")

Provide a suitable name for your VPC and input a CIDR range value.

Keeping all other configurations as default, click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594298756/7d1c80ba-5821-4d1c-8590-0ca85e5a9bb0.png align="center")

We have created our first VPC.

Now, let's repeat the process two more times. Ensure the CIDR range for your other VPC does not overlap.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594441473/2e2108f6-4a1d-4993-a3b5-166d5217be74.png align="left")

We have created three VPCs. Let's move on to the next task.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717641471895/ddf4bb4b-a86b-4fb1-bba6-3cbc4e352455.png align="center")

---

### Task 2: Creating an Internet gateway

On **Internet gateways**, click on `Create Internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594575864/dc32e172-d10c-4a24-8148-e9e57b229303.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594630885/bd5f6c1f-b031-43a2-a6e9-8b0ef0146ee4.png align="left")

Provide a suitable name for your Internet Gateway and click on `Create Internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594686895/260e130c-8767-4ac4-b249-bdfb5a532ca1.png align="left")

Now, we should attach the Internet gateway to our VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594727185/81a9024c-58ee-4d23-8b84-22c8b4517232.png align="left")

Select your first VPC and click on `Create internet gateway`.

Now, repeat the same process for the remaining two VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594881441/26d3b7b8-dae7-48bb-8bd8-53c5b9285f5b.png align="center")

Alright, we have created three internet gateways and attached them to the VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717641564209/9bae2f63-acf2-4da2-8e0b-86b80183ee57.png align="center")

---

### Task 3: Creating Subnets

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717594949091/671e0008-3504-47e1-8c09-2315626d8963.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595004091/6ed6c266-2603-4495-a064-bb8b1eaaef2b.png align="left")

Select your first VPC, pick an AZ and provide an eligible CIDR range.

Click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595082493/923621f5-8d0b-44dd-8a0d-fb30717b855f.png align="left")

We have created the first subnet. Now, let's repeat the process for the other two subnets.

Ensure that you provide eligible CIDR ranges for your subnets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595225949/be9054d3-98da-4cce-add4-f52ade439e9a.png align="center")

We have created three subnets, each residing in a different VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717641925656/4bf35a88-cf1b-45e8-87b7-6764966e5037.png align="center")

---

### Task 4: Creating Route Table

On **Route table**, click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595291460/e48861f1-32a9-4f7b-bdf4-47474b0a8deb.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595329813/f81fe597-a98b-4714-acd0-efa8681a3670.png align="center")

Provide a suitable name for your route table, select your VPC and click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595406103/a2c2e805-8fca-406f-b62b-0ee96c7e6c1d.png align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595455375/16e37ead-b605-437a-816d-7bc9a61a1a7d.png align="left")

Tick your subnet and click on `Save associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595494424/bac002f4-87ea-40ca-b006-598543c7be1e.png align="left")

Now, on **Routes**, click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595555414/d54ed25f-5693-407d-91df-b1d5e85c93c1.png align="center")

Click on **Add route**, configure the above configurations, and click on `Save changes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595617773/674feea7-a2b3-487e-b9e8-aba1434ac3b5.png align="center")

Now, we have associated the subnets and set up the necessary routes for the current VPC. Let's repeat the process for our other VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595776104/99a697f4-e39d-4af3-b4a8-d63c7a6988e5.png align="left")

We have created our route tables.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717642045396/4a5d74e0-cb61-45d3-86d1-e601e49c742f.png align="center")

---

### Task 5: Creating EC2 Instances

Search for **EC2** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595858058/34e1b440-3ddd-4964-b344-63d16fc875e3.png align="center")

On **Instances**, click on `Launch Instances`.

Provide a suitable name for your instance, choose an AMI, either create or use an existing key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717595916064/2280a545-aea0-4567-bf6b-fb996863e0ba.png align="left")

Edit Network settings, select your VPC, and `Enable` the Auto-assign public IP option.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717596019558/aad20393-d9c9-4f34-a34a-552c098d5c6d.png align="left")

We are provided with default SSH configuration. Now, click on `Add security group rule` to add a rule for HTTP requests.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717596069447/39e214de-3eae-4e5c-905b-9fb08a467a0b.png align="left")

Configure the above configurations in your new security group rule.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717596133237/5cbc8d51-c834-4828-aa7e-3591d513cd12.png align="left")

Scroll to bottom & click on **Advanced details**.

On **User data**, paste the following command.

```bash
#!/bin/bash

# Update and install apache2
apt-get update
apt-get install -y apache2

# Create a simple index.html file with "Hello" text
echo "Hello from the FIRST" > /var/www/html/index.html

# Restart apache2 service
systemctl restart apache2
```

Click on `Launch instance`.

Now, it's time to set up the other two instances. Repeat the process while making necessary changes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717596516806/36a61a67-22bf-45f4-a300-d6420ae9f0ca.png align="left")

Setting up our second instance.

Configure security rule like we did earlier.

And paste the following in the user data for our second instance.

```bash
#!/bin/bash

# Update and install apache2
apt-get update
apt-get install -y apache2

# Create a simple index.html file with "Hello" text
echo "Hello from the SECOND" > /var/www/html/index.html

# Restart apache2 service
systemctl restart apache2
```

Click on `Launch instance`.

Now, time to set up our third instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717598561537/a547150b-804d-4f39-a6aa-ee1ef68828dc.png align="left")

Setting up our third instance. Make sure you have added the additional security group rule.

On **User data** section, paste the following.

```bash
#!/bin/bash

# Update and install apache2
apt-get update
apt-get install -y apache2

# Create a simple index.html file with "Hello" text
echo "Hello from the THIRD" > /var/www/html/index.html

# Restart apache2 service
systemctl restart apache2
```

Click on `Launch instance`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717598774314/6fcedb54-91d0-4f98-8d8d-fed78bcd1a88.png align="center")

We have launched three instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717642332134/5d781a7b-c245-4757-b8ad-7c3bbf679533.png align="center")

---

### Task 6: Setting Transit Gateway

Search for **Transit Gateway** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717598853846/cce53576-0319-434a-a5c5-3487202288d8.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717598917458/52bc9cb5-ab27-4563-b718-a1d4eec118fc.png align="center")

Provide a suitable name for your transit gateway.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717599144776/4664584a-531f-49a9-9df3-74ed4de30b59.png align="left")

We left these settings as default. The ASN will be provided by AWS. Now, let's discuss these options for a while.

**Transit Gateway** is a central hub for connecting and managing different networks. **ASN** is a unique identifier used to help the Transit Gateway route traffic between these networks using Border Gateway Protocol.

**DNS Support** means the ability to resolve domain names to IP addresses. When enabled, DNS support allows resources connected through the Transit Gateway to use DNS resolution for communication.

**Equal-Cost Multi-Path (ECMP) Support** for VPNs allows the use of multiple paths for data traffic. This improves load balancing and redundancy by distributing traffic across multiple VPN tunnels that have the same cost (or path length).

**Default Route Table Association** refers to the automatic association of new VPC attachments to the Transit Gateway's default route table. This means that any new VPCs connected to the Transit Gateway will automatically use the default route table for routing traffic.

**Default Route Table Propagation** means that routes learned from new VPC attachments are automatically added to the default route table of the Transit Gateway. This ensures that all connected networks are aware of each other without manual route updates.

**Multicast Support** allows the transmission of data from one sender to multiple receivers simultaneously. This is particularly useful for applications like video conferencing, where the same data needs to be sent to multiple users at the same time.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717600796249/11411974-bf0e-4bc4-88f6-7cae21581459.png align="left")

It automatically accepts requests from other AWS accounts to attach their VPCs or VPNs to your Transit Gateway.

With all other configurations left as default, click on `Create transit gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717601382097/b551636c-3249-4248-a0d3-697fbe5d5625.png align="left")

We can see the details of our transit gateway.

Now that we have created a transit gateway, it's time to create transit gateway attachments.

A Transit Gateway Attachment is needed to connect your VPCs, VPN connections, or Direct Connect gateways to the Transit Gateway. The attachments essentially establish the pathways through which data can travel between your networks using the Transit Gateway.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717633343172/991383d5-1f8f-4a96-8e7c-c308a8d61a99.png align="left")

On **Transit gateway attachments**, click on `Create transit gateway attachment`. Since we have three VPC connections, we need to create a transit gateway attachment for each of them.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717633475673/aed2046e-ddcc-4301-aeb3-4d16965947c8.png align="left")

Provide a suitable name and select your Transit Gateway. Since our Attachment type is VPC, we can keep it as the default option.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717633573682/46f193f4-3361-41f2-aa6a-a57a9a40268d.png align="left")

Select your first VPC for the VPC ID. Keep all configurations as default, then click on `Create transit gateway attachment`.

Now, let's repeat the process to create the other two transit gateway attachments.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717633833728/1da31b5e-e52e-46c0-8d4c-ae0cc8a413ec.png align="left")

We have created three transit gateway attachments and associated them with their respective VPCs.

Now, it's time to update the route tables of all three VPCs. We need to add routes that direct traffic destined for the other VPCs to the Transit Gateway.

Let's start with the route table of the first VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717634886946/b9bf8528-da5a-4452-93c7-1a1629f16283.png align="center")

Select your first route table and on **Routes**, click on `Edit routes`.

Duplicate your current tab and navigate to the CIDR value of your second VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717634984182/de734cdd-9f31-4e72-ba6e-4e5ae3b70701.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635024310/dd367964-160a-4ea4-9043-75147a1f7fce.png align="center")

Now, click on `Add route`, paste the copied CIDR into the **Destination** field, and select your transit gateway attachment in the **Target** field on Transit Gateway.

Click on `Save changes`.

Now, click on `Add route` again.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635144244/58566d4a-c807-4a89-9cef-1854d9d54c4f.png align="left")

Copy the CIDR range of the third VPC and follow the same steps as before.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635276738/3fefd4a1-51cb-49b8-88bd-63cb8383898a.png align="center")

Click on `Save changes`.

Now, we need to repeat the process for our second and third VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635402671/23dc2414-c2cc-43ec-b423-d18a50169f69.png align="center")

We have completed all the necessary route setup for our second VPC.

Click on `Save changes`.

Now, let's proceed with our third VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635533138/5b87d6b6-ed4b-4b41-9040-62f3c356f760.png align="center")

Okay, the necessary routes for the third VPC have been configured.

Click on `Save changes`.

Now that all setups are complete, it's time to test the connectivity between the VPCs to ensure everything is working as expected.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717643279876/df3ca6fc-9b01-4d60-8ccf-8042aaf8ec0a.png align="center")

---

### Task 7: SSH into EC2 instances.

Go to **EC2 dashboard** by searching for **EC2** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635748096/be8888f5-6758-48c6-b4c3-8a6a7a975557.png align="center")

Select your first instance and click on `Connect`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635779520/da3bb19d-76e1-4c00-bdff-e6d51a0b3529.png align="left")

Now on SSH client, we have to follow given procedures.

Open terminal in your local machine.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635891821/0eb7fc46-b14a-45f7-94e1-9a0749013036.png align="left")

Paste the copied command into your terminal to change the key permissions.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635927358/73a55409-6215-4d19-bfe6-2640edbb3c3d.png align="left")

Now copy the command and paste it into your terminal to SSH to your instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717635977551/096a32d4-96cf-4cb2-b281-c484b18f3717.png align="left")

Click on `Enter`, proceed with **yes**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636025449/cdad64a9-21a2-4404-b097-b4b641ae5930.png align="center")

We're now in our first instance. Let's check if we can communicate with the other two instances that are in different VPCs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636118838/ad4d1462-5efe-4828-b797-d1ad30ef7bb8.png align="center")

Select your second instance & copy the **Private IPv4 address**.

```bash
curl <Private IPv4 address>
```

Paste the command into your terminal.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636260785/993f55a0-d89f-4c01-929f-e528007b8bf6.png align="left")

Alright, we've received a response from our second instance.

Let's check for a response from our third instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636342182/718c939b-bdf0-4db0-91e1-044f552768e0.png align="left")

We've received a response from our third instance as well.

This confirmation was from our first instance in our first VPC. To check the connectivity from our other two instances in different VPCs, we need to SSH into them and follow the same process we used for the first instance.

Let's now check from our second instance in our second VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636565049/c57f3e26-0f85-4dd5-a22b-8fd2db15062b.png align="left")

Now it's time from our third instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717636692787/b421051b-e23f-4fa9-85d0-e69f25aec567.png align="left")

Alright, we have tested connectivity from all of our instances, and they all have a connection.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717644517436/fb78511d-7cd4-4c6b-b6cd-9f8cd9e0d2be.png align="center")

---

### Conclusion

We have successfully set up a transit gateway and connected three VPCs to it. We tested the communication between instances in these VPCs using a simple `curl` command, confirming that they can communicate with each other.

---

### Task 8: Clean Up

1. **Terminating Instances.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717637116659/1c009bb6-b5e5-46e4-9743-4c913927a262.png align="center")
    
    Select all your instances and in the **Instance state** panel, click on `Terminate instance`.
    
2. **Deleting Transit Gateway attachments.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717637667541/a782708c-54a8-4cd5-86a0-2f95d9d7a246.png align="center")
    
    Go to **VPC dashboard**, scroll down to **Transit gateways**, in the **Actions** panel, click on `Delete transit gateway attachment`. Repeat this process for the remaining two attachments. Note that it will take some time for the deletion to complete.
    
3. **Deleting Transit Gateway.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717637752080/32af7ee4-542d-40c0-afd7-923c66fc7ccd.png align="center")
    
    Select your transit gateway and in the **Actions** panel, click on `Delete transit gateway`.
    
4. **Deleting Subnets.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717637211064/d42b1aab-4d18-4e18-99bd-60737d5e5f05.png align="center")
    
    On **Subnets**, select all your 3 subnets and in the **Actions** panel, click on `Delete subnet`.
    
5. **Deleting VPCs.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717637970382/bb6abc2f-0c10-44b2-a0b3-87457d38a6b6.png align="left")
    
    Select your first VPC and in the **Actions** panel, click on `Delete VPC`.
    
    Deleting a VPC will also remove all associated route tables and internet gateways. Repeat the process for the other two VPCs.
    

We have removed all the used resources now.

---

Alright, see you in the next one!
