-
Clone the Project:
- Create a
.env
file and add the following environment variables:DOMAIN="http://localhost:3000" PORT=3000 STATIC_DIR="./client" # Stripe API keys PUBLISHABLE_KEY="YOUR_PUBLISHABLE_KEY" SECRET_KEY="YOUR_SECRET_KEY"
- Note: Obtain the Stripe API keys from your Stripe account.
- Create a
-
Run Locally:
- Run the app locally to test if it’s working before deploying.
-
Create an IAM User:
- In AWS, create an IAM user with administrative access.
- Log in as the IAM user to create and manage your EC2 instance.
-
Create an EC2 Instance:
- Choose Ubuntu as the OS, select the region, configure a VPC, and create a key pair (
.pem
file). - Launch the EC2 instance.
- Choose Ubuntu as the OS, select the region, configure a VPC, and create a key pair (
-
Configure Key Permissions on Local Machine:
- For PowerShell: Open PowerShell in the folder containing your
.pem
file and set permissions (sincechmod
doesn’t work in PowerShell):icacls "yourkey.pem" /inheritance:r icacls "yourkey.pem" /grant:r "%username%:R" icacls "yourkey.pem" /remove "NT AUTHORITY\Authenticated Users" icacls "yourkey.pem" /remove "BUILTIN\Users"
- For Linux:
chmod 400 yourkey.pem
- For PowerShell: Open PowerShell in the folder containing your
-
Connect to EC2 Instance:
- Connect via SSH:
ssh -i "yourkey.pem" ubuntu@<EC2-Public-DNS>
- Connect via SSH:
-
Install Dependencies:
- Update and install necessary packages on the instance:
sudo apt update sudo apt install -y git nodejs npm
- Update and install necessary packages on the instance:
-
Clone GitHub Repository:
- Clone your repository and navigate to the project folder:
git clone https://github.com/your-username/aws-ec2-nodejs-app.git cd aws-ec2-nodejs-app
- Clone your repository and navigate to the project folder:
-
Install Node Modules:
- Install project dependencies:
npm install
- Install project dependencies:
-
Set Up
.env
on EC2:- Create and edit the
.env
file directly on the EC2 instance:touch .env vi .env # Add environment variables as needed
- Create and edit the
-
Start the Application:
- Start the Node.js app:
npm start
- Start the Node.js app:
-
Configure EC2 Security Group:
- In the AWS Console, go to Security Group for your instance.
- Allow traffic on port
3000
by adding a new rule for Custom TCP, port3000
, accessible from Anywhere (IPv4).
-
Access the App:
- Use the EC2 public IP with port
3000
to view your app:http://<EC2-Public-IP>:3000
.
- Use the EC2 public IP with port
-
Add Secrets:
- In your GitHub repository, go to Settings > Secrets and Variables > Actions and add the following secrets:
EC2_HOST
: Public DNS or IP of your EC2 instance (e.g.,ec2-100-26-43-234.compute-1.amazonaws.com
).EC2_USER
:ubuntu
(the default user for Ubuntu on EC2).EC2_KEY
: The content of your.pem
key.
- In your GitHub repository, go to Settings > Secrets and Variables > Actions and add the following secrets:
-
GitHub Actions Workflow File:
- In your project, create
.github/workflows/deploy.yml
with the following content:name: Deploy to EC2 on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Add SSH key env: EC2_KEY: ${{ secrets.EC2_KEY }} run: | mkdir -p ~/.ssh echo "${EC2_KEY}" > ~/.ssh/ec2-key.pem chmod 600 ~/.ssh/ec2-key.pem - name: Deploy to EC2 env: HOST: ${{ secrets.EC2_HOST }} USER: ${{ secrets.EC2_USER }} run: | ssh -o StrictHostKeyChecking=no -i ~/.ssh/ec2-key.pem $USER@$HOST << 'EOF' cd /home/ubuntu/aws-ec2-nodejs-app git pull origin main npm install pm2 restart your-app-name || pm2 start app.js --name your-app-name EOF
- This workflow automatically deploys your app to EC2 each time you push changes to the
main
branch.
- In your project, create
- Make sure
pm2
is installed on your EC2 instance for process management. - Replace
your-app-name
with the desired name for your app when usingpm2
. - Verify environment variables and secrets before running the deployment workflow.
This completes the setup for deploying a Node.js app to AWS EC2 with GitHub Actions for CI/CD. Enjoy your deployment!