This guide helps developers set up an Auto Scaling Group (ASG) in AWS using EC2 instances configured to run a Bun-based project with PM2 for process management. This supports both CI/CD pipelines and manual launch scenarios.
- AWS account with permission to create EC2, Launch Templates, Load Balancers, and Auto Scaling Groups
- Bun project deployed to GitHub or available for
git pull
- Node.js (v22.14.0) and Bun installed or installable via script
- AMI based on Amazon Linux or Ubuntu (depending on your path setup)
ASG/
├── bin.ts
├── package.json / bun.lockb
├── .git/
Install dependencies and verify your app is working. Then:
- SSH into your EC2 instance
- Clone your repo and setup Bun:
git clone https://github.com/your-username/your-repo.git ASG cd ASG bun install bun run bin.ts
After setup:
- Go to EC2 Dashboard → Instances
- Select your configured EC2 instance
- Click Actions → Image and templates → Create image
- Name it (e.g.,
asg-bun-app
) and create the image
While creating a Launch Template:
- Choose the AMI created in Step 2
- Go to Advanced → User Data and paste one of the scripts below
#!/bin/bash
cd /home/ec2-user/ASG || exit
export PATH=$PATH:/home/ec2-user/.nvm/versions/node/v22.14.0/bin/
npm install -g pm2
git pull
bun install
pm2 start --interpreter /home/ec2-user/.nvm/versions/node/v22.14.0/bin/bun bin.ts
#!/bin/bash
cd /home/ec2-user/ASG || exit
export PATH=$PATH:/home/ec2-user/.nvm/versions/node/v22.14.0/bin/
npm install -g pm2
pm2 start --interpreter /home/ec2-user/.nvm/versions/node/v22.14.0/bin/bun /home/ec2-user/ASG/bin.ts
- Go to EC2 → Load Balancers
- Create an Application Load Balancer
- Use appropriate listener (e.g., HTTP on port 80)
- Link to a Target Group
- Register your EC2 instances with the target group
- Go to EC2 → Auto Scaling Groups
- Click Create Auto Scaling Group
- Link your Launch Template
- Attach your Load Balancer Target Group
- Choose Network and Subnets
- Configure Dynamic Scaling Policies
- After ASG is created, go to its settings
- Select Automatic Scaling
- Add policy to scale out when CPU > 70%
- Add policy to scale in when CPU < 30%