Deploy Django on AWS with Terraform and Ansible — Part 2
Continuing on from the first part of our guide, “Deploy Django on AWS with Terraform and Ansible — Part 1”, we will now using Ansible to configure our Django Page Hit Counter app.
About the Django Page Hit Counter app
The Django app that we are deploying will count every page visit to our site. It is an implementation of this django-hitcount app on Github. I did a slight modification to direct the home page to a particular version of the page hit counter. To simplify things, I packed the app code into ansible/django-hitcount.tar.gz
which will be copied over to the EC2 Instances by Ansible.
Make sure our Infrastructure exists
Make sure to have run terraform apply -var-file=”secret.tfvars”
so that our infrastructure exists. Also, make sure that they EC2 SSH Key pairs that you created are working. You should be able to use them to login to either of the EC2 Instances with a command like this.
ssh ubuntu@[EC2_INSTANCE_IPADDRESS] -i /path/to/sshkey.pem
Required Packages
The Django web app requires several packages to run. If you look at tf/user_data.tf
you will see a list of these packages including nginx, uwsgi, and libmysqlclient. We could have installed these using Ansible, however it is faster to do it during provisioning the EC2 instances.
Get the Public IP Addresses of our EC2 Instances
For Ansible to work we need to populate the ansible/hosts
file with the public IP addresses or DNS names of the EC2 Linux Instances. You could easily go into the AWS Management console and look these up. Luckily, I’m providing a script that will look this up for you, aws-asg-instances-ip.sh
.
sh aws-asg-instances-ip.sh
Ansible hosts file
Ansible uses the ansible/hosts
file to organize all of the devices that it is allowed to connect to. In our case, we want to add the public IP addresses of our EC2 Instances and also configure some values from the existing AWS infrastructure provisioned by Terraform. A sample copy of the hosts file is located at ansible/hosts.tpl
; copy that as ansible/hosts
. Update your ansible/hosts
file with the public IP addresses of the EC2 Instances, the path to your SSH key file, clb_dns_name
, aws_db_dns_name
, aws_db_username
, aws_db_password
. It should look something like this:
all:
hosts:
192.168.0.150:
192.168.0.206:
vars:
ansible_user: ubuntu
ansible_ssh_private_key_file: /home/jose/.ssh/ec2key.pem
home_dir: /var/www
django_dir: "{{ home_dir }}/django-hitcount/example_project"
static_dir: "{{ home_dir }}/static"
django_project: example_project
clb_dns_name: terraform-elb-1118941239.us-west-1.elb.amazonaws.com
aws_db_dns_name: djangodb.abcmdh19rc1t.us-west-1.rds.amazonaws.com
aws_db_username: django
aws_db_password: foobarbaz
Deploying our Code and Config files
OK. Now it’s time to deploy our code and some configuration files to the EC2 instances with Ansible. Use the command below.
ansible-playbook -i hosts deploy.yaml
ansible-playbook -i hosts config_files.yaml
After running these Ansible playbooks, the Django Page Hit Counter app should be accessible through the load balancer end point, clb_dns_name
. Go there now.
Start Playing Around
This setup is easily created and destroyed. Try changing parameters in the tf/main.cf
like the minimum number of instances in the autoscaling group, min_size
. Also try changing parts of the source code of the Django app.
Not Production Ready
This setup is a nice way of getting an introduction onto how Terraform and Ansible can help deploy your Django app. However, it is not something you would want to use in production. For one, any new EC2 instance will need to be manually configured with Ansible to run the Django app. In terms of security, it would be nice to separate the infrastructure to a 3-Tier Architecture.
Summary
In this series, we configured our local environment to run Terraform and Ansible. We then provision our web app infrastructure using Terraform. Finally, we deployed our Django Page Hit Counter app using Ansible playbooks. I hope this guide was useful to you and provided a quick baseline for which you can deploy your scalable web apps.
Keep Hustling, My Friends!