├── scripts
├── application_start.sh
└── after_install.sh
├── appspec.yml
├── .github
└── workflows
│ └── deploy.yml
├── app.js
├── package.json
├── .gitignore
└── README.md
/scripts/application_start.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | echo 'run application_start.sh: ' >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
4 |
5 | echo 'pm2 restart nodejs-express-app' >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
6 | pm2 restart nodejs-express-app >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
--------------------------------------------------------------------------------
/scripts/after_install.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | echo 'run after_install.sh: ' >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
3 |
4 | echo 'cd /home/ec2-user/nodejs-server-cicd' >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
5 | cd /home/ec2-user/nodejs-aws-codedeploy-pipeline >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
6 |
7 | echo 'npm install' >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
8 | npm install >> /home/ec2-user/nodejs-aws-codedeploy-pipeline/deploy.log
9 |
--------------------------------------------------------------------------------
/appspec.yml:
--------------------------------------------------------------------------------
1 | version: 0.0
2 | os: linux
3 | files:
4 | - source: /
5 | destination: /home/ec2-user/nodejs-aws-codedeploy-pipeline
6 | hooks:
7 | # ApplicationStop:
8 | # DownloadBundle:
9 | # BeforeInstall:
10 | # - location: scripts/before_install.sh
11 | # timeout: 300
12 | # runas: root
13 | # Install:
14 | AfterInstall:
15 | - location: scripts/after_install.sh
16 | timeout: 300
17 | runas: root
18 | ApplicationStart:
19 | - location: scripts/application_start.sh
20 | timeout: 300
21 | runas: root
22 | # ValidateService:
23 |
--------------------------------------------------------------------------------
/.github/workflows/deploy.yml:
--------------------------------------------------------------------------------
1 | name: CI/CD Pipeline
2 | on:
3 | push:
4 | branches: [ main ]
5 | jobs:
6 | deploy:
7 | runs-on: ubuntu-latest
8 | steps:
9 | # Step 1
10 | - name: Checkout to repo
11 | uses: actions/checkout@v2
12 |
13 | # Step 2
14 | - name: Set AWS credentials
15 | uses: aws-actions/configure-aws-credentials@v1
16 | with:
17 | aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
18 | aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
19 | aws-region: us-east-1
20 |
21 | # Step 3
22 | - name: Create CodeDeploy Deployment
23 | id: deploy
24 | run: |
25 | aws deploy create-deployment \
26 | --application-name nodejs-express-app \
27 | --deployment-group-name nodejs-express-app-cd1 \
28 | --deployment-config-name CodeDeployDefault.OneAtATime \
29 | --github-location repository=${{ github.repository }},commitId=${{ github.sha }}
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const app = express();
3 | const hostname = '127.0.0.1'; // Your server ip address
4 | const port = 3000;
5 |
6 | const version = '3,000,000';
7 |
8 | app.get('/', (req, res) => {
9 | // set response content
10 | res.send(`
11 |
12 | [Version ${version}]: THis is AMAZING!!! Like & Subscribe!
13 |
14 |

15 |
16 |
17 | `);
18 |
19 | console.log(`[Version ${version}]: New request => http://${hostname}:${port}`+req.url);
20 |
21 | })
22 |
23 | app.listen(port, () => {
24 | console.log(`[Version ${version}]: Server running at http://${hostname}:${port}/`);
25 | })
26 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nodejs-aws-codedeploy-pipeline",
3 | "version": "1.0.0",
4 | "description": "Deploy nodejs app to aws ec2 ubuntu server with aws codedeploy and aws pipeline and Github action - CI / CD tutorial",
5 | "main": "app.js",
6 | "scripts": {
7 | "test": "test",
8 | "start": "node app.js > app.log 2> error.log &"
9 | },
10 | "repository": {
11 | "type": "git",
12 | "url": "git+https://github.com/saasscaleup/nodejs-aws-codedeploy-pipeline.git"
13 | },
14 | "keywords": [
15 | "aws",
16 | "aws",
17 | "codedeploy",
18 | "aws",
19 | "pipeline",
20 | "cicd",
21 | "ci",
22 | "cd",
23 | "ci-cd",
24 | "deploy",
25 | "devops",
26 | "nodejs",
27 | "express.js"
28 | ],
29 | "author": "ScaleUp SaaS",
30 | "license": "ISC",
31 | "bugs": {
32 | "url": "https://github.com/saasscaleup/nodejs-aws-codedeploy-pipeline/issues"
33 | },
34 | "homepage": "https://github.com/saasscaleup/nodejs-aws-codedeploy-pipeline#readme",
35 | "dependencies": {
36 | "express": "^4.18.1"
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 |
9 | # Diagnostic reports (https://nodejs.org/api/report.html)
10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11 |
12 | # Runtime data
13 | pids
14 | *.pid
15 | *.seed
16 | *.pid.lock
17 |
18 | # Directory for instrumented libs generated by jscoverage/JSCover
19 | lib-cov
20 |
21 | # Coverage directory used by tools like istanbul
22 | coverage
23 | *.lcov
24 |
25 | # nyc test coverage
26 | .nyc_output
27 |
28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29 | .grunt
30 |
31 | # Bower dependency directory (https://bower.io/)
32 | bower_components
33 |
34 | # node-waf configuration
35 | .lock-wscript
36 |
37 | # Compiled binary addons (https://nodejs.org/api/addons.html)
38 | build/Release
39 |
40 | # Dependency directories
41 | node_modules/
42 | jspm_packages/
43 |
44 | # TypeScript v1 declaration files
45 | typings/
46 |
47 | # TypeScript cache
48 | *.tsbuildinfo
49 |
50 | # Optional npm cache directory
51 | .npm
52 |
53 | # Optional eslint cache
54 | .eslintcache
55 |
56 | # Microbundle cache
57 | .rpt2_cache/
58 | .rts2_cache_cjs/
59 | .rts2_cache_es/
60 | .rts2_cache_umd/
61 |
62 | # Optional REPL history
63 | .node_repl_history
64 |
65 | # Output of 'npm pack'
66 | *.tgz
67 |
68 | # Yarn Integrity file
69 | .yarn-integrity
70 |
71 | # dotenv environment variables file
72 | .env
73 | .env.test
74 |
75 | # parcel-bundler cache (https://parceljs.org/)
76 | .cache
77 |
78 | # Next.js build output
79 | .next
80 |
81 | # Nuxt.js build / generate output
82 | .nuxt
83 | dist
84 |
85 | # Gatsby files
86 | .cache/
87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js
88 | # https://nextjs.org/blog/next-9-1#public-directory-support
89 | # public
90 |
91 | # vuepress build output
92 | .vuepress/dist
93 |
94 | # Serverless directories
95 | .serverless/
96 |
97 | # FuseBox cache
98 | .fusebox/
99 |
100 | # DynamoDB Local files
101 | .dynamodb/
102 |
103 | # TernJS port file
104 | .tern-port
105 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nodejs-aws-codedeploy-pipeline
2 |
3 | How to set ci/cd for nodejs app with aws codeDeploy and aws codePipeline
4 |
5 |
6 |
7 | ## Installation instructions
8 |
9 | ### 1. Launch amazon linux server in aws
10 |
11 | ### 2. ssh to linux to install packages
12 |
13 | ```sh
14 | ssh -i ec2-user@ -v
15 | ```
16 |
17 | ### 3. Update and Upgrade linux machine and install node, nvm and pm2
18 |
19 | ```sh
20 | sudo yum update
21 | ```
22 |
23 | ```sh
24 | sudo yum upgrade
25 | ```
26 |
27 | ```sh
28 | sudo yum install -y git htop wget
29 | ```
30 |
31 | #### 3.1 install node
32 |
33 | To **install** or **update** nvm, you should run the [install script][2]. To do that, you may either download and run the script manually, or use the following cURL or Wget command:
34 | ```sh
35 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
36 | ```
37 | Or
38 | ```sh
39 | wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
40 | ```
41 |
42 | Running either of the above commands downloads a script and runs it. The script clones the nvm repository to `~/.nvm`, and attempts to add the source lines from the snippet below to the correct profile file (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`).
43 |
44 | #### 3.2 Copy & Past (each line separately)
45 |
46 | ```sh
47 | export NVM_DIR="$HOME/.nvm"
48 | [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
49 | [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
50 | ```
51 |
52 | #### 3.3 Verify that nvm has been installed
53 |
54 | ```sh
55 | nvm --version
56 | ```
57 |
58 | #### 3.4 Install node
59 |
60 | ```sh
61 | nvm install --lts # Latest stable node js server version
62 | ```
63 |
64 | #### 3.5 Check nodejs installed
65 | ```sh
66 | node --version
67 | ```
68 |
69 | #### 3.6 Check npm installed
70 | ```sh
71 | npm -v
72 | ```
73 |
74 | ### 4. Clone nodejs-aws-codedeploy-pipeline repository
75 |
76 | ```sh
77 | cd /home/ec2-user
78 | ```
79 |
80 | ```sh
81 | git clone https://github.com/saasscaleup/nodejs-aws-codedeploy-pipeline.git
82 | ```
83 |
84 | ### 5. Run node app.js (Make sure everything working)
85 |
86 | ```sh
87 | cd nodejs-aws-codedeploy-pipeline
88 | ```
89 |
90 | ```sh
91 | npm install
92 | ```
93 |
94 | ```sh
95 | node app.js
96 | ```
97 |
98 | ### 6. Install pm2
99 | ```sh
100 | npm install -g pm2 # may require sudo
101 | ```
102 |
103 | ### 7. Set node, pm2 and npm available to root
104 |
105 | ```sh
106 | sudo ln -s "$(which node)" /sbin/node
107 | ```
108 | ```sh
109 | sudo ln -s "$(which npm)" /sbin/npm
110 | ```
111 | ```sh
112 | sudo ln -s "$(which pm2)" /sbin/pm2
113 | ```
114 |
115 | ### 8 Starting the app as sudo (Run nodejs in background and when server restart)
116 | ```sh
117 | sudo pm2 start app.js --name=nodejs-express-app
118 | ```
119 | ```sh
120 | sudo pm2 save # saves the running processes
121 | # if not saved, pm2 will forget
122 | # the running apps on next boot
123 | ```
124 |
125 | #### 8.1 IMPORTANT: If you want pm2 to start on system boot
126 | ```sh
127 | sudo pm2 startup # starts pm2 on computer boot
128 | ```
129 |
130 | ### 9. Install aws code deploy agent
131 | ```sh
132 | sudo yum install -y ruby
133 | ```
134 |
135 | ```sh
136 | wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
137 | ```
138 |
139 | ```sh
140 | chmod +x ./install
141 | ```
142 | ```sh
143 | sudo ./install auto
144 | ```
145 | ```sh
146 | sudo service codedeploy-agent start
147 | ```
148 |
149 | ### 10. Continue in AWS console...
150 |
151 | Watch the rest of the youtube video...
152 |
153 |
154 | ## Support 🙏😃
155 |
156 | If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you:
157 |
158 | 1. Subscribe to My youtube channel and leave a comment: http://www.youtube.com/@ScaleUpSaaS?sub_confirmation=1
159 | 2. Buy me A coffee ❤️ : https://www.buymeacoffee.com/scaleupsaas
160 |
161 | Thanks for your support :)
162 |
163 |
164 |
--------------------------------------------------------------------------------