A How To: GeoDjango Deployed on AWS

Sean Hudson
Feb 3, 2018 · 6 min read

Getting a latitude and longitude search result started off so innocently (as most dark programming holes often do). Here’s where I began my search:

  • “Django lat lng distance finder”

Ah, Stackoverflow, the lifeblood of this craft; how many times have you’ve saved my unworthy ass. Then my searching began to get ugly…

  • “Install GeoDjango local machine”
  • “Setting up PostGIS on Windows machine”
  • “What’s a GDAL”
  • “How to install GDAL Windows”
  • “Seriously, how the hell do I install GDAL”
  • “Why do I do this to myself”
  • “Best way to celebrate when no one gets what you’re celebrating” (“Oh I’m really happy to hear that, sweetie! … what’s a GDAL?”)

By the way, besides Django’s instructions on installing PostGIS, this was the best set of instructions I could find. However, my laptop behaved completely irrationally compared to my desktop, so be prepared for an afternoon of hatred.

I was finally cooking with an application I could do local searches with — for example, return a list of our closest locations to White Rock, BC (my hometown) from a Google Maps geocode result. Sweet. Now to put it online!

I returned to Google to see what I was up against.

  • “AWS and PostGIS” (was happy to find out RDS’s Postgres has PostGIS installed already!)

I ambitiously ran eb deploy on my staging machine, and ran into the GDAL problem once again, but this time with a sinking feeling:

How the hell am I going to install GDAL libraries on my EC2 machine that I’ve always found a way to NOT ssh into??? Has it come to finally learning how?

AH!

Finding out that AWS has PostGIS installed, but no GDAL library was a real knife in my friggin kidneys. I have tried to SSH into my EC2 instances in the past, but always got stuck along the way with each time getting a little further. Finally I’ve cracked the code, and I’m here to share it with you.

This assumes that you have an AWS account, basic starter knowledge of its ecosystem, and aren’t afraid to get your hands a little dirty. We’re starting from a fresh machine on this one, so let’s get to it.


Okay, enough with the backstory

We’re going to use Elastic Beanstalk for brevity. Head to your EB Dashboard and use the top menu to Create a New Environment. Here’s a quick rundown of the settings I’ve used:

Web server environment → Preconfigured Python platform & Sample application
Let’s name it charlene (why? It just is, that’s why!)
(Click Configure more options here)
Modify Instances: use a t1 or t2 micro
Security: choose an EC2 Key Pair (see below if you are key-pair-less)
Database: Postgres, latest version

To create a new Key Pair on AWS: go to the EC2 console → select Key Pair from the sidebar → click Create Key Pair → mazel tof it’s a girl, give it a name → save the .pem file to your local machine → more on this later

Hit Create environment and go grab a coffee, this’ll take about 20 minutes.

If you’ve time to kill still, then setup the following for your (hopefully Windows) machine:

  • Install PuTTY, who is basically your confusing, but incredibly smart, friend
  • From the same page, download PuTTYgen.exe
  • Run puttygen.exe, and from the Conversions menu select Import…
  • Browse to and select the .pem file you saved when you created your Key Pair
  • You can password protect it for security using the Key passphrase input box
  • Click Save private key and save the file something like aws-private-key-2018–02.pek (I’m not a wacko, naming files is no time to be silly)
  • Run PuTTY, and you’ll be greeted with a really Sys-admin looking window

Here is where the connection to your EC2 machine will happen — but first, head back to your EB console and make sure your machine is done provisioning itself.

  1. Host Name is the Public DNS of your instance: EC2 Console → Instances → charlene → Description tab → Public DNS (IPv4) → paste the value into the box
  2. While you’re in the EC2 Console, you’ll need to change the security group settings to allow your computer to connect. From the Description tab, click on the Security Group: awseb… text. In the Inbound tab, select Edit → Add Rule → Use settings Type: SSH, Source: My IP, Description: Temp. I like to add a description so I can remember which one to safely remove later on.
  3. Navigate to +SSH (clicking on the +), then to Auth (clicking on Auth)

3. Click Browse and select the aws-private-key-2018–02.pek file.

