Deploying React on Rails to Elastic Beanstalk

In order to deploy a React on Rails app to elastic beanstalk, you must install yarn on each instance. If yarn is not installed, asset compilation will fail on the elastic beanstalk instance.

You can install yarn by adding a 0x_install_yarn.config file to your .ebextensions folder which contains these commands.

  "/opt/elasticbeanstalk/hooks/appdeploy/pre/" :
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -xe

      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
      EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)

      . $EB_SUPPORT_DIR/envvars
      . $EB_SCRIPT_DIR/

      # Install nodejs
      echo "install nodejs"
      curl --silent --location | sudo bash -
      yum -y install nodejs
      echo "install yarn"
      # install yarn
      wget -O /etc/yum.repos.d/yarn.repo;
      yum -y install yarn;

      # yarn install

      # mkdir /home/webapp
      mkdir -p /home/webapp
      chown webapp:webapp /home/webapp
      chmod 700 /home/webapp

This script installs yarn and all node.js dependencies before the rails do assets:precompile. Also, it creates /home/webapp directory allowing the precompile task to create temp files.

Your app can be deployed to elastic beanstalk successfully. However, the react app javascript files are under public/packs. If you are using nginx, you need to let it know the location of https://yourhost/packs.

In your proxy.conf setting, please add the following code.

  location /packs {
    root /var/app/current/public;

You can find an example code here:

results matching ""

    No results matching ""