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

NB❗Sometimes working with Elastic Beanstalk can be like this.

Long story short

To successfully run GeoDjango on an Elastic Beanstalk 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 Elastic Beanstalk 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 the Geo features of Django and enable PostGIS support for the project.

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

I tried to build it from sources, installed it on a running instance, and deployed the 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 the required libraries (I needed two more that didn't exist in the repositories).

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

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, the Python container didn't see the variables I set in the web interface.

I tried to set them using the 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 the 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 the wrong way, and I started from the beginning.

Creating a custom AMI

  1. Open the Amazon EC2 console.
  2. Choose Launch Instance.
  3. Choose Community AMIs.
  4. Insert ami-0a2fbeb2675c47493 into a search bar.
  5. Choose Select to select the AMI.
  6. Launch instance.
  7. Connect with SSH.

Building GDAL

GitHub Gist is loading...

  1. Go to the EC2 Instances page.
  2. Select the created instance.
  3. Go to Actions -> Images -> Create image.

Running Elastic Beanstalk using the newly created image

Create an EB environment as you are 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 the ID of a new image in the AMI ID field.
  5. Press save.
  6. Configure rest parameters.
  7. Launch the 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). 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 can independently schedule a replicated set of apps on the cluster.
  2. One Elastic Beanstalk task is one ECS cluster (EC2 mode).
Image.

Conclusion

I hope my experience will save you time, and you'll successfully run GeoDjango on an Elastic Beanstalk container. Good luck with your next Beanstalk deployment.

Custom backend solutions by Mad Devs.

Latest articles here

The Nuances of Testing in Pair Cucumber.js + Puppeteer.

The Nuances of Testing in Pair Cucumber.js + Puppeteer

This article will talk about some points that will save time for those who will write tests using the tools: Cucumber.js + Puppeteer. I hope it will...

Reduce GPS

Reduce GPS Data Errors on Android App with Kalman Filter and Accelerometer

One day I got a cool task to improve the accuracy of positioning and distance calculating based on GPS data on Android devices for the taxi service...

Storage of Media Files on Multiple Django or My First Experience in Open Source.

Storage of Media Files on Multiple Django or My First Experience in Open Source

In this article, I will try to share my first experience in open source, on how to build a Python package by publishing it in PyPI, as well as to...

Go to blog