Search code examples
amazon-web-servicesamazon-ec2aws-code-deploy

Node JS app is crashing on AWS EC2 instance


I have deployed app on ec2 using codedeploy service but app is crashing and showing below error message:

Script at specified location: scripts/start_server failed to complete in 100 seconds

Below is my appspec.yml file:

version: 0.0
os: linux
files:
  - source: /
    destination: /tmp/
hooks:
  AfterInstall:
     - location: scripts/install_dependencies
       timeout: 100
       runas: root
  ApplicationStart:
     - location: scripts/start_server
       timeout: 100
       runas: root

install_dependencies

cd /tmp/

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node

npm install

start_server

cd /tmp/

node server.js

Someone let me know what I am doing wrong.


Solution

  • Your deployment hangs because of this node server.js. This will start your app and that's it. It will never finish, so CodeDeploy timeouts waiting for that to finish.

    One way to overcome the problem is to start your app through systemd. Thus you have to create a systemd unit file. There are many examples for that:

    Other ways could involve running your app using pm2 or forever.