A quick overview and sample codes on how I used Ansible to deploy a Rails application to AWS.software development ansible devops automation
[Update 13 September 2016 - Merged playbooks into 1]
In the past week, I’ve been exploring how to automate the deployment of a Rails application into production on an AWS infrastructure. Ideally, I would like to have an environment consisting of 2 EC2 instances behind a load balancer and a Postgres database running on RDS. The Rails application requires Ruby 2.3.0 and uses PostgreSQL as a database.
This post assumes the Ansible host has been set up with AWS Access Key ID and AWS Secret Access Key as environment variables and there is an existing EC2 keypair.
deploy.yml does all the weight lifting from setting up AWS EC2 instances, PostgreSQL on AWS RDS, AWS ELB classic load balancer, installing ruby, rails and booting servers, up to registering EC2 instances to ELB.
Here is an overview of the roles, which can be found in the Gist below:
setup_aws does the following:
- Creates a security group for EC2 which allows HTTP, HTTPS, and SSH access
- Creates a security group for RDS which allows Postgres access on port
- Starts 2 EC2 instances
- Creates an EC2 ELB with a listener on port 80
- Starts a PostgreSQL instance on RDS
The other roles perform the following:
1. Installs Ruby, Rails, Nginx, and other dependencies on EC2 instances using roles
2. Precompiles and packages the Rails application locally using role
3. Copies Rails application to the EC2 instances and boot in production mode using role
4. Register both EC2 instances to ELB using role