Search code examples
postgresqlpostgisgdalamazon-elastic-beanstalkgeodjango

Setting up Django with GeoDjango Support in AWS Beanstalk or EC2 Instance


So I have at one point had this going via Beanstalk, using Amazon Instance (2013.09) ami-35792c5c . At the time this ebextension scripts worked great when placed in the root of your repo in .ebextensions/

00_repo.config

packages:
    rpm:
        pgdg-redhat93-9.3-1: 'http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm'
        remi: 'http://rpms.famillecollet.com/enterprise/remi-release-6.rpm'

files:
    "/etc/yum.repos.d/pgdg-93-redhat.repo":
        mode: "000644"
        owner: root
        group: root
        content: |
            [pgdg93]
            name=PostgreSQL 9.3 $releasever - $basearch
            baseurl=http://yum.postgresql.org/9.3/redhat/rhel-6-$basearch
            enabled=1
            gpgcheck=1
            gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-93

            [pgdg93-source]
            name=PostgreSQL 9.3 $releasever - $basearch - Source
            failovermethod=priority
            baseurl=http://yum.postgresql.org/srpms/9.3/redhat/rhel-6-$basearch
            enabled=0
            gpgcheck=1
            gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-93


commands:
    epel_repo:
        command: yum-config-manager -y --enable epel

    remi_repo:
        command: yum-config-manager -y --enable remi

01_app.config

packages:
    yum:
        libtiff-devel: ''
        libjpeg-devel: ''
        libzip-devel: ''
        freetype-devel: ''
        postgresql-devel: ''
        gdal: ''
        gdal-python: ''
        geos: ''
        proj: ''
        libmemcached: ''
        libmemcached-devel: ''
        cyrus-sasl-devel: ''
        zlib-devel: ''

container_commands:
    01_collectstatic:
        command: 'PYTHONPATH=.:..:../lib cd site/kpmkhv && ./manage.py collectstatic -c --noinput && cd ../..'
        leader_only: true
    02_syncdb:
        command: 'PYTHONPATH=.:..:../lib cd site/kpmkhv && ./manage.py syncdb --noinput && cd ../..'
        leader_only: true
    03_migrate:
        command: 'PYTHONPATH=.:..:../lib cd site/kpmkhv && ./manage.py migrate --noinput && cd ../..'
        leader_only: true

option_settings:
    - namespace: aws:elasticbeanstalk:container:python
      option_name: WSGIPath
      value: site/kpmkhv/wsgi.py
    - namespace: aws:elasticbeanstalk:container:python:staticfiles
      option_name: /static/
      value: site/kpmkhv/static/
    - option_name: DJANGO_SETTINGS_MODULE
      value: settings_prod

So now when I use the same instance and launch my environment, I get this error regarding a dependency.

Error: Package: gdal-libs-1.9.2-5.rhel6.x86_64 (pgdg93)
            Requires: libpoppler.so.5()(64bit)
  You could try using --skip-broken to work around the problem
  You could try running: rpm -Va --nofiles --nodigest

Looks like the same repo is now returning a newer version of poppler, it was 12.x and now its 22.x and gdal needs the old version.

I also tested this out on an EC2 Instance and got the same error. But then I ran into this document from amazon on locking an AMI to its original repository version.

So adding this to User Options via the EC2 Console when you launch fixes the problem on EC2:

#cloud-config
repo_releasever: 2014.03

What is the best way to always have this option when your Beanstalk launches the EC2 Instance on your behalf? I read about cloud-init and perhaps a script deployed via ebextensions would be the best bet?

Any insight on this is appreciated, thanks.


Solution

  • So I now have a working ebextensions workflow on 2013.09 stack ami ami-35792c5c. For the 2014.09 stack see the other solution. The solution below works with postgis by installing the needed gdal component, 00_repo.config needs to look like this:

    files:
      "/etc/yum.repos.d/pgdg-93-redhat.repo":
        mode: "000644"
        owner: root
        group: root
        content: |
          [pgdg93]
          name=PostgreSQL 9.3 $releasever - $basearch
          baseurl=http://yum.postgresql.org/9.3/redhat/rhel-6-$basearch
          enabled=1
          gpgcheck=1
          gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-93
    
          [pgdg93-source]
          name=PostgreSQL 9.3 $releasever - $basearch - Source
          failovermethod=priority
          baseurl=http://yum.postgresql.org/srpms/9.3/redhat/rhel-6-$basearch
          enabled=0
          gpgcheck=1
          gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-93
    
    commands:
      epel_repo:
        command: yum-config-manager -y --enable epel
      remi_repo:
        command: yum-config-manager -y --enable remi
    
    packages:
      rpm:
        pgdg-redhat93-9.3-1: 'http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm'
        remi: 'http://rpms.famillecollet.com/enterprise/remi-release-6.rpm'
        qt4-devel: 'http://mirror.centos.org/centos/6/os/x86_64/Packages/qt-4.6.2-28.el6_5.x86_64.rpm'
    

    The 2nd extension stays in tact. This works on Amazon Instance (2013.09) ami-35792c5c, I haven't tried the newer containers yet with it.

    Alternative 1:

    If you want less dependencies on repos / rpms from the ebextensions file, you could upload all required rpms to S3 and update the ebextensions 'packages' to point to your s3 rpms. Setup your S3 bucket for public get access using CORS headers.

    Alternative 2:

    Create a custom AMI where you compile all dependencies from source. This way there will be no rpm conflicts and you don't have to temper with the default repos supplied by the AMI you are customizing. See this answer: Configuring Amazon Elastic Beanstalk with PostGIS

    Also check out the tool I made:

    https://github.com/radlws/django-awseb-tasks