Hosting a Secure Node.js App on AWS: EC2 for Code, RDS for Postgres


In today’s cloud-native era, developers often look for scalable, secure, and cost-efficient ways to deploy applications. This guide walks you through hosting a secure Node.js application using Amazon EC2 for the application backend and Amazon RDS (PostgreSQL) for your relational database. We will focus on best practices for deployment, security hardening, and operational efficiency.


Why Use AWS EC2 and RDS?

  • Amazon EC2 gives you complete control over your compute resources and flexibility in deploying and managing your app.

  • Amazon RDS for PostgreSQL simplifies database management tasks like backups, patching, scaling, and high availability.

This combination enables robust backend hosting with scalable and managed databases.


Architecture Overview

Components:

  • Node.js backend hosted on Amazon EC2 (Amazon Linux 2)

  • PostgreSQL database hosted on Amazon RDS

  • Security Groups, IAM Roles, and Key Pairs for secure communication

  • Nginx (optional) as a reverse proxy


Step-by-Step Guide to Hosting Securely

1. Provision Your EC2 Instance

  • Use Amazon Linux 2 or Ubuntu AMI

  • Select appropriate Instance Type (e.g., t3.medium for moderate traffic)

  • Create a Key Pair for SSH access.

  • Set Security Group rules: Only allow SSH (port 22) from your IP and HTTP/HTTPS (ports 80/443), and restrict outbound access if needed.

2. Install Node.js and Required Dependencies

SSH into your EC2 instance:

sudo yum update -y

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

sudo yum install -y nodejs git


3. Clone Your App and Start the Server

git clone https://github.com/your-org/your-node-app.git

cd your-node-app

npm install

node server.js


Use a process manager like PM2 for production:


npm install -g pm2

pm2 start server.js

pm2 startup

pm2 save



4. Create and Configure Amazon RDS (PostgreSQL)

  • Launch a new PostgreSQL RDS instance

  • Configure VPC, subnet, and security groups to allow inbound connections from EC2’s private IP.

  • Enable automatic backups, encryption, and multi-AZ for HA (high availability).

Use a .env file to connect:


PG_HOST=your-db-instance.abcdefg.us-east-1.rds.amazonaws.com

PG_PORT=5432

PG_USER=youruser

PG_PASSWORD=yourpassword

PG_DATABASE=yourdatabase


Use pg in Node.js:


const { Pool } = require('pg');

const pool = new Pool({

  host: process.env.PG_HOST,

  user: process.env.PG_USER,

  password: process.env.PG_PASSWORD,

  database: process.env.PG_DATABASE,

  port: process.env.PG_PORT

});



5. Secure Your Deployment

  • If you expect to scale, set up an Application Load Balancer (ALB).

  • Use HTTPS via ACM SSL certificates on ALB or Nginx/Certbot.

  • Enable IAM roles for EC2 to access RDS (optional advanced control)

  • Enable CloudWatch Logs to monitor Node.js and RDS performance.

  • Use VPC with private subnets for RDS and NAT Gateway for EC2 if required.


6. Automate with CI/CD (Optional)

Use GitHub Actions, CodeDeploy, or Jenkins to automate testing, building, and deploying your app to EC2.


Bonus Security Tips

  • Never expose your database directly to the public internet.

  • Regularly rotate secrets and passwords.

  • Keep EC2 OS and Node.js up to date with security patches.

  • Use AWS Systems Manager Parameter Store for secure credential storage.


Conclusion

Hosting a secure Node.js application on AWS using EC2 and RDS for PostgreSQL offers the perfect balance of control, scalability, and security. Whether you're launching a small project or a production-ready app, this architecture lays a solid foundation for future growth.

Comments

Popular posts from this blog

ECS Deployment Best Practices: Blue/Green with CodePipeline and CodeDeploy

HTTP Basic vs API Key Auth: Best Practices for Secure API Development

Creating BI Solutions: AI/BI Genie Space Authoring Best Practices in Databricks

YouTube Channel