When trying to run a script on AfterInstall
event I receive:
LifecycleEvent - AfterInstall
Script - deploy/install_dependencies.sh
[stdout]Install dependencies. /
[stderr]/usr/bin/env: node: No such file or directory
, which is strange because:
which node
yields:~/.nvm/versions/node/v15.2.0/bin/node
which npm
yields:~/.nvm/versions/node/v15.2.0/bin/npm
The appspec.yml wasn't setup by me, but I think it's straightforward:
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/deploy/
permissions:
- object: /
pattern: "**"
owner: ec2-user
hooks:
AfterInstall:
- location: deploy/install_dependencies.sh
- location: deploy/build.sh
- location: deploy/install_prd_dependencies.sh
- location: deploy/copy_overwrite.sh
# - location: deploy/migrations.sh
timeout: 300
runas: ec2-user
ApplicationStart:
- location: deploy/start_server.sh
timeout: 300
runas: ec2-user
I added runas: ec2-user
and the permissions
key with its values after reading this: Start Node Application in AfterInstall Hook
, though not the same issue, it seemed that would solve my problem, but it didn't.
I checked if nvm
was installed issuing which nvm
, it didn't work:
[ec2-user@my-ip ~]$ which nvm
/usr/bin/which: no nvm in (/home/ec2-user/.nvm/versions/node/v15.2.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin)
but, issuing only nvm
I recevie nvm
's CLI help commands without any problem.
I checked the .bashrc
of ec2-user
to see if nvm was being exported correctly (as suggested in this answer):
# User specific aliases and functions
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
All scripts are using absolute path for npm/node binaries, examples:
install_dependencies.sh
#!/bin/bash
echo "Install dependencies. $(pwd)"
cd /home/ec2-user/deploy/
/home/ec2-user/.nvm/versions/node/v15.2.0/bin/npm install
start_server.sh
#!/bin/bash
echo "Restarting servers..."
/home/ec2-user/.nvm/versions/node/v15.2.0/bin/pm2 restart 0
Is codedpeloy using root
user to issue these commands, even with runas
setup for ec2-user
? I don't understand what's happening, it worked before.