Copied to Clipboard
6. Deployment Process on AWS EC2
On EC2 server:
- Clone project first time:
- git clone
- cd project
- npm install
- pm2 start app.js --name node-app
After CI/CD setup:
Deployment becomes automatic on every push.
Practical Demonstration (Images Explained)
Step 1. BEFORE: Original Login Page
Step 2. Making Changes & Pushing Code
- This terminal screenshot shows the developer workflow.
- The code was reflected in the GitLab repository 6 minutes ago.
Step 3. GitLab Pipelines Dashboard
- This shows the Pipelines page in GitLab with successful deployments:
The highlighted row #2330658746 is the most recent deployment that was triggered automatically when code was pushed to the prd branch.
The pipeline deployment was successful. See the image below.
Step 4. AFTER: Updated Login Page
This shows the application after successful deployment. The title has changed from "Login Page" to "Addweb Login Page" - confirming the CI/CD pipeline worked correctly!
GitLab Pipeline Failure Scenario
Step 1. Intentionally Introduce an Error
To test pipeline failure behavior, we intentionally modified the package.json file by adding an invalid dependency:
Example change in package.json:
- "package-does-not-exist-3232": "2.1.0"
This package does not exist in the npm registry. the purpose was to simulate a real-world mistake such as:
- Typo in package name
- Incorrect dependency version
- Invalid module added by mistake
Step 2. Commit and Push the Wrong Code
After modifying package.json, the changes were committed and pushed to the prd branch:
- git commit -m "We mentioned the wrong package name in package.json."
- git push origin prd
Since our GitLab Pipeline is configured to run automatically on the prd branch, this push immediately triggered a new pipeline execution.
Step 3. GitLab Pipeline Triggered Automatically
As expected, GitLab Pipelines started running automatically as soon as the code was pushed.
In the Pipelines dashboard we can see:
- New pipeline execution created #2330702561
- Status initially shown as "Failed"
This confirms that the CI/CD automation is working correctly.
Step 4. Pipeline Execution Failed
During pipeline execution, the following command was executed on the server:
- npm install **
Because we added a non-existent package, the installation failed with this error:
npm error 404 Not Found - GET https://registry.npmjs.org/package-does-not-exist-3232 - Not found
npm error 404 The requested resource **'package-does-not-exist-3232@2.1.0' could not be found or you do not have permission to access it.
npm error 404
As a result:
- The pipeline step stopped
- Deployment process was aborted
- Application was NOT restarted
- Previous working version remained intact
If any step in the CI/CD pipeline fails, the deployment automatically stops. This protects production from broken or unstable code.
"First automate, then optimize."
7. Security Best Practices
- Use SSH keys instead of passwords
- Restrict EC2 security group (only required ports)
- Store secrets in GitLab CI/CD variables
- Disable root login on EC2
- Use environment variables for API keys
- Enable firewall (UFW)
8. Interesting Facts & Statistics
- Companies using CI/CD deploy 30x faster than manual deployment.
- Automated pipelines reduce deployment failures by 40–60%.
- GitLab CI/CD supports Auto DevOps for full automation.
- AWS EC2 is used by millions of applications worldwide.
- 90% of DevOps teams use CI/CD pipelines in production.
9. FAQs
Q1: Why use CI/CD for Node.js?
→ It automates testing and deployment, saving time and reducing errors.
Q2: Why GitLab CI/CD?
→ GitLab provides built-in CI/CD with repositories, making setup easier.
Q3: Why use PM2?
→ PM2 keeps Node.js apps running and supports auto restart.
Q4: Can we deploy multiple environments?
→ Yes, create separate branches:
- dev
- staging
- production
Q5: Is EC2 safe for production?
→ Yes, if proper security (SSH keys, firewall, updates) is applied.
10. Key Takeaways
- CI/CD automates build and deployment.
- GitLab pipeline integrates easily with AWS EC2.
- SSH keys ensure secure deployment.
- PM2 manages Node.js processes efficiently.
- Automated deployment saves time and reduces downtime.
11. Conclusion
Implementing CI/CD for a Node.js application using GitLab and AWS EC2 significantly improves development workflow and deployment reliability.
With automated pipelines:
- Developers can focus on coding
- Deployments become faster
- Errors are minimized
- Applications stay updated continuously
CI/CD is no longer optional, it is a standard practice for modern scalable applications.
About the Author: Rajan is a DevOps Engineer at AddWebSolution, specializing in automation infrastructure, Optimize the CI/CD Pipelines and ensuring seamless deployments.