Elastic beanstalk require python 3.5
Asked Answered
O

2

5

I recently created a new python program using the latest stable release of python (3.5). Unfortunately, AWS EB does not provide a 3.5 base image. I have been trying to configure .ebextensions to get the image to upgrade the python distro as it's first action. I have not had success. I have tried the following:

Packages

packages:
    yum:
        postgresql93-devel: []
        python35: []

Commands

container_commands:
      01_install_packages:
        command: 'yum install -y python35 python35-pip' 

In the case of container_commands, the log did show python 35 successfully installing. However, our code (in a follow-up command) then failed in a way that is only possible if python version 3.4 or lower is sourced. I am assuming that although 3.5 installed the environment did not switch to it as the source and continued to use the 3.4 distro.

We cannot manually modify the environment, because we need our configuration to be ready for auto-scaling. Thus, the solution must come from some configuration in .ebextensions. Any help here would be very appreciated.

UPDATE

I received an email from AWS support informing me that, because the operating system and application python environments are tightly coupled (ie the same), it is not possible to change the default python version for the application to python 3.5. They recommended I create a docker image. I've started looking into how to do that. If I come up with a solution I will post here.

Olfactory answered 23/1, 2017 at 20:4 Comment(0)
O
5

The way I ended up solving this was to create a python based docker container, and switch to using the elastic beanstalk docker configuration. I'm including the script below to help folks out. Note that it doesn't contain uwsgi or supervisor, as it's just a first pass. You might want to add those depending on your situation.

Dockerfile

FROM python:3.5

ADD . /src

RUN apt-get update
RUN apt-get install -y postgresql postgresql-contrib libpq-dev python3-dev
RUN pip3 install -r /src/requirements.txt
EXPOSE  8080
RUN python3 --version
CMD ["python3", "/src/application.py", "-p 8080"]
Olfactory answered 27/1, 2017 at 19:43 Comment(0)
C
3

This solution works on 06.06.2017 with AMI image: ami-1871797e

In your project simply create file .ebextensions/00_python_version.config with contents:

packages:
  yum:
    postgresql94-devel: []
    postgresql95-devel: []
    libffi-devel: []
    python35: []
    python35-devel: []
    python35-libs: []
    mod24_wsgi-python35: []

files:
  "/temp/change_python.sh":
    mode: "000644"
    owner: root
    group: root
    content: |
      rm -rf /opt/python/run/venv
      virtualenv -p /usr/bin/python35 /opt/python/run/venv
      rm -rf /opt/python/run/baselinenv
      ln -sf /opt/python/run/venv /opt/python/run/baselinenv

files:
  "/temp/change_python_2.sh":
    mode: "000644"
    owner: root
    group: root
    content: |
      ln -sf /opt/python/run/venv/lib64/python3.5 /opt/python/run/venv/lib64/python3.4
      ln -sf /opt/python/run/venv/lib/python3.5 /opt/python/run/venv/lib/python3.4

commands:
  00_aws_change_python:
    command: "sh /temp/change_python.sh"

container_commands:
  00_aws_change_python_2:
    command: "sh /temp/change_python_2.sh"
Cuneo answered 9/6, 2017 at 10:43 Comment(1)
Upvoted because it accurately answers the question, but, I really think the docker container is the better approach. Modifying ebextension configurations is kind of nightmarish because Amazon doesn't make it easy to test those on the fly. I think it is easier to use as little ebextension foo as possible and to instead employ configuration in a dockerfile.Olfactory

© 2022 - 2024 — McMap. All rights reserved.