0

I have run into a problem when trying to setup an AWS Codepipline. My ApplicationStart script makes a call to start the express server listening on port 60900, but because the express.listen() holds the command line up while it listens the ApplicationStart script times out and my deployment fails.

I've tried moving it to a background process with an & at the end of the command that starts the server, but I'm still getting the error at the ApplicationStart hook.

When I run the my start_server.sh script manually it almost instantly starts the server up and give me back control of the command line.

appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/mbmbam.app/
hooks:
  BeforeInstall:
    - location: scripts/stop_server.sh
      timeout: 300
      runas: root
    - location: scripts/remove_previous.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/change_permissions.sh
      timeout: 300
      runas: root
    - location: scripts/install_app.sh
      timeout: 300
      runas: root
    - location: scripts/install_db.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/start_server.sh
      timeout: 300
      runas: ubuntu

scripts/start_server.sh

#!/usr/bin/env bash

NODE_ENV=production npm start --prefix /var/www/mbmbam.app/app

Script assigned to the npm start command

app/start_app.sh

#!/bin/sh

if [ "$NODE_ENV" = "production" ]; then
  node server.js & 
else
  nodemon --ignore './sessions' server.js;
fi

AWS Codedeploy error

LifecycleEvent - ApplicationStart
Script - scripts/start_server.sh
[stdout]
[stdout]> [email protected] start /var/www/mbmbam.app/app
[stdout]> ./start_app.sh
[stdout]

Any help would be appreciated. I've been stuck on this for a day or so.

2 Answers 2

1

I solved it by changing the start_app.sh to

#!/bin/sh

if [ "$NODE_ENV" = "production" ]; then
  node server.js > app.out.log 2> app.err.log < /dev/null & 
else
  nodemon --ignore './sessions' server.js;
fi

Looks like it AWS even listed it in their troubleshooting steps here: https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-long-running-processes

Sign up to request clarification or add additional context in comments.

Comments

0

The issue seems to be node not going cleanly in the backgroud.

Can you try the following way to start node server in 'app/start_app.sh':

$ nohup node server.js > /dev/null 2>&1 &

Also I would suggest to look at making your node process a service so it is started if the server is rebooted:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.