Elastic Beanstalk with PostgreSQL

JunePyo Suh·2020년 9월 10일
0

Common errors while deploying

When updating your elastic beanstalk with PostgreSQL RDS, you may encounter the following error:

ERROR: Your requirements.txt is invalid. Snapshot your logs for details

There is a known issue with psycopg2 on AWS, and that is the likely cause of the error.

Installing packages

First install some packages so that pip install command completes successfully. In .ebextensions/01_packages.config:

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

yum is used to install the packages we need, because EC2 instances run on Amazon Linux, which is a Redhat flavor. In the above, we will be installing two packages - git and the Postgres client.

Configuring python environment

In /.ebextensions/02_python.config:

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "iotd.settings"
    "PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: iotd/iotd/wsgi.py
    NumProcesses: 3
    NumThreads: 20
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "www/static/"

Handle database migrations

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
    leader_only: true

Apache config

If using Apache with Beanstalk, you could set up Apache to enable gzip compression so files are downloaded faster by the clients. This can be done with container_commands. In .ebextensions/03_appache.config,

container_commands:
  01_setup_apache:
    command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf"

Then in .ebextensions/enable_mod_deflate.conf,

# mod_deflate configuration
<IfModule mod_deflate.c>
  # Restrict compression to these MIME types
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xml+rss
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/css
  # Level of compression (Highest 9 - Lowest 1)
  DeflateCompressionLevel 9
  # Netscape 4.x has some problems.
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  # Netscape 4.06-4.08 have some more problems
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  # MSIE masquerades as Netscape, but it is fine
  BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
<IfModule mod_headers.c>
  # Make sure proxies don't deliver the wrong content
  Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>

Must-know directories in EBS EC2

  • /opt/python : root of where your application ends up
  • /opt/python/current/app : the current application that is hosted in the environment
  • /opt/python/on-deck/app : the app is initially put in on-deck, and then after all the deployment is complete, it will be moved to current. If you are getting failures in your container_commands, check out the on-deck folder and not the current folder.
  • /opt/python/current/env : all the env variables that eb will set up for you. If you are trying to reproduce an error, you may first need to source /opt/python/current/env to get things set up as they would be when eb deploy is running.
  • /opt/python/run/venv : the virtual env used by your application; also need to run source /opt/python/run/venv/bin/activate if you are trying to reproduce an error

More can be found about using postgreSQL with EBS here.

playing with eb ssh

  • sudo find / -name appName : will show working directory of the uploaded app
  • to activate venv and do django operations,
    - source /opt/python/run/venv/bin/activate
    • source /opt/python/current/env

Stackoverflow help on eb ssh

0개의 댓글