Introduction to Deploying Angular Applications on AWS
Deploying an Angular application on AWS EC2, with an RDS PostgreSQL database and PM2 for process management, is a powerful combination for creating scalable, robust, and efficient web applications. This guide will walk you through the step-by-step process of setting up your Angular application on an AWS EC2 instance, connecting it to an RDS PostgreSQL database, and ensuring smooth operation with PM2.
Overview of the Deployment Process
The deployment process involves several critical steps, including setting up the AWS EC2 environment, configuring the RDS PostgreSQL database, securely transferring your application files, and managing your application processes with PM2. By following this guide, you will have a fully functional Angular application running on AWS, backed by a robust PostgreSQL database.
Prerequisites for Successful Deployment
Before you begin, ensure you have the following:
- AWS Account: An active AWS account with permissions to create EC2 instances and RDS databases.
- Angular Application: A ready-to-deploy Angular application.
- PostgreSQL Knowledge: Basic knowledge of PostgreSQL for database management.
- SSH Access: SSH key pair is used to access your EC2 instance.
- PM2: Familiarity with PM2 for process management.
Setting Up the AWS EC2 Environment
Launching an EC2 Instance
- Navigate to EC2 Dashboard: Log in to your AWS account and go to the EC2 dashboard.
- Launch Instance: Click “Launch Instance,” choose an Amazon Linux 2 AMI, select the instance type (e.g., t2.micro for free tier), and configure the instance details.
- Configure Security Group: Allow HTTP, HTTPS, and SSH access by configuring the security group.
- Review and Launch: Review the settings, select your key pair, and launch the instance.
Installing Required Dependencies on the Server
- SSH into EC2 Instance: Use SSH to connect to your instance.
ssh -i /path/to/key.pem ec2-user@your-ec2-ip - Update and Install Dependencies: Update your server and install Node.js, NPM, and Git.
sudo yum update -y
sudo yum install -y nodejs npm git
Configuring RDS PostgreSQL Database
Creating an RDS Database Instance
- Navigate to RDS Dashboard: Go to the RDS dashboard in AWS.
- Create Database: Choose PostgreSQL as the engine, select a free tier or appropriate instance type, and configure the database settings (username, password, etc.).
- Configure Security: Ensure the security group allows inbound connections from your EC2 instance.
Noting Down Important Database Credentials
- Database Endpoint: Note the endpoint, port, username, and password. You will need these for your application’s database connection.
Testing Database Connectivity
- Install PostgreSQL Client: Install the PostgreSQL client on your EC2 instance.
sudo yum install -y postgresql - Connect to the Database: Test connectivity using the following command:
psql -h your-rds-endpoint -U your-username -d your-database-name -p 5432
Ensuring Smooth Communication Between Application and Database
- Environment Variables: Set up environment variables in your application for database credentials to ensure secure and smooth communication.
Transferring Project Files to the EC2 Instance
Using SCP for Secure File Transfer
- Transfer Files: Use SCP to securely transfer your Angular application files to the EC2 instance.
scp -i /path/to/key.pem -r /local/path/to/angular-app ec2-user@your-ec2-ip:/path/to/remote/directory
Deploying the Backend on the EC2 Server
- Navigate to Project Directory: SSH into your EC2 instance and navigate to the project directory.
- Install Dependencies: Run npm install to install project dependencies.
cd /path/to/remote/directory
npm install
Installing PM2 for Process Management
- Install PM2: Install PM2 globally on your server.
sudo npm install -g pm2
Starting the Backend Application with PM2
- Start Application: Use PM2 to start your Angular backend application.
pm2 start npm –name “angular-app” — start
Configuring PM2 for Automatic Startup
- Startup Script: Configure PM2 to start your application automatically on server reboot.
pm2 startup
pm2 save
Conclusion
Deploying an Angular application on AWS EC2 with an RDS PostgreSQL database and managing it with PM2 ensures a scalable, reliable, and efficient web application setup. This guide provided a comprehensive walkthrough, from setting up the AWS environment to ensuring smooth deployment and operation.
References
Creating and connecting to a PostgreSQL DB instance
Adding an Amazon RDS DB instance to your Node.js Elastic Beanstalk environment