How does my web app run across multiple EC2 instances with auto scaling


I was not able to find an answer in the AWS documentation. I intended to have both the frontend and backend of my React/Node.js web app run on one EC2 instance. However, since I don't know exactly how much CPU, memory and network capacity my web app will require, I setup an auto scaling group for my EC2 instance, with 1 instance being the minimum and a maximum of 8. When I setup the Nginx server, install Node.js and dependencies and upload my web app to one EC2 instance, what happens when auto scaling group adds more instances due to capacity demand ? Will my web app automatically run on all instances without me doing anything? Or do I need to do any setup when auto scaling group adds more EC2 instances? how does my web app run across multiple EC2 instances? Thanks in advance for helping.

  • Careful with backend on the instance. When the ASG scales in, the instances will be terminated and any data will be lost unless you setup a way to persist it. Separating out the data to a separate tier as suggested below will help with this. ASG is works simplest with stateless instances

asked 21 days ago85 views
3 Answers

You need to make the necessary configurations so that when a new instance starts, your application runs. Here I'm going to give you some options:

  • You can create a "launch template" in the "user-data" section you can launch bash commands. Here, for example, you can create a bash script that downloads your project from the repository, then place instructions for downloading the necessary libraries and then the command that starts your server in nginx.

  • Create a custom AMI that contains the necessary libraries and packages and your application is ready to start. Then you create a "lauch template" with the created AMI.

So when autoscaling launches a new instance, the instance will already know how to start your application.

As a tip, I recommend separating the front-end from the back-end so that they can scale independently or even better, move the front-end to S3 and use CloudFront to improve your architecture.

profile picture
answered 21 days ago

Hi thanks for reaching out!

You'll need a combination of Load Balacing, EC2 and an external database on EC2 or RDS if you want to scale from one instance to multiple instances. Please note that your data tier (database) should be in a separate environment for data consistency and all your web tier nodes should point to that database.

Please make sure that your web tier AMI (Amazon Machine Image) contains all the necessary configurations beforehand, so when Auto Scaling launches the new instances they will come online automatically.

Please have a look at this documentation for Wordpress which explains some concepts applicable to your use case:

I hope this helps.

Steve T
answered 21 days ago

Both great answers. Another option is to use code deploy to deploy your application to new ec2 instances that start and register. Once your application is running it will become health on the target group and requests will be routed to the new instance.

This negates the need to manage an ami and also helps you deploy new versions to current and future ec2 instances.

profile picture
answered 21 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions