Troubleshooting Deploying a Django Application With Elastic Beanstalk
Overview
An ongoing list of issues, with solutions as they are found, that I have encountered while deploying a Django web application using Elastic Beanstalk.
Errors installing awsebcli on Mac OS for Python 2.7
Use case
$pip install awsebcli
[...]
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 209, in main
status = self.run(options, args)
[...]
Cause
OS X El Capitan ships with six 1.4.1 already installed. awscli < botocore < python-dateutil < six>=1.5. pip doesn't have permission to System Integrity Protection doesn't it to modify directories.[1]
In addition, I suspect I may have installed some components using sudo pip. I realize now that it's pretty much always inappropriate to use sudo with pip.
Fix
(Using sudo in cases like this may be bad practice, but at this point it was the only way to get it to work with Python 2.7.)[2]
$ sudo pip install --ignore-installed awsebcli
"No environment found" error with aws eb cli
Use case
Almost all eb commands result in an error the effect of No Environment found for EnvironmentName = 'name-ev'.
Cause
An EB environment was created, but then not deleted entirely.
Fix
Delete the environment through the AWS console, and then delete the .elasticbeanstalk/ and .ebextensions/ directories.
Zip file errors while installing a custom Django module
Use case
pip install path/to/my-custom-package.0.1.zip results in error: BadZipFile: File is not a zip file.
Cause
This happened after using wget to retrieve a .zip archive off GitHub.
Despite the .zip extension, the file type can register as HTML on Linux.
Fix
Test the file type:
$ file myzipfile.zip myzipfile.zip: HTML document, UTF-8 Unicode txt, with very long lines
Upload the zip file to the server using scp:
`scp -i path/to/namekey_pair.pem path/to/source.zip ec2-user@public_dns.us-west-2.compute.amazonaws.com:path/to/dest.zip`
(Where namekey_pair.pem is the actual name of a .pem file, and public_dns is the actual address of the public dns from the EC2 console.)
eb deploy errors where EB environment doesn't match
Use case
Attempting to update .ebextensions/01-packages.config to list Git and PostgreSQL as packages to install, then running eb deploy to install the packages, the deploy process quits with errors to the effect that the YAML is malformed in the .config file. Editing the file makes it evident that the errors are originating in another environment.
Cause
Logged into wrong EC2 instance.
You can log into an EC2 instance and run eb init and eb create commands to create new Elastic Beanstalk applications and environments. The new app will spawn a new EC2 instance, that is separate from the current EC2 instance.
Fix
It's necessary to ssh to the correct EC2 instance to run eb deploy. Go back to the AWS EC2 console, find the EC2 instance corresponding to the new EB environment, and ssh to its public DNS.
YAML formatting errors on eb create
Use case
This is related to the previous troubleshooting case.
Run eb create and receive error
ERROR: The configuration file .ebextensions/02-northrose.config in application version app-dd48-160209_151318 contains invalid YAML or JSON. YAML exception: while scanning for the next token
found character '\t' that cannot start any token
in "<reader>", line 2, column 1:
"aws:elasticbeanstalk:applicati ...
^
, JSON exception: Unexpected character (o) at position 0.. Update the configuration file.
Cause
Not 100% certain about the cause. It's either that changes to the .config file needed to be checked in in order to be deployed to the EC2 instance. Or that I had tried recently to launch an environment with the same name, and so the config file in question was still seen in its old state in the previous environment.
Fix
1) Fixed the YAML formatting in the file. Committed the changes, and merged them into the master branch.
2) Took some time making the fixes, which possibly gave the AWS Management Console enough time to clear out the previous aborted Elastic Beanstalk environments that shared the environment name.
manage.py migrate fails during eb deploy
Use case
Execute eb deploy from the command line. No errors reported on the command line (there are probably errors in the logs) but migrate obviously fails and database objects are not available.
Cause
Several blog posts related to AWS Django deployment document the following container_command (in .ebextensions/): [3]
command: "source /opt/python/run/venv/bin activate && python /opt/python/current/app/migrate.py migrate --noinput"
The problem with that command is the the RDS_DB_NAME system environment variable is not available as it is when running the Django application. This causes the process to fall back on local database connection properties which are invalid on the AWS server.
Fix
Use django-admin instead:
<syntaxhighlight lang="yaml">
01_migrate:
command: "django-admin migrate"
leader_only: true
</syntaxhighligh>
Notes
- ↑ Six issue when installing package - pypa / pip GitHub Issues
- ↑ “OSError: [Errno 1 Operation not permitted” when installing Scrapy in OSX 10.11 (El Capitan) (System Integrity Protection)] - Stack Overflow
- ↑ [] - Real Python