Skip to content

Instantly share code, notes, and snippets.

@moshest
Last active October 13, 2021 18:09
Show Gist options
  • Save moshest/c6abc2d2af943d2755c5 to your computer and use it in GitHub Desktop.
Save moshest/c6abc2d2af943d2755c5 to your computer and use it in GitHub Desktop.
Node.js Project on AWS CodeDeploy CentOS
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/node
permissions:
- object: /home/ec2-user
owner: ec2-user
group: ec2-user
type:
- directory
- file
hooks:
BeforeInstall:
- location: install.sh
timeout: 300
runas: root
AfterInstall:
- location: post_install.sh
timeout: 600
runas: ec2-user
ApplicationStart:
- location: run.sh
timeout: 120
runas: ec2-user
ApplicationStop:
- location: stop.sh
timeout: 120
runas: ec2-user
ValidateService:
- location: validate.sh
timeout: 60
runas: ec2-user
#!/usr/bin/env bash
set -e
# update instance
yum -y update
# install general libraries like Java or ImageMagick
yum -y install default-jre ImageMagick
# add nodejs to yum
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum -y install nodejs #default-jre ImageMagick
# install pm2 module globaly
npm install -g pm2
pm2 update
#!/usr/bin/env bash
set -e
cd ~/node
npm install
# setup NODE_ENV
if [ ! -z "$DEPLOYMENT_GROUP_NAME" ]; then
export NODE_ENV=$DEPLOYMENT_GROUP_NAME
hasEnv=`grep "export NODE_ENV" ~/.bash_profile | cat`
if [ -z "$hasEnv" ]; then
echo "export NODE_ENV=$DEPLOYMENT_GROUP_NAME" >> ~/.bash_profile
else
sed -i "/export NODE_ENV=\b/c\export NODE_ENV=$DEPLOYMENT_GROUP_NAME" ~/.bash_profile
fi
fi
# add node to startup
hasRc=`grep "su -l $USER" /etc/rc.d/rc.local | cat`
if [ -z "$hasRc" ]; then
sudo sh -c "echo 'su -l $USER -c \"cd ~/node;sh ./run.sh\"' >> /etc/rc.d/rc.local"
fi
#!/usr/bin/env bash
if [ ! -z "$DEPLOYMENT_GROUP_NAME" ]; then
export NODE_ENV=$DEPLOYMENT_GROUP_NAME
fi
cd ~/node
pm2 start bin/www -n www -i 0
#!/usr/bin/env bash
cd ~/node
pm2 stop www || true
#!/usr/bin/env bash
sleep 10
nc -zv 127.0.0.1 3000
@MacgyverMartins
Copy link

Nice Gist. Helped me a lot.
But I'm having an error in this line pm2 start bin/www -n www -i 0

This line tries to start pm2 in cluster mode, but it logs the error:
pm2 cluster ENOENT: no such file or directory, uv_cwd

But if I start pm2 in fork mode, it works

@mattdaisley
Copy link

Thanks for this!

@johnatdoshii
Copy link

Nice work, really helpful!

@djakaitis
Copy link

Thank you for this! Big help.

@Shamim-Aktar
Copy link

@moshe Simantov
Please provide me the step for this gist I mean to say. How I can run this all shell script

@krukru
Copy link

krukru commented Jul 22, 2019

Brilliant and simple! Exactly what I needed to get me started. Thanks

@wmhartl
Copy link

wmhartl commented Sep 20, 2020

Great example - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment