Home > Net >  Django elastic beanstalk CLI deployment failure after adding .env files to gitignore
Django elastic beanstalk CLI deployment failure after adding .env files to gitignore

Time:11-27

I'm new to Django/EB/Git and been working on a django project and have successfully separated my setttings and separated .env files for both development and production which all works as expected and deployed- see the following project structure:

Project Structure

project root
    myapp
        settings
            __init__
            base.py
            dev.py
            prod.py
.env.dev
.env.prod
.gitignore
manage.py
requiremnts.txt

However the moment I add my my .env files to the .gitignore file I now received the following error with deployment within eb logs (cfn-init-cmd.log):

.gitignore

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
.env.dev
.env.prod

Error: eb logs (cfn-init-cmd.log) FileNotFoundError: [Errno 2] No such file or directory: '.env.prod'

If i remove .env.prod from the .gitignore file then my project deploys successfully.

Moreoever, I read online that might be due to me git adding and comitting the .env.prod file to the repo however believe I have also excluded git add/commit when I started fresh and re-created the git repo with the following command (commands run on local project):

git add --all -- :!.env.dev :!.env.prod
git commit -m "Initial commit"

Followed by:

eb deploy myproject-env

See my .ebextensions config file as follows:

.ebextensions/django.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: "myproject.settings.prod"
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    "/static": "static/"
packages:
  yum:
    python3-devel: []
    mariadb-devel: []
container_commands:
  01_collectstatic:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py collectstatic --noinput"
  02_migrate:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate --noinput"
    leader_only: true

I was not sure if I'm supposed to add any git commands to my .ebextensions config but assumed it's just to be done on local git repo and then push to github, I have also tried to deploy with and without codecommit but made no difference to the above.

I have spent a about a week figuring this all out and finally being able to deploy and this I believe was supposed to be the very last step adding .env files to .gitignore file, I'm just not sure what I'm missing or have done something I correctly with the git repo.

I'd greatly appreciate any help/guidance on this, Thanks in advance.

CodePudding user response:

Elastic beanstalk use .gitignore file if .ebignore file does not exist. So you can use both for your file management.

AWS doc says:

You can tell the EB CLI to ignore certain files in your project directory by adding the file .ebignore to the directory. This file works like a .gitignore file.

...

If .ebignore isn't present, but .gitignore is, the EB CLI ignores files specified in .gitignore. If .ebignore is present, the EB CLI doesn't read .gitignore.

When .ebignore is present, the EB CLI doesn't use git commands to create your source bundle. This means that EB CLI ignores files specified in .ebignore, and includes all other files. In particular, it includes uncommitted source files.

  • Related