Click here to Skip to main content
15,884,986 members
Articles / Database Development / Elasticsearch
Tip/Trick

Running Geo Django on ElasticBeanstalk

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Jul 2020CPOL4 min read 3.7K   1  
Run GeoDjango on Elasticbeanstalk container using Python
How to use Python to successfully run GeoDjango on Elasticbeanstalk container

I want to tell you this story because I wasted about 14 hours to realize how to achieve it correctly.

Long Story Short

To successfully run GeoDjango on Elasticbeanstalk container using Python 3.6, you need to follow these steps:

  1. Launch EC2 instance from ElasticBeanstalk image ami-0a2fbeb2675c47493. NB! Do not use EC2 instances under the control of ElasticBeanstalk for that purpose. Launch an instance from the source image used by Beanstalk.
  2. Install GDAL 2.
  3. Create a custom AMI.
  4. Run your Django app using this AMI.

The Road So Far

We have been using ElasticBeanstalk for 2 years to deploy our Django application in one of our projects. However, we weren’t using Geocoding features for our Django app. In one of our services, we decided to use Geo features of Django and enable PostGIS support for the project.

The problem was with missing GDAL 2 library, required for Django. There is GDAL 1 in Amazon Linux’s repositories, but it’s outdated dependency to make Django enable its GeoSpatial features.

I tried to build it from sources, installed on running instance and deployed code. Everything was fine, but I wanted to make it more reliable and be sure that it would work constantly because my changes would be deleted by autoscaling. Hence, I wanted to build a custom AMI image with required libraries (I needed two more, that didn't exist in repositories).

Okay. I ran a clean configuration of the Elastic Beanstalk environment, installed everything, and created a custom AMI from EC2 instance I created using ElasticBeanstalk.

Seems pretty easy, I thought. I tried to create a new environment using this image and I found a problem with environment variables. For an unknown reason, Python container didn’t see the variables I set in the web interface.

I tried to set them using eb command and tried to set them using .ebextensions. Finally, I set them manually and had luck running the Django app, but it was unable to deploy it using eb deploy command.

In the next step, I tried to use an updated image with Python 3.7 and Amazon Linux 2, but there were a lot of differences between Amazon Linux and Amazon Linux 2 that required a lot of changes in my deployment configuration.

I started thinking about what’s wrong with my configuration and why it didn't work. I made a suggestion, that I built custom AMI wrong way and I started from the beginning.

Creating Custom AMI

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. Choose Launch Instance.
  3. Choose Community AMIs.
  4. Insert ami-0a2fbeb2675c47493 to a search bar.
  5. Choose Select to select the AMI.
  6. Launch instance.
  7. Connect with SSH.

Building GDAL

sudo yum -y update
sudo yum-config-manager --enable epel
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel
cd /tmp
curl -L http://download.osgeo.org/gdal/2.0.0/gdal-2.0.0.tar.gz | tar zxf -
cd gdal-2.0.0/
./configure --prefix=/usr/local --without-python
make -j4
sudo make install
  1. Go to EC2 Instances page.
  2. Select created instance.
  3. Go to Actions -> Images -> Create image.

Running ElasticBeanstalk Using Newly Created Image

Create an EB environment as you got used to it, you need to do the following steps:

  1. Go to “Configure More options”.
  2. Go to Capacity.
  3. Click Edit.
  4. Paste ID of a new image in AMI ID field.
  5. Press Save.
  6. Configure rest parameters.
  7. Launch application.

Deploy your Django application and enjoy it!

P.S. Try to move to EKS, sometimes Beanstalk can get you some problems:

  1. Elastic Beanstalk is a wrapper around Amazon Elastic Container Service and it already has some automation (by choosing actions in the UI interface). Thus you can’t well describe your Infrastructure using the IaaC approach.
  2. The instances in each environment run the same set of containers. For instance, you have one frontend application and one backend application. You can’t scale them independently from each other.
  3. It has a hard limit of 4KB to store all key-value pairs, thus you can’t set as many env variables as you want.
  4. One Elastic Beanstalk task is one ECS cluster (EC2 mode).

But it has the following pros:

  1. It’s one of the easiest ways to deploy a simple application to AWS.
  2. AWS Elastic Beanstalk is a good choice for a newly started application on a staging environment.
  3. It offers multi-container Docker.
  4. Has a user-friendly UI.

And, it also has some cons:

  1. It loses independently schedule a replicated set of apps on the cluster.
  2. One Elastic Beanstalk task is one ECS cluster (EC2 mode).

Conclusion

Hope my experience will save you time, and you’ll successfully run GeoDjango on Elasticbeanstalk container. Good luck with your next Beanstalk deployment.

History

  • 25th July, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Mad Devs Group LTD
United Kingdom United Kingdom
I act as a team lead for multiple projects, and still deeply involved with their coding part. I also mentor junior tech staff.
I am a CTO and co-founder at Mad Devs Group LTD. Beyond hands-on work on the company's projects, I mentor junior tech staff and conducts staff seniority assessments while providing recommendations on skills to master to my mentees and interns. I am also a tech blogger having my articles published on DZone, Hacker Noon, and other media outlets for software engineers. I am a frequent speaker at popular tech conferences like TeamLead Conf, Golang Conf, HighLoad++, and FOSDEM.

If you have any questions or need more tips and tricks, feel free to write to me.

Comments and Discussions

 
-- There are no messages in this forum --