DEV Community

Radurga Rajendran
Radurga Rajendran

Posted on

Building a Secure Web Application with AWS VPC, RDS, and a Simple Registration Page

Here, we will see how to set up a Virtual Private Cloud (VPC) with two subnets: a public subnet to host a web application and a private subnet to host a secure RDS (Relational Database Service) instance. We’ll also build a simple registration page hosted in the public subnet, which will log user input into the RDS instance.

By the end of this tutorial, you will have a functional web application where user data from a registration form is captured and stored securely in a private RDS instance.

  1. VPC Setup: We will create a VPC with two subnets:
  • Public Subnet: Hosts a simple HTML-based registration page with an EC2 instance.
  • Private Subnet: Hosts an RDS instance (e.g., MySQL or PostgreSQL) to store registration data.
  1. Web Application: A simple registration page on the public subnet will allow users to input their data (e.g., name, email, and password). When submitted, this data will be logged into the RDS database in the private subnet.

  2. Security:

    • The EC2 instance will be in the public subnet, accessible from the internet.
    • The RDS instance will reside in the private subnet, isolated from direct public access for security purposes.
  3. Routing: We will set up appropriate route tables and security groups to ensure the EC2 instance in the public subnet can communicate with the RDS instance in the private subnet, but the RDS instance will not be accessible from the internet.

Step 1: Create a VPC with Public and Private Subnets

  1. Create the VPC:

    • Open the VPC Console in the AWS Management Console.
    • Click Create VPC and enter the details:
      • CIDR Block: 10.0.0.0/16 (this is the range of IP addresses your VPC will use).
      • Name: Eg:MyVPC.
  2. Create Subnets:

    • Public Subnet:
      • CIDR Block: 10.0.1.0/24
      • Name: PublicSubnet
      • Availability Zone: Choose an available zone.
    • Private Subnet:
      • CIDR Block: 10.0.2.0/24
      • Name: PrivateSubnet
      • Availability Zone: Choose a different zone.
  3. Create an Internet Gateway (IGW):

    • In the VPC Console, create an Internet Gateway and attach it to your VPC.
  4. Update the Route Table for Public Subnet:

    • Create or modify the route table for the public subnet to include a route to the Internet Gateway (0.0.0.0/0 → IGW).
  5. Update the Route Table for Private Subnet:

    • Create or modify the route table for the private subnet to route traffic to the NAT Gateway (for outbound internet access, if needed).

Step 2: Launch EC2 Instance in Public Subnet for Webpage Hosting

  1. Launch EC2 Instance:

    • Go to the EC2 Console, and launch a new EC2 instance using an Ubuntu or Amazon Linux AMI.
    • Select the Public Subnet and assign a public IP to the instance.
    • Attach a Security Group that allows inbound traffic on HTTP (port 80).
  2. Install Apache Web Server:

    • SSH into your EC2 instance and install Apache:
     sudo apt update
     sudo apt install apache2
    
  3. Create the Registration Page:

    • In /var/www/html, create an HTML file for the registration form (e.g., index.html):
     <html>
       <body>
         <h1>Registration Form</h1>
         <form action="/register" method="post">
           Name: <input type="text" name="name"><br>
           Email: <input type="email" name="email"><br>
           Password: <input type="password" name="password"><br>
           <input type="submit" value="Register">
         </form>
       </body>
     </html>
    
  4. Configure Apache:

  • Edit the Apache config files to ensure the server is serving the HTML page and can handle POST requests. You can use PHP or Python (Flask, Django) for handling backend processing.

Step 3: Launch RDS Instance in Private Subnet

  1. Create the RDS Instance:

    • In the RDS Console, create a new MySQL or PostgreSQL database instance.
    • Ensure the database is not publicly accessible (so it stays secure in the private subnet).
    • Choose the Private Subnet for deployment.
  2. Security Groups:

    • Create a Security Group for the RDS instance that allows inbound traffic on port 3306 (for MySQL) or 5432 (for PostgreSQL) from the public subnet EC2 instance.

Step 4: Connect the EC2 Web Server to RDS

  1. Install MySQL Client on EC2:

    • SSH into your EC2 instance and install the MySQL client:
     sudo apt-get install mysql-client
    
  2. Test Database Connectivity:

    • Test the connection to the RDS instance from the EC2 instance using the database endpoint:
     mysql -h <RDS-endpoint> -u <username> -p
    
  3. Create the Database and Table:

    • Once connected, create a database and table to store the registration data:
     CREATE DATABASE registration_db;
     USE registration_db;
     CREATE TABLE users (
       id INT AUTO_INCREMENT PRIMARY KEY,
       name VARCHAR(100),
       email VARCHAR(100),
       password VARCHAR(100)
     );
    

Step 5: Handle Form Submissions and Store Data in RDS

  1. Backend Processing:

    • You can use PHP, Python (Flask/Django), or Node.js to handle the form submission.
    • Example using PHP:
      • Install PHP and MySQL:
       sudo apt install php libapache2-mod-php php-mysql
    
 - Create a PHP script to handle the form submission (`register.php`):
Enter fullscreen mode Exit fullscreen mode
   ```php
   <?php
   if ($_SERVER["REQUEST_METHOD"] == "POST") {
       $name = $_POST['name'];
       $email = $_POST['email'];
       $password = $_POST['password'];
       // Connect to RDS MySQL database
       $conn = new mysqli("<RDS-endpoint>", "<username>", "<password>", "registration_db");
       if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
       }
       // Insert user data into database
       $sql = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$password')";
       if ($conn->query($sql) === TRUE) {
           echo "New record created successfully";
       } else {
           echo "Error: " . $sql . "<br>" . $conn->error;
       }
       $conn->close();
   }
   ?>
   ```
Enter fullscreen mode Exit fullscreen mode
 - Place this script in the public_html directory and configure Apache to serve the form.
Enter fullscreen mode Exit fullscreen mode




Step 6: Test the Registration Form

  1. Access the Webpage:

    • Open a browser and go to the public IP address of the EC2 instance (e.g., http://<EC2-Public-IP>).
  2. Submit the Registration Form:

    • Enter a name, email, and password, then submit the form.
  • Check the RDS database to ensure the data has been correctly inserted.

MY OUTPUT:

Image description

Image description

By following these steps, we have successfully built a secure and scalable web application on AWS. The EC2 instance in the public subnet hosts the registration page, and the private subnet securely stores user data in an RDS instance. We have ensured security by isolating the RDS instance from public access, using VPC subnets, and configuring appropriate security groups.

Top comments (0)