AWS code deploy npm install fail with EACCES: permission denied

Viewed 99

I'm installing a nodejs application on a EC2 using CodeDeploy. Following is appspec.yaml

version: 0.0
os: linux 
files:
  - source: /
    destination: /home/ubuntu/myapp
hooks:
  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: ubuntu
  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 300
      runas: ubuntu
  ApplicationStart:
    - location: scripts/application_start.sh
      timeout: 300
      runas: ubuntu

In after_install.sh npm install is run as below

#!/bin/bash
cd /home/ubuntu/my-app
npm install

Since files are copied as a ubuntu user I expected directories to have write access. But it seems like directories doesn't have write access

drwxr-xr-x 507 root   root    24576 Jun 19 14:55 node_modules

So sudo is needed when npm install is run. Is there a way around this?

1 Answers

#!/bin/bash #add npm and node to path

export NVM_DIR="$HOME/.nvm" 
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # loads nvm   
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # loads nvm bash_completion (node is in path now)

#give permission for everything in the project directory

sudo chmod -R 777 /home/<yourprojectdir>

#navigate into our working directory where we have all our github files

cd /home/<yourprojectdir>

#install node modules

echo "trying to install node modules"
npm install

#restrict permission for everything in project directory

sudo chmod -R 775 /home/<yourprojectdir>
Related