Elastic Beanstalk (EB) is a Platform as a Service (PaaS) that helps build web apps by leveraging other cloud services provided by Amazon.
It couples the server (EC2), database (RDS), and the static files (S3). As of the day of writing this article, EB supports a wide range of applications/ technologies including Java, Django, Ruby on Rails, Sinatra, Docker, Flask, PHP and node.js. The list is most likely to expand in the near future. As Amazon puts it in their official documentation,
You can simply upload your code and Elastic Beanstalk will automatically handle the deployment, from capacity provisioning, load balancing, auto-scaling to application health monitoring. At the same time, you retain full control over the AWS resources powering your application and can access the underlying resources at any time. When your application needs updating, you upload the updated source, and Elastic Beanstalk updates the underlying resources appropriately.
There are multiple ways to deploy an application on EB. You can either create an application source bundle and upload it manually to the EB Console (within AWS website) or you can install the EB Command Line Interface (EB CLI) tool in your local system to do the same.
In case you did not know,
● In Django’s vernacular, a project can consist of a single application or multiple applications. But not everyone uses this jargon and the project itself is called as an (web) application. So, if you come across an article that says ‘ django application’ instead of ‘django project’, do not get confused. It is the same.
● Virtualenv is a tool that helps you create isolated Python environments. The main reason behind using virtualenv is related to versions and dependencies. If you need to install a certain version of a package as part of this tutorial, there is good chance that it might break another application that relied on another version of the same package. We don’t want that and virtualenv is the answer.
So, here are the steps to deploy your django project (aka django application) on EB.
- Open Command Line.
- Install virtualenv tool. Use the following command to install it, provided you have installed ‘pip’ tool.
pip install virualenv - Create a virtual environment named ‘new_ve’ by typing in the following command. This can be done from any directory you want.
virualenv new_ve - Once the new virtual environment is created, you will be able to see a new directory with the name ‘new_ve’ within your current directory. Next step is to activate the newly created virtual environment. Go to ‘new_ve\Scripts’ folder from within the commandline and type in the following command.
activate - Next, install django in the virtual environment.
pip install django==1.9.12
Create a django project/ Copy your existing Django project to the virtual environment
- Copy your django project to the same directory that contains the newly created virtual environment ‘new_ve’.
- To verify if your Django project works, change into the project directory and run the following.
python manage.py runserver - Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser to view the site.
Now that you have the site up and running on your local machine, let’s start the Amazon deployment process. The next step is to configure the project for deployment with Elastic Beanstalk.
Configure Your Django Project for Elastic Beanstalk
- Run the following command and save the output to a file named ‘requirements.txt’: Elastic Beanstalk uses ‘requirements.txt’ to determine which package to install on the EC2 instances that run your project. This file should be kept in the root folder of your django project.
pip freeze - Create a new directory, called ‘.ebextensions’ within the root folder of your django project.
- Within the ‘.ebextensions’ directory, add a configuration file named ‘django.config’ with the following content:
option_settings: aws:elasticbeanstalk:container:python: WSGIPath: mysite/wsgi.py aws:elasticbeanstalk:container:python:staticfiles: "/static": "static/"
The setting, WSGIPath, specifies the location of the WSGI script that Elastic Beanstalk uses to start your application.
Your project directory should now look like this: Here, ‘mysite’ is the project root folder.
mysite--> .ebextensions--> django.config mysite--> __init__.py settings.py urls.py wsgi.py db.sqlite3 manage.py requirements.txt
Deploy on Elastic BeanStalk
After performing all the above steps, you now have a django project that have been configured to work on the Elastic BeanStalk. But there is one problem. The project is still in your local system. Therefore, the next step is to deploy this project on the Elastic BeanStalk.
At this stage, you can either install ‘EB CLI’ tool and continue with the command line execution like you did up until now or alternatively, you can create a zip file of the project and upload it yourself on to the EB console (AWS website). Both approaches are essentially the same. Both require you to create an elastic beanstalk application, set its configurations and then deploy the app version (your project). If you install ‘EB CLI’, you do everything from your local system’s command line. But with the second approach, you use the AWS website. It is just a matter of preference.
Here are the steps to do it from AWS website. These steps were taken from the AWS official documentation.
- Bundle your project as a single .zip or .war file. A bundle can’t include a top-level folder, so you must compress the individual app files, rather than compressing the directory that contains them.
- Open the Elastic Beanstalk console at https://console.aws.amazon.com/elasticbeanstalk/.
Choose Create New Application. - On the Application Information page, enter a name for the app. Choose Next.
- On the New Environment page, choose Create web server.
- On the Environment Type page, set Predefined configuration to Node.js, and then choose Next.
- On the Application Version page, select Upload your own, Choose File, and then select the source bundle that you created. Choose Next.
- On the Environment Information page, enter an environment name and a unique environment URL. Choose Check availability to ensure that your URL is available. Choose Next.
- On the Additional Resources page, choose Next.
- On the Configuration Details page, use the default values for all settings by choosing Next.
- On the Environment Tags page, choose Next.
- On the Review page, verify your settings, and then choose Launch. On the AWS Elastic Beanstalk dashboard, you can watch in real time as Elastic Beanstalk creates an environment and deploys the app. This process takes a few minutes.
- When the deployment is finished and the environment health is listed as “Green”, open the URL of the app.
Leave a Reply