NodeJS — EC2 — NGINX — Codedeploy

Sebastian Velez Zuluaga
3 min readSep 28, 2021

In this topic I would like to recreate the process to update our production code from github on EC2 instance, LINUX UBUNTU 20.04

INSTANCE EC2 — NODE — NGINX

  1. We Need to login in aws console.
  2. Crete a free tier instance. LINUX UBUNTU 20.04
    the default user is ubuntu, and AWS recommend use the key pair to connect your instance
  3. In your instance Install:

Install node V12
sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Install V16 node

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04

4. Install git, and clone your.

5. Intall GNIX and PM2.

Importan in nginx config.

Let’s edit the default file which exists in /etc/nginx/sites-enabled/default

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

6. Deploy you code on your instance, please be sure that your code is workin.

Remember the security group open ports, 80 for nginx and 3000 for express, and test the nginx is runing.

7. Set the env varibales and saver in profile.

https://github.com/Sanjeev-Thiyagarajan/PERN-STACK-DEPLOYMENT

set -o allexport; source /home/ubuntu/.env; set +o allexport

https://www.youtube.com/watch?v=NjYsXuSBZ5U

Config CODEDEPLOY

  1. Install CodeAgent for Ubuntu 20.04
apt-get update
apt-get install -y ruby
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/releases/codedeploy-agent_1.0-1.1597_all.deb
mkdir codedeploy-agent_1.0-1.1597_ubuntu20
dpkg-deb -R codedeploy-agent_1.0-1.1597_all.deb codedeploy-agent_1.0-1.1597_ubuntu20
sed 's/2.0/2.7/' -i ./codedeploy-agent_1.0-1.1597_ubuntu20/DEBIAN/control
dpkg-deb -b codedeploy-agent_1.0-1.1597_ubuntu20
dpkg -i codedeploy-agent_1.0-1.1597_ubuntu20.deb
systemctl start codedeploy-agent
systemctl enable codedeploy-agent

https://gist.github.com/amanjuman/8bccd5ee15f23543e400942f0e18abc2

2. Create two roles on AWS

Codeploy role

EC2

Update Trust Policy and Add the EC2 Rol, tag to you instance.

3. Create an Application

4. Create Group.

5. Create pipeline

On opt/codedeploy-agent/deployment-root/deployment-logs you can see the step by step of the YML or /var/log/aws$ cd codedeploy-agent/ aws you can see the log from the pipeline

Remember upload the YML file, and script hook on the repo.

Happy coding and testing. Regards

--

--