<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Naved Hussain</title>
    <description>The latest articles on DEV Community by Naved Hussain (@naved1606).</description>
    <link>https://dev.to/naved1606</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3212698%2F2d0da942-49f7-4ccb-98ef-c1d203f77849.jpg</url>
      <title>DEV Community: Naved Hussain</title>
      <link>https://dev.to/naved1606</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naved1606"/>
    <language>en</language>
    <item>
      <title>🚀 Deploy a Node.js App to AWS EC2 with GitHub Actions in 15 Minutes</title>
      <dc:creator>Naved Hussain</dc:creator>
      <pubDate>Tue, 27 May 2025 06:19:09 +0000</pubDate>
      <link>https://dev.to/naved1606/deploy-a-nodejs-app-to-aws-ec2-with-github-actions-in-15-minutes-3630</link>
      <guid>https://dev.to/naved1606/deploy-a-nodejs-app-to-aws-ec2-with-github-actions-in-15-minutes-3630</guid>
      <description>&lt;h2&gt;
  
  
  🚀 &lt;strong&gt;Deploy a Node.js App to AWS EC2 with GitHub Actions in 15 Minutes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;CI/CD pipelines make development faster, safer, and more consistent. In this guide, we’ll automate the deployment of a Node.js app to an EC2 instance using GitHub Actions.&lt;/p&gt;

&lt;p&gt;🧱 Prerequisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic AWS account with EC2 access&lt;/li&gt;
&lt;li&gt;A running EC2 instance (Amazon Linux 2 or Ubuntu)&lt;/li&gt;
&lt;li&gt;SSH key pair configured&lt;/li&gt;
&lt;li&gt;A simple Node.js app in a GitHub repo&lt;/li&gt;
&lt;li&gt;GitHub Actions enabled in your repo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;☁️ Step 1: Launch and Configure EC2&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to EC2 → Launch instance&lt;/li&gt;
&lt;li&gt;Use Amazon Linux 2 or Ubuntu&lt;/li&gt;
&lt;li&gt;Open port 22 (SSH) and port 3000 (app port) in the security group&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SSH into the server:&lt;br&gt;
&lt;code&gt;ssh -i your-key.pem ec2-user@&amp;lt;EC2_PUBLIC_IP&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Node.js:&lt;br&gt;
&lt;code&gt;sudo yum update -y&lt;br&gt;
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -&lt;br&gt;
sudo yum install -y nodejs git&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔧 Step 2: Set Up the Node.js App&lt;/p&gt;

&lt;p&gt;SSH into the server and clone your repo:&lt;br&gt;
&lt;code&gt;git clone https://github.com/&amp;lt;your-username&amp;gt;/&amp;lt;your-repo&amp;gt;.git&lt;br&gt;
cd &amp;lt;your-repo&amp;gt;&lt;br&gt;
npm install&lt;br&gt;
node app.js  # or your startup script&lt;/code&gt;&lt;br&gt;
Ensure it runs on port 3000.&lt;/p&gt;

&lt;p&gt;🔐 Step 3: Configure SSH Key as a GitHub Secret&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Convert your private key to a single line:&lt;br&gt;
&lt;code&gt;cat your-key.pem | base64&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to your GitHub repo → Settings → Secrets and variables → Actions → Add new secret:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;EC2_KEY → Paste base64 key&lt;/li&gt;
&lt;li&gt;EC2_USER → ec2-user&lt;/li&gt;
&lt;li&gt;EC2_HOST → &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧪 Step 4: GitHub Actions Workflow&lt;/p&gt;

&lt;p&gt;Create a file in your repo: .github/workflows/deploy.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Deploy to EC2

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Decode private key
        run: |
          echo "${{ secrets.EC2_KEY }}" | base64 -d &amp;gt; key.pem
          chmod 600 key.pem

      - name: Deploy over SSH
        run: |
          ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} &amp;lt;&amp;lt; EOF
            cd &amp;lt;your-app-folder&amp;gt;
            git pull origin main
            npm install
            pkill node || true
            nohup node app.js &amp;gt; app.log 2&amp;gt;&amp;amp;1 &amp;amp;
          EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Done!&lt;br&gt;
Now, every time you git push, GitHub Actions will automatically deploy your app to your EC2 instance.&lt;/p&gt;

&lt;p&gt;📌 Final Tips&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replace node app.js with pm2 start if using process managers.&lt;/li&gt;
&lt;li&gt;Secure your instance! Rotate keys, use .env files, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know in the comments if you'd like a version with PM2, Docker, or Elastic Beanstalk.&lt;/p&gt;

&lt;p&gt;🟢 Tags:&lt;/p&gt;

&lt;h1&gt;
  
  
  aws #ec2 #githubactions #devops #cicd
&lt;/h1&gt;

</description>
    </item>
  </channel>
</rss>
