Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying a Rails / Ember app on AWS Elastic Beanstalk

I am having issues deploying my elastic beanstalk rails 4 + ember cli app. I have a rails application and within the root I have a folder called 'frontend' which contains my ember app generated by ember CLI Rails.

My configuration: 64bit Amazon Linux 2015.03 v1.3.1 running Ruby 2.1 (Puma)

I encounter the following error from my activity log after I run eb deploy:

At cursory, I get this

ERROR: Instance: i-25c139e7 Module: AWSEBAutoScalingGroup ConfigSet: null Command failed on instance. Return code: 1 Output: (TRUNCATED)...mber-cli-rails.rb:58:in `compile!'

Looking into /var/log/eb-activity.log

I first get a lot of npm ERR! Error: Attempt to unlock X, which hasn't been locked

followed by npm ERR! System Linux 3.14.35-28.38.amzn1.x86_64 npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" npm ERR! cwd /var/app/ondeck/frontend npm ERR! node -v v0.10.35 npm ERR! npm -v 1.4.28 npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /var/app/ondeck/frontend/npm-debug.log npm ERR! not ok code 0 rake aborted! EmberCLI Rails requires your Ember app to have an addon.

From within your EmberCLI directory please run:

$ npm install --save-dev [email protected]

in you Ember application root: /var/app/ondeck/frontend

Tasks: TOP => assets:precompile => ember:compile
(See full trace by running task with --trace) (Executor::NonZeroExitStatus)

So I ssh into the directory indicated and run npm install, which also leaves me with a lot of errors regarding authorization. When I run with via sudo, the modules install correctly, but when I redeploy my app, it gives me the exact same error.

I have tried sudo NPM install and chown -R node_modules webapp so that the node_modules folder can be accessed by the webapp group with no success.

like image 574
Tim Hui Avatar asked Feb 10 '23 07:02

Tim Hui


1 Answers

I hate long answers, but this scenario is quite complicated.

As mentioned in the comments above, it was discovered that the home directory for the webapp user needed to be created (/home/webapp). Once this directory is created, the node package manager (npm) can execute without error. Because AWSEB environments can scale, SSH'ing into the EB host and performing one-off installations of packages and modules will not work in the long run. Essentially the answer boils down to the following logical steps:

  1. Install git on the application server because bower needs it.
  2. Create the home directory of the webapp user at /home/webapp.
  3. Install bower globally using npm.
  4. Invoke the npm install of your ember app.
  5. Invoke bower install for your ember app.

To fix this I went ahead and created several .ebextensions customization files that are executed during an eb deploy. Here they are in order:

  1. .ebextensions/00_option_settings.config - sets some EB options; for example the timeout length for command executions performed during an eb deploy. In this case all commands will timeout after 1200 seconds.

    option_settings:
      - namespace: 'aws:elasticbeanstalk:command'
        option_name: 'Timeout'
        value: '1200'
    
  2. .ebextensions/01_packages.config - can install packages through yum and make them available to your eb instance. In this case I use yum to install git, this will later be used by bower.

    packages:
      yum:
        git: []
    
  3. .ebextensions/02_commands.config - allows you to run OS commands prior to unpacking the application that was uploaded through eb deploy. This part of the answer satisfies the main theme of this question: In my particular case, I need to create the /home/webapp directory, make sure it is owned by the webapp user, and also has 700 permissions. Lastly, I ensure that bower is installed globally as it will be needed by my ember application.

    commands:
      01_mkdir_webapp_dir:
        # use the test directive to create the directory
        # if the mkdir command fails the rest of this directive is ignored
        test: 'mkdir /home/webapp'
        command: 'ls -la /home/webapp'
      02_chown_webapp_dir:
        command: 'chown webapp:webapp /home/webapp'
      03_chmod_webapp_dir:
        command: 'chmod 700 /home/webapp'
      04_install_bower_global:
        command: 'npm install -g bower'
    
  4. .ebextensions/03_container_commands.config - runs OS command after the application has been unpacked. NOTE: My ember app lives in the frontend directory of application source code. In order to install the npm and bower dependencies, the npm install and bower install commands need to be executed from the frontend directory. It is also worth mentioning that the bower-install command needs the --allow-root flag in order to succeed as the AWS user executing these commands has elevated privileges.

    container_commands:
      01_npm_install:
        # set the current working directory to fully-qualified frontend
        cwd: '/var/app/ondeck/frontend/'
        command: 'npm install'
        leader_only: 'false'
      02_bower_install:
        # set the current working directory to fully-qualified frontend
        cwd: '/var/app/ondeck/frontend/'
        command: 'bower --allow-root install'
        leader_only: 'false'
      03_seeddb:
        # seed my database (has nothing to do with this answer)
        command: 'rake db:seed_fu'
        leader_only: 'true'
    
like image 130
Dan Avatar answered Feb 16 '23 15:02

Dan