Migrating a Node.js Application and MongoDB Database Between AWS

Moosa Zafar

Cloud Infrastructure Architect
DevOps Engineer
Amazon EC2
AWS
AWS S3
Migrating a backend application and its MongoDB database from one AWS account to another can be done seamlessly with the right steps. Let’s call the account from which you are transferring the source account and the account to which you are migrating the destination account. In this guide, we’ll walk through the steps required to complete this migration, assuming you’re using EC2 instances in both accounts.

MongoDB Dump and Transfer from Source Account

Start by creating a MongoDB dump of your database on the source EC2 instance. The MongoDB dump captures all the data from your database in a format that can easily be restored on another instance.
mongodump - out /path/to/dump
Once you have the dump, zip it to compress the data for transfer:
tar -czvf mongodump.tar.gz /path/to/dump
Next, upload the zipped MongoDB dump to an S3 bucket in your source account for easy transfer to the destination account.
aws s3 cp mongodump.tar.gz s3://your-bucket-name/

Set Up the EC2 Instance in the Destination Account

In your destination AWS account, create a new EC2 instance with the same specifications as your source instance. For example, if your source instance was t2.medium, ensure the new instance matches that. Also, add the same security groups as those in the source instance to ensure the proper network access (such as SSH, MongoDB ports, etc.).

Install MongoDB, AWS CLI, and Node.js

Once your destination EC2 instance is up, you need to install MongoDB and Node.js.
Install MongoDB
sudo apt update
Install Node.js:
sudo apt install nodejs npm

Transfer and Restore the MongoDB Dump

Now, download the MongoDB dump from the S3 bucket to your destination instance:
aws s3 cp s3://your-bucket-name/mongodump.tar.gz
Unzip the dump:
tar -xzvf mongodump.tar.gz
Then, restore the MongoDB dump:
mongorestore /path/to/dump
You can use mongo or mongosh to connect to MongoDB and verify that the data was restored correctly:
mongosh\
use your-database-name\
db.collection.find().limit(1)

Clone and Set Up the Backend Application

Next, clone the backend application repository from GitHub onto your destination EC2 instance:
git clone [https://github.com/your-repo/backend-app.git\
cd](https://github.com/your-repo/backend-app.gitcd) backend-app\
npm install
Run the application to verify that it works and is correctly pulling data from MongoDB. Make sure to open the required port (e.g., 3001) in the security group of your destination instance so that you can access the app:
node app.js

Allocate Elastic IP to EC2 Instance

To make your EC2 instance more accessible, allocate an Elastic IP and assign it to the instance. This will give your instance a permanent public IP address, even if it’s restarted.

Install PM2 to Run the Application

PM2 is a process manager for Node.js applications that makes it easy to manage and keep your app running in the background.
Install PM2:
npm install -g pm2
Run your application with PM2:
pm2 start app.js\
pm2 save\
pm2 startup
This ensures your application runs in the background and restarts automatically if the server reboots.

Set Up NGINX for Your Backend Application

Install NGINX on the destination instance:
sudo apt install nginx
Once NGINX is installed, copy the NGINX configuration file from your source instance:
cat /etc/nginx/sites-available/domain-name
Paste it into the destination instance’s NGINX configuration:
sudo nano /etc/nginx/sites-available/domain-name
Remove any Certbot-related configuration (such as SSL settings) since we will set it up again later. Save the file and create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/domain-name /etc/nginx/sites-enabled/
Finally, test and restart NGINX:
sudo nginx -t\
sudo systemctl restart nginx

Set Up Certbot for SSL

Certbot is a tool that automates the process of obtaining and renewing SSL certificates from Let’s Encrypt, ensuring your site runs securely over HTTPS.
Install Certbot:
sudo snap install - classic certbot
Now, run Certbot to obtain an SSL certificate for your domain:
sudo certbot - nginx -d [your-domain.com](http://your-domain.com)
Certbot will automatically configure NGINX to use the certificate, and your site will be accessible via HTTPS.

Update DNS and Finalize

Go to your domain registrar and update the IP address of your domain to point to the Elastic IP of your new EC2 instance. After updating the DNS, run:
sudo certbot - nginx -d [your-domain.com](http://your-domain.com)
Finally, restart NGINX:
sudo systemctl restart nginx

Conclusion

Congratulations! You have successfully migrated your backend application and MongoDB database from one AWS account to another. Your application should now be running with SSL on your destination instance, ready to serve requests from the new environment.
Partner With Moosa
View Services

More Projects by Moosa