How to Setup CI/CD Pipeline for Next.js App with AWS: Complete Step by Step Guide
By Braincuber Team
Published on March 27, 2026
Setting up automated CI/CD pipelines is crucial for modern web development, especially when working with Next.js applications on AWS. Manual deployment processes are time-consuming, error-prone, and don't scale well with your team. This comprehensive guide will walk you through setting up a complete automated deployment pipeline using AWS CodePipeline and CodeDeploy.
What You'll Learn:
- Prerequisites for AWS CI/CD setup
- EC2 instance configuration and IAM setup
- CodeDeploy Agent installation and configuration
- Creating appspec.yml file for deployment instructions
- Setting up CodePipeline with GitHub integration
- Configuring CodeDeploy for automatic deployments
- Creating IAM roles for secure deployment access
- Testing and verifying automated deployment
Prerequisites
Before setting up your CI/CD pipeline, you'll need:
EC2 Instance
Ubuntu server with Node.js installed and basic EC2/IAM knowledge
SSH Key Pair
PEM file for secure EC2 access
GitHub Repository
Next.js application code with proper structure
AWS CLI Tools
AWS Command Line Interface configured
Manual Deployment to EC2
Let's start by manually deploying a Next.js application to EC2 to understand the basic deployment process:
Step 1: Login to EC2
Connect to your EC2 instance using SSH with the key pair you created:
SSH Command
ssh -i /path/key-pair-name.pem instance-user-name@instance-IP-address
Common Error: Permissions 0664
If you get "Permissions 0664 for .pem file is too open" error, fix by setting file permissions to 400:
Install Node.js
Use the official Node.js installation guide for Ubuntu servers
Clone Repository
git clone https://github.com/5minslearn/deploy_nextjs_app.git
Install Dependencies
yarn install or npm install (recommend yarn for speed)
Run Application
yarn dev or npm run dev
Verify Application
Visit http://ec2-public-ip-address:3000/ to see your app
Step 2: Fix Timeout Error and Optimize Performance
Common issues include timeout errors and slow loading. Here's how to fix them:
EC2 Security Group
Add port 3000 to inbound rules for web access
Production Mode
Build and run in production for better performance
Installing CodeDeploy Agent
Step 3: Install CodeDeploy Agent
CodeDeploy Agent is an AWS service that helps manage deployments on EC2 instances. Install it using:
Installation Command
sudo apt-get update && sudo apt-get install codedeploy-agent
Verify Installation
Check if CodeDeploy Agent is running
Creating appspec.yml File
Step 4: Create appspec.yml File
The appspec.yml file tells CodeDeploy how to deploy your application. Here's the basic structure:
version: 0.0
os: linux
hooks:
ApplicationStart:
- location: deploy.sh
timeout: 300
runas: ubuntu
files:
- source: /
destination: /var/www/html
Key Elements
- version: 0.0 - semantic versioning
- os: linux - specifies the operating system
- files: source and destination directories
- hooks: lifecycle events and scripts
Advanced Configuration
For more complex deployments, you can add build stages, test configurations, and approval workflows
Setting Up CodePipeline
Step 5: Create CodePipeline with GitHub Integration
AWS CodePipeline automates your build and deployment process. Here's how to set it up with GitHub:
Create Pipeline
Search for "CodePipeline" in AWS Console and create new pipeline
Source Provider
Choose GitHub (version 2) as source provider
Repository Selection
Select your GitHub repository and branch
Auto-Trigger
Enable "Start the pipeline on source code change" for automatic deployments
Configuring CodeDeploy for Automatic Deployment
Step 6: Configure CodeDeploy for Automatic Deployment
CodeDeploy handles the deployment process on your EC2 instance. Here's how to configure it:
Application Selection
Choose the CodeDeploy application you created earlier
Deployment Group
Select the deployment group you created
Service Role
Select the EC2/On-premises service role you created
Agent Configuration
Select "Never" for deployment settings, disable load balancing
Creating IAM Roles for CodeDeploy
Step 7: Create IAM Roles for CodeDeploy
CodeDeploy needs IAM roles to deploy applications to your EC2 instances. Create two separate roles:
EC2 Role
For deploying to EC2 instances with full admin access
CodeDeploy Role
For CodeDeploy service to manage deployments
Testing and Verification
Step 8: Test Automated Deployment
Once everything is set up, test your automated deployment:
Push Code Changes
Make changes to your Next.js app and push to GitHub
Monitor Pipeline
Check AWS CodePipeline console for deployment status
Verify Deployment
Visit your application to confirm automatic deployment worked
Success Indicators
Look for "Succeeded" status in CodePipeline and faster app loading
