3

I'm trying to run a Python script I've uploaded as part of my AWS Elastic Beanstalk application from my development machine, but can't figure out how to. I believe I've located the script correctly, but when I attempt to run it under SSH, I get an import error.

For example, I have a Flask-Migrate migration script as part of my application (pretty much the same as the example in the documentation), but after successfully SSHing to my EB instance with

> eb ssh

and locating the script with

$ sudo find / -name migrate.py

when I run in the directory (/opt/python/current) where I located it with

$ python migrate.py db upgrade

at the SSH prompt I get

Traceback (most recent call last):
  File "db_migrate.py", line 15, in <module>
    from flask.ext.script import Manager
ImportError: No module named flask.ext.script

even though my requirements.txt (present along with the rest of my files in the same directory) has flask-script==2.0.5.

On Heroku I can accomplish all of this in two steps with

> heroku run bash
$ python migrate.py db upgrade

Is there equivalent functionality on AWS? How do I run a Python script that is part of an application I uploaded in an AWS SSH session? Perhaps I'm missing a step to set up the environment in which the code runs?

Community
  • 1
  • 1
orome
  • 45,163
  • 57
  • 202
  • 418

1 Answers1

5

To migrate your database the best is to use container_commands, they are commands that will run every time you deploy your application. There is a good example in the EBS documentation (Step 6) :

container_commands:
  01_syncdb:    
    command: "django-admin.py syncdb --noinput"
    leader_only: true

The reason why you're getting an ImportError is because EBS installs your packages in a virtualenv. Before running arbitrary scripts in your application in SSH, first change to the directory containing your (latest) code with

cd /opt/python/current

and then activate the virtualenv

source /opt/python/run/venv/bin/activate

and set the environment variables (that your script probably expects)

source /opt/python/current/env
orome
  • 45,163
  • 57
  • 202
  • 418
volent
  • 473
  • 6
  • 16
  • 1
    Ah, a virtualenv; that's probably it! – orome Jan 16 '15 at 17:00
  • 2
    To add to this, you should never edit your instance directly with ssh, because EB could swap out your instances at anytime, and any changes made directly on the box will not persist. – Nick Humrich Jan 16 '15 at 17:50
  • @NickHumrich: Do `container_commands` run automatically each time I deploy? What about running things manually, at other times (i.e., the equivalent of the Heroku example above: running an arbitrary script *with all the context of that my app has*)? – orome Jan 16 '15 at 18:03
  • container_commands will run every-time the container is started. This includes deploys, new instances, etc. You should avoid running things manually, as it is not scalable. If you have tasks you need to run, you should write these into your webapp, or use a worker environment. – Nick Humrich Jan 16 '15 at 18:24
  • @NickHumrich: Not clear to me from the docs (it's probably just me) but will any migrations I've just deployed with the deployment that triggers the running of `container_commands` be available to the commands? For example, if I've run `python db_migrate.py db migrate` locally (generating a migration) and then deploy (including the new migration) will the container command python migrate.py db upgrade` use have access to the just generated (and uploaded) migration? Put another way: does the container command use the just uploaded files (or does it run *before* they are averrable)? – orome Jan 16 '15 at 21:10
  • 1
    @raxacoricofallapatorius the ebextensions (container_commands is a form of ebextension) are packaged WITH your code, so yes, it will use the new code/files when it runs. – Nick Humrich Jan 16 '15 at 21:40
  • @NickHumrich: Thanks, yes I see that this works; very nice (now if I could figure out [how to get Git to automatically add](http://stackoverflow.com/q/27993246/656912) the migration files that `... migrate` generates). – orome Jan 16 '15 at 21:44