Search code examples
amazon-web-servicesamazon-s3amazon-ec2amazon-ebsamazon-elb

How to setup basic Auto Scaling for Amazon EC2 website using PHP and MySQL


I have a basic PHP / MySQL website stored entirely on an Amazon EC2 micro instance. Traffic is about to increase for a temporary period, and I would like to implement basic auto-scaling mainly to give me more CPU power since that seems to be the bottleneck. So the main constraints I know I have are:

  • Preserve MySQL database data
  • Preserve website files (which are at /var/www/* )

Can anyone point to the best way to do this?

P.S. Because my server setup is not that complex, I am willing to rebuild / re-upload all of it if I have to.. I just need to get the Auto Scaling in place correctly.


Solution

  • I had a similar problem and here's what I did.

    Prepare the App server to be auto-scalable (able to run on multiple instances simultaneously):

    • I moved my SQL databases (MySQL and MSSQL) to RDS;
    • I modified my program so it does not write to the local volumes/drives and instead put all the files in an S3 bucket;

    I tested this instance to make sure my app-server is totally separate from my DB and other write Storage. When it's all set:

    • pushed all my changes to a repository;
    • scheduled a task that will run when my server starts (and every 2 hours after) that will do a git pull origin master from the repository (to make sure the code on the new instances will always be the latest);
    • created a custom AMI of this Instance;
    • created a Load Balancer;
    • created a Launch configuration/Auto-scaler group that uses the AMI I created;

    Now that the Auto-scaler is all set:

    • I changed the DNS entry pointing to my site to use a DNS CNAME pointing to the Load-Balancer end-point (DNS name) instead of the elastic IP I originally associated to my EC2 instance (from where we created the AMI and did the testing);

    Take note: DON'T use the static IP address assigned to your Load Balancer because they will change very frequently. You really need to setup a CNAME pointing to the Load-Balancer end-point.

    Some maintenance that I needed to do:

    • I cloned a copy of the repository to a local copy where I do the testing and updates. When I need to change anything on the production server, I simply push my changes to the repository and my production server picks-them-up every 2 hours.
    • When there's already too much updates on my code from the last time I created the AMI, I recreate the AMI to reduce the amount of time needed to pull from the repository.

    And BTW, I'm using T1.micro instance. That's the whole idea of using Auto-scaling in the first place. We don't want to be spending on a Medium server all the time our app is on a slack.