4

I am trying to get a very simple nodejs app to go though my setup (BitBucket to AWS), and I can get the default example to work:

https://github.com/awslabs/aws-codedeploy-samples/tree/master/applications/SampleApp_Linux

But that example is in apache, httpd, so when I try to change the appspec.yml for nodejs, the setup brakes. This is my appspec.yml:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/app
hooks:
  BeforeInstall:
    - location: scripts/install_dependencies
      timeout: 300
      runas: root
    - location: scripts/start_server
      timeout: 300
      runas: root

install_dependencies:

#!/bin/bash
yum install -y nodejs npm
npm install

start_server:

#!/bin/bash
node server.js
  • Can you give us more specifics as to how it breaks? Are you getting error messages anywhere, etc? – Chris Thompson Oct 03 '16 at 18:51
  • Actually, without any more info my guess is that your `start_server` script is causing a "timeout" because `node server.js` starts the application in the foreground, unlike the example that starts the Linux service in the background. `node server.js` will never return until the process is terminated – Chris Thompson Oct 03 '16 at 18:54
  • The deployment says that the last recet event was beforeInstall. I just found that it also says this: (): mapping values are not allowed in this context at line 8 column 11. You said that start_server will never return--does that mean that I should put it in ApplicationStart? – LearnMoreCoding.com Oct 03 '16 at 19:02
  • @LearnMoreCoding.com need help if you solve your problem – Adiii Jun 10 '17 at 20:36

2 Answers2

2

I figured it out from the ec2 event logs and other places. Here is what I have:

appspec.yml

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:

#!/bin/bash
cd /tmp/

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum install -y gcc-c++ make
yum install -y nodejs npm

npm install -g pm2
npm install

For start_server, stop and delete httpd. Then start pm2. Then problem with "node server.js" is that it never resolves.

start_server:

#!/bin/bash
cd /tmp/

isExistApp = `pgrep httpd`
if [[ -n  $isExistApp ]]; then
    service httpd stop
fi

yum remove -y httpd

pm2 delete all
pm2 start server.js
  • I know this is kind of an old thread, but did you ever get it resolving? I am having similar problems except with Ruby and the Sinatra framework. From what I can tell in the logs, with my appspec.yml, it is getting everything up and running, but it times out because codedeploy does not realize that what it has just done is success. So it is successfully up and running for however long before the timeout hits. I am having trouble figuring out how to get CodeDeploy to recognize success. – Mike Aug 27 '17 at 20:45
1

For ubuntu/debian based distros, here and here I found a couple of gists that really helped me for getting this together.

appspec.yml:

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
  ValidateService:
    - location: scripts/test
      timeout: 100
      runas: root

install_dependencies:

#!/bin/bash
cd /tmp/

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

npm install -g pm2
npm install

start_server:

#!/bin/bash
cd /tmp/
# set any env variables
export NODE_ENV=staging

pm2 delete all
pm2 start dist/server/index.js --name MyAPI

test:

#!/bin/bash
sleep 10
nc -zv 127.0.0.1 80
SebaGra
  • 2,801
  • 2
  • 33
  • 43