It feels like everyone today wants to build the next great scalable web application. Amazon Web Services has emerged as one of the leading cloud providers when it comes to deploying and scaling our web applications. If you’re new to AWS, it can seem overwhelming. Today, there are over 40 different AWS services, and many concepts to consider when building your application. However, AWS provides helpful guides, excellent documentation on each of their services, white papers, reference architectures for common apps and more. Now, coming to Rails, the idea of adopting it, wasn't given weight merely because of its technicalities, but rather because it is an ecosystem developed for startups. Thanks to its big-name users like Twitter, Groupon, and Hulu and all the Agilists hyping Rails, has sparked the need for services like Heroku, AWS, New Relic, and Github, allowing Rails to scale.
There are more providers, less complex than AWS for deploying your web applications. However, deploying scalable web apps can bring some unique complexities, and it helps to rely on a giant’s shoulder for the job. Now, you are perhaps solving a lot of these complexities yourself by putting together an infrastructure on AWS. Even so, with Elastic Beanstalk you can get a completely scalable solution without having to configure all of the components yourself.
A scalable web application automatically brings forth additional resources without any need of human intervention or assistance. In this type of automated orchestration, you’ll require a fair bit of infrastructure, something that takes time to configure. Picture an infrastructure including application servers, load balancers, database servers, firewall, log aggregation, monitoring and alerting. If you do not automate, just about every component of your infrastructure will require updating to make it aware of the new resources. Now with Elastic Beanstalk, the creation and tear-down of resources are handled automatically, since it comes with all the components of a robust infrastructure. In other words, ideal if you’re looking to scale soon. Below are the steps involved in deploying your Rails application on AWS Elastic Beanstalk.
1) First, ensure the app is running locally within a Docker container
Feel free to base your Dockerfile on the official Rails image. If you’re used to Heroku, use Herokuish to save yourself a couple of hours of figuring out which dependencies you need to install along with your application. End of the day, it should be possible to run your application locally using Docker and control its parameters using ENV variables.
2) Prepare a Virtual Private Cloud
If you’re new to AWS, setting up a Virtual Private Cloud can prove to be a nightmare. Begin with the most straightforward setup and work your way up. Follow the VPC creation wizard to configure an uncomplicated, three public subnets VPC. See, there’s no reason to complicate things with private subnets when you’re starting things up as you can always limit access using security groups.
3) Use the Amazon Relational Database Service (RDS) for Postgres DB
Begin by launching an RDS instance into a VPC you just created. Like the last bit, just follow the wizard and you’ll manage to do a decent job. Also, ensure that you create the database and run the migrations before running the application in Elastic Beanstalk.
4) Prepare the Elastic Beanstalk environment
If you’re new to Elastic Beanstalk, start with the UI wizard to help you out. When it comes to platform, choose the multi-container Docker option available under the Generic section. This option will instruct Elastic Beanstalk to use ECS (Docker container’s orchestration service) under the hood. See, ECS is a lot more powerful than your regular Docker platform and will allow you to deploy multiple Docker containers along with your application. If you’re looking to include third-party add-ons for monitoring or log aggregation, this will prove truly useful. If you're planning to use SSL, be sure to upload your SSL certificate to the Certificate Manager or ACM. Later on, you can assign this certificate to your ELB (LoadBalancer). Be sure to assign your instances a security group in the Security section, having access to the Postgres database you just created. Also in the Network section, make sure you choose the VPC you created above.
5) Upload your application’s Docker image to a registry
You can use AWS ECR, which integrates well with Elastic Beanstalk or if you prefer, Dockerhub’s private repository.
6) The Command Line Interface (CLI) will allow you to deploy your application
Download Elastic Beanstalk’s CLI (eb). You’ll need it to deploy your application to your newly-created environment. Describe how your Docker container is to be deployed using the ‘Dockerrun.aws.json’ file. By now, you’ve created almost all the AWS resources you will need. Go ahead and run the “eb deploy” command and your application will deploy to the new environment.
By now you would’ve come to realize that the process of deploying a new Rails application to Elastic Beanstalk isn’t quite as uncomplicated and streamlined as deploying to Heroku. Therefore, it’s recommended to use Heroku for small projects and if you're working in a startup, in your initial stages. You can always migrate to AWS down the road when your product grows, and you have, ideally more resources to invest in setting up and managing AWS resources.
Bottomline, make sure AWS is right for your workload. If you are keeping all of your servers online 24/7 and basically don't have an elastic workload, then look around for a dedicated hardware from another provider. AWS makes sense when you can take advantage of its ability to spin up and spin down your instances as and when needed.