Optimizing EC2 Build Time

User
Npm run build command is slow on Amazon ec2 free tier
Assistant
The slowness of the `npm run build` command on an Amazon EC2 free tier instance is common due to limited resources. The free tier provides a micro instance (like `t2.micro` or `t3.micro`), which only has 1 vCPU and 1 GB of RAM. These limited resources can make build processes (especially for large applications) significantly slower.

Here are some strategies to optimize the build process on your EC2 instance:

### 1. **Increase Swap Memory**
By default, EC2 instances may not have swap enabled, which can cause performance issues, especially when there's not enough RAM. Increasing swap memory can help mitigate this:

# Create a swap file (e.g., 2GB)
sudo fallocate -l 2G /swapfile

# Secure the swap file
sudo chmod 600 /swapfile

# Mark it as swap space
sudo mkswap /swapfile

# Enable the swap file
sudo swapon /swapfile

# Verify if swap is enabled
sudo swapon --show

# Make the change permanent (edit /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

### 2. **Use Node.js Flags to Optimize Build**
You can use flags to limit memory usage when running the build process:

NODE_OPTIONS="--max-old-space-size=512" npm run build

This command limits the memory usage to 512MB, which can prevent your instance from running out of memory.

### 3. **Build with Fewer Resources**
Use the `–no-cache` option or other build-specific flags if your project supports them:

npm run build -- --no-cache

### 4. **Optimize Dependencies**
Make sure your project dependencies are optimized:

– Use `yarn` instead of `npm` for potentially faster dependency resolution:

yarn install
  yarn build

– Remove unnecessary dependencies and reduce package bloat.
– Use `npm ci` instead of `npm install` if you're working with a `package-lock.json` file. This command is faster and more reliable for production builds:

npm ci
  npm run build

### 5. **Use `screen` or `tmux` to Avoid Timeouts**
If your SSH session disconnects, it could interrupt the build process. Use `screen` or `tmux` to run your build in a separate session:

# Start a new screen session
screen -S build-session

# Run your build
npm run build

# Detach from the session (press Ctrl + A, then D)

### 6. **Upgrade EC2 Instance Type (Optional)**
If feasible, consider temporarily upgrading your instance to a type with more CPU and RAM, such as `t2.medium` or `t3a.small`. You can always scale back down after completing your build.

### 7. **Leverage Build Caching (Docker)**
If you're using Docker, take advantage of build caching to speed up your build:

# Dockerfile example
FROM node:21 AS build

WORKDIR /app

# Cache package installation
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy all files and build
COPY . .
RUN yarn build

Then, build using:

docker build --no-cache -t my-app .

### 8. **Build Locally and Deploy**
If the above solutions don't help, consider building the application locally on your more powerful machine and then deploying the build artifacts to the EC2 instance:

1. **Build Locally:**

npm run build

2. **Upload Build to EC2:**

scp -r ./dist ubuntu@ec2-16-170-254-93.eu-north-1.compute.amazonaws.com:/var/www/share

These optimizations should help reduce the build time on your EC2 free tier instance.

Leave a Comment

Your email address will not be published. Required fields are marked *