Skip to content

Instantly share code, notes, and snippets.

@pdib
Last active April 5, 2022 07:39
Show Gist options
  • Save pdib/e319bdb353cc22c00024 to your computer and use it in GitHub Desktop.
Save pdib/e319bdb353cc22c00024 to your computer and use it in GitHub Desktop.
Installing node and npm on a Django AWS ElasticBeanstalk
# This specifies the deployment process on AWS ElasticBeanstalk for a Django app using npm and Postgres.
#
# The target environment should have access to a Postgres Database through environment variables.
# The environment can be setup using `eb create --database.engine=postgres`
# The necessary environment variables to access the database will be automatically defined on the
# instances of that environment.
#
# In addition, the target environment should define environment variables (django secret key ...).
# They can be manually defined using the AWS ElasticBeanstalk interface on the web.
# They can also be specified when creating the environment from command line, for example:
# ```
# eb create --database.engine=postgres --envvars ENVVAR1=value,ENVVAR2=value
# ```
#
packages:
yum:
git: []
postgresql93-devel: []
commands:
01_node_install:
cwd: /tmp
test: '[ ! -f /usr/bin/node ] && echo "node not installed"'
command: 'yum install -y nodejs --enablerepo=epel'
02_npm_install:
cwd: /tmp
test: '[ ! -f /usr/bin/npm ] && echo "npm not installed"'
command: 'curl -L http://npmjs.org/install.sh | sh'
03_node_update:
cwd: /tmp
test: '[ ! -f /usr/bin/n ] && echo "node not updated"'
command: 'npm install -g n && n stable'
option_settings:
"aws:elasticbeanstalk:application:environment":
# your settings module here
DJANGO_SETTINGS_MODULE: "server.settings_prod"
# add the path to the root of your django app
# note that this is the path on the target machine
# EB will deploy your application in /opt/python/current on the target machines
PYTHONPATH: "/opt/python/current/app/server:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
# path to your wsgi.py file from the root folder of your application
WSGIPath: server/server/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
# These commands will be run just before the application is started
container_commands:
01_migrate:
command: 'python server/manage.py migrate --noinput'
leader_only: true
# You can create a "createadmin" command to create a super user automatically
# You can use environment variables to define the credentials of the super user
# If you don't use a createadmin command, delete this command.
02_createadmin:
command: 'python server/manage.py createadmin'
leader_only: true
# You can define a build script in packages.json (using gulp, grunt...) to build your client side files
03_npm_build:
command: 'npm install && npm run build'
04_collectstatic:
command: 'python server/manage.py collectstatic --noinput'
@DleiaDev
Copy link

DleiaDev commented Jan 28, 2022

@systemTetra2 As @HCNick pointed out, Node isn't available anymore through EPEL repo. What worked for me is, I created a post deploy hook (bash script) in .platform/hooks/postdeploy and installed Node through nodesource.com:

# Install NPM (if it doesn't exist)
if ! command -v npm &> /dev/null
then
  curl -fsSL https://rpm.nodesource.com/setup_14.x | sudo bash -
  sudo yum install nodejs -y
fi

# Install dependencies
npm install

# Compile assets
npm run build

This is for Node 14, I haven't tested it for latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment