DEV Community

harsh patel
harsh patel

Posted on • Edited on

Connecting Multi-VPC Applications Using AWS Transit Gateway

Deploying a React App Across Multiple VPCs Using AWS Transit Gateway

Introduction

If you're working with AWS and need to connect multiple VPCs efficiently, AWS Transit Gateway is your best bet! It acts as a centralized hub, making it easier to route traffic between multiple VPCs without the complexity of VPC peering.

In this guide, we'll deploy a React application where the frontend and backend live in separate VPCs. These VPCs will be connected using AWS Transit Gateway, allowing smooth communication between services. Let’s dive in!

Prerequisites

  • An AWS account with admin access
  • Basic knowledge of AWS Networking (VPCs, Subnets, Route Tables)
  • Familiarity with React and MySQL

Architecture Overview

Image description
Here’s what we’re building:

  • Frontend VPC → Hosts the React application
  • Backend VPC → Runs a MySQL database
  • AWS Transit Gateway → Connects both VPCs
  • Transit Gateway Attachments → Link each VPC to the Transit Gateway
  • Routing Tables → Manage traffic flow between VPCs
  • Security Groups → Control access

Step 1: Create Two VPCs

  1. Go to AWS VPC Console.
  2. Click Create VPC and name it FrontendVPC (CIDR: 10.0.0.0/16).
  3. Create BackendVPC (CIDR: 10.1.0.0/16).
  4. Add public and private subnets in both VPCs:

FrontendVPC Public: 10.0.1.0/24
FrontendVPC Private: 10.0.2.0/24
BackendVPC Public: 10.1.1.0/24
BackendVPC Private: 10.1.2.0/24

Step 2: Set Up AWS Transit Gateway

  1. Go to AWS Transit Gateway Console.
  2. Click Create Transit Gateway, name it, and enable DNS support.
  3. Once created, note the Transit Gateway ID.

Step 3: Attach VPCs to Transit Gateway

  1. In Transit Gateway Attachments, create:
  2. Attachment for FrontendVPC
  3. Attachment for BackendVPC

Step 4: Configure Route Tables

1.Modify each VPC’s route table:
2.Associate these routes with the correct subnets.

Image description

Step 5: Deploy the Frontend Application

  1. Launch an EC2 instance in FrontendVPC public subnet.
  2. Install Node.js and React:
    sudo yum install -y mysql-server
    sudo systemctl start mysqld
    sudo mysql_secure_installation
    mysql -u root -p -e "CREATE DATABASE app_db;"

  3. Allow inbound HTTP traffic (port 3000) via security groups.

Step 6: Deploy the Backend and MySQL Database

  1. Launch an EC2 instance in BackendVPC private subnet.
  2. Install MySQL and set up a database: sudo yum install -y mysql-server sudo systemctl start mysqld sudo mysql_secure_installation mysql -u root -p -e "CREATE DATABASE app_db;"
  3. Set up a backend API using Express.js: npm install express mysql node server.js
  4. Allow MySQL traffic (port 3306) from FrontendVPC in security groups.

Step 7: Open and Test the Application

  1. Retrieve the Public IP Address of the frontend EC2 instance.
  2. Open a browser and go to: ping <Backend-EC2-Private-IP> curl http://<Backend-EC2-Private-IP>:<Backend-Port>
  3. Test frontend-backend connectivity:
    sudo yum install -y mysql-server
    sudo systemctl start mysqld
    sudo mysql_secure_installation
    mysql -u root -p -e "CREATE DATABASE app_db;"

  4. If something fails, check:

  • Route Tables for correct routing.
  • Security Groups for inbound/outbound traffic.
  • Transit Gateway Attachments for proper linking.

This architecture allows for scalability, security, and efficient communication between VPCs.

Top comments (0)