4. You’re ready to connect. Click Open, and a command-lookin window will open, asking you for a user name. Use this list from AWS to determine your username (for instance, I use ec2-user)


Install the GDAL Libraries Already

Congrats on connecting to your EC2! That’s half the battle. Next: GDAL libs.

First, there are a few libraries to install: GEOS, PROJ.4, then GDAL. The Django docs are helpful here

# Install GEOS
wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2
tar xjf geos-3.4.2.tar.bz2
cd geos-3.4.2
./configure
make
sudo make install
cd ..
# Install PROJ.4
wget http://download.osgeo.org/proj/proj-4.9.1.tar.gz
wget http://download.osgeo.org/proj/proj-datumgrid-1.5.tar.gz
tar xzf proj-4.9.1.tar.gz
cd proj-4.9.1/nad
tar xzf ../../proj-datumgrid-1.5.tar.gz
cd ..
./configure
make
sudo make install
cd ..

Next, install GDAL. These instructions vary from the Django docs, which simply say:

# Hey, read the above comment about this code before just running it!
wget http://download.osgeo.org/gdal/1.11.2/gdal-1.11.2.tar.gz
tar xzf gdal-1.11.2.tar.gz
cd gdal-1.11.2

So cosider the following a more involved version, and what I found to be effective.

*Note you can find the most recent gdal lib version at http://download.osgeo.org/gdal/

sudo yum-config-manager --enable epel
sudo yum -y update
** All one command; copy from 'sudo' to 'automake'
sudo yum install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel autoconf automake gdal
cd /tmp
** All one command; copy from 'curl' to 'zxf -'
curl -L http://download.osgeo.org/gdal/2.2.3/gdal-2.2.3.tar.gz | tar zxf -
cd gdal-2.2.3/
./configure --prefix=/usr/local --without-python
** next command will take a while on a t2.micro
make -j4
sudo make install
cd /usr/local
tar zcvf ~/gdal-2.2.3-amz1.tar.gz *

Thanks to this Github Gist by mojodna. It was one of many that I tried, but ultimately the one that worked without error.

You can verify that GDAL library is installed properly by running the command

gdalinfo --version
GDAL 2.2.3, released 2017/11/20

Recap

Connecting via SSH to your EC2 instance is something you should be comfortable with, especially if you’re the sys-admin for lack of a better person! I’m largely self-taught, so this was breaking new ground, but I’m glad to have stuck through it. If you’re having connection problems I feel bad for you son, but in all honesty leave me a comment and I’ll do my best to help you.

  • *The above instructions worked twice for me on 2018–02–02, with my currently existing Windows 10 laptop, during a Waning Gibbous moon phase. Your mileage may vary.

*Update, 2018–11–01

I’ve gotten pretty good mileage from this post, so I thought I’d update the references to the latest libraries and post an end to end script!

wget http://download.osgeo.org/geos/geos-3.7.0.tar.bz2
tar xjf geos-3.7.0.tar.bz2
cd geos-3.7.0
./configure
make
sudo make install
cd ..
wget http://download.osgeo.org/proj/proj-5.2.0.tar.gz
wget http://download.osgeo.org/proj/proj-datumgrid-1.5.tar.gz
tar xzf proj-5.2.0.tar.gz
cd proj-5.2.0/nad
tar xzf ../../proj-datumgrid-1.5.tar.gz
cd ..
./configure
make
sudo make install
cd ..
wget http://download.osgeo.org/gdal/2.3.1/gdal-2.3.1.tar.gz
tar xzf gdal-2.3.1.tar.gz
cd gdal-2.3.1
sudo yum-config-manager --enable epel -y
sudo yum -y update
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel autoconf automake gdal
cd /tmp
curl -L http://download.osgeo.org/gdal/2.3.1/gdal-2.3.1.tar.gz | tar zxf -
cd gdal-2.3.1/
./configure --prefix=/usr/local --without-python
make -j4
sudo make install
cd /usr/local
tar zcvf ~/gdal-2.3.1-amz1.tar.gz *
gdalinfo --version

Sean Hudson

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade