How to Deploy Next.js App & setup CI/CD using AWS
Published: 2023-05-03 13:07:29


![](https://cdn-images-1.medium.com/max/2048/1*vXAe-xrX6zEnf_WZld_pHw.png)

Hello Everyone!

Deploying a web application is a challenging task (at least for me), especially when it comes to keeping it updated. It can take up a lot of time and energy if it has to be deployed manually every time a change is made. But I recently discovered a way to automate the deployment process for the Next.js app using AWS CodeDeploy and CodePipeline. It made my life so much easier, and I’m excited to share it with you.

In this blog, I’ll guide you through the process of setting up auto-deployment for your Next.js app using AWS services such as CodePipeline and CodeDeploy. By the end of it, you’ll be able to save a lot of time by deploying your app automatically every time you push the code.

Let’s get started!

## Prerequisites

## 

 1. EC2 Machine running Ubuntu

 2. Very basic knowledge of EC2 and IAM AWS Services

## How to deploy the Next.js app to AWS EC2

To start simple, let’s manually deploy the sample Next.js boilerplate app “hello-world” to EC2. The steps are almost the same for all Next.js applications.

## Login to EC2

## 

Login to the EC2 machine which you’ve created using the below command

    ssh -i /path/key-pair-name.pem [email protected]

When you try to log in to EC2, this is the common error that most people will encounter (including me).

![Permissions 0664 for .pem file is too open error](https://cdn-images-1.medium.com/max/2048/0*GWVttJkXg2FdaC4-.png)

This error describes that the .pem should be read-protected. Only the root user should be able to read it. So, you have to set the file permission to 400. Run the following command to achieve that.

    chmod 400 key-pair-name.pem

EC2 by default come with no software installed. Once you logged into EC2, install NodeJS. There’s an excellent [article](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04) published by Digital Ocean and I use it every time I have to install NodeJS on the server.

I have uploaded the [boilerplate repo](https://github.com/5minslearn/deploy_nextjs_app) to Github. You can clone the repo by running the following command.

    git clone https://github.com/5minslearn/deploy_nextjs_app.git

Navigate to the project repo and install the project dependencies by running the below commands.

A Quick note here. I’m a big fan of yarn for its lightning-fast dependency management. But I see most people use npm to manage their dependencies. If you like to use npm, you can replace yarn install with npm install in the below commands.

If you like to go with yarn, install yarn by following this [tutorial](https://classic.yarnpkg.com/lang/en/docs/install/#debian-stable) first.

    cd deploy_nextjs_app 
    yarn install

Let’s run the Application.

    yarn dev

Hit “http://ec2-public-ip-address:3000/" on your browser and you should be able to see the following page.

![Next.js Hello World App](https://cdn-images-1.medium.com/max/2048/0*SAEjSSxdVNOdI29u.png)

There’s another common issue that most people face here.

## How to fix timeout error (EC2)

“Oh, My God!!! My site is loading for a long time and finally, it’s throwing a timeout error. What could be the issue? Where did I made mistake?”

If this happens to you, then you can follow the below steps to fix that.

This issue basically occurs if your server does not expose port 3000. Remember people, by default NextJS app, will be running on port 3000. But, you have to allow port 3000 from the Security Group of your EC2 console to access from your browser.

Login to your AWS console, select your EC2 instance and then select the Security group option. Click on the “Edit inbound rules” button. Add port 3000 to the list as shown in the below screenshot. Then hit the “Save rules” button.

![Adding a port 3000 to a security group](https://cdn-images-1.medium.com/max/2048/0*8LrGwfnNLy-I6qeT.png)

Visit the link “http://ec2-public-ip-address:3000/", and you’ll be amazed to see your page loaded like magic.

So far, we just ran our app in development mode and verified that it’s working.

## Run Next.js App in Production Mode

To deploy the app in Production Mode, you have to build your app first. Run yarn build to build the app and yarn start to start the app in production mode.

    yarn build 
    yarn start

Hit “http://ec2-public-ip-address:3000/" again and this time you’ll note that your app loads faster than earlier.

Because the apps running in Production mode will always be faster when compared to the ones running in Development mode. And the reason for it is, the Production apps will be optimized for performance.

### How to run Next.js app forever when console is closed?

### 

So, you have your app running now. You could notice that it’s blocking you from closing your terminal and exiting from server connection. If you do so, your site will be down. That’s where PM2 comes to play. Basically, PM2 is a process manager that helps to keep NodeJS applications alive all the time. It runs in the background managing NodeJS applications for you.

Install PM2 using the following command

    sudo yarn global add pm2

After PM2 installation, run the below command to run and manage your app in the background.

    pm2 start yarn --name [name-of-your-app] -- start -p [port-number]

Replace [name-of-your-app] with your app name and [port-number] with 3000. Here's an example command,

    pm2 start yarn --name next_hello_world_app -- start -p 3000

Hit “http://ec2-public-ip-address:3000/" and you’ll again be amazed to see your app is up and running.

It’s always a best practice to save the PM2 process. When you reboot your instance, your PM2 instances will be lost. In order to restore it to its old state, you have to save the PM2 process. Here’s the command for that.

    pm2 save

Here’s the command to restore your PM2 instances.

    pm2 resurrect

We have successfully deployed the Next.js app manually. But remember, every time you make a code change and want to see them on your site, you have to login into EC2, pull the latest changes, build the app, and restart the app. This will consume plenty of time and I’m super lazy to do this. Let’s automate this in the next step!

Before setting up automatic deployment you have to have a knowledge of CodeDeploy.

## CodeDeploy

CodeDeploy is used to deploy our application automatically to any number of EC2 instances. We need to get ready with two items before beginning this.

 1. CodeDeploy Agent must be installed in EC2 instance — It is used to continuously poll CodeDeploy and deploy if anything is available

 2. A file called appspec.yml must be present in the root folder - This file describes the steps to be followed for the deployment

There is awesome [documentation](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html) by AWS to install CodeDeploy Agent. Please follow each and every step in the [documentation](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html) to install CodeDeploy Agent on your EC2 machine.

To verify CodeDeploy agent is installed run the below command. If you see *active (running)* Kudos to you! CodeDeploy was installed successfully!

    sudo service codedeploy-agent status

![CodeDeploy Agent running status](https://cdn-images-1.medium.com/max/2048/0*ycRQgB8NBk4e_Ats.png)

Now let’s create the appspec.yml file. I've written the deployment instructions in deploy.sh file. It's enough to run the file in appspec.yml file. If you want to learn more about appspec.yml check out AWS official [documentation](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure.html)

Create a file called appspec.yml and add the following contents

    version: 0.0 
    os: linux 
    hooks: 
      ApplicationStart: 
        - location: deploy.sh 
          timeout: 300 
          runas: ubuntu

I hope you understand the instruction in the above file. If not, here’s a super simple explanation. I’m advising the CodeDeploy Agent that I’m running a Linux OS in my instance and instructing it to run the deploy.sh file as ubuntu user with the timeout being 300 seconds.

Here’s my deploy.sh file.

    #!/bin/bash
    cd /path/to/project/on/EC2 
    git pull origin master
    yarn install &&
    yarn build &&
    pm2 restart [name]

This file contains instructions to navigate to the project folder on EC2, pull the latest code from source control, install dependencies, build the project, and restart the project instance.

Commit these files and push them to Git. Now it’s time to set up automatic deployment.

## How to setup auto deployment using CodePipeline and CodeDeploy

Two IAM roles have to be created to set up auto-deployment. Some complications will begin from here. To make things simple, I’ve attached screenshots with the appropriate items highlighted with red boxes.

## Create IAM Role for CodeDeploy

Navigate to IAM in AWS Console by searching on the search bar at the top. Click Roles on the left pane and Click Create role button at the top right.

![Create IAM role](https://cdn-images-1.medium.com/max/2048/0*rdchTnIREZmcz4mu.png)

Choose AWS service in Trusted entity types and choose CodeDeploy in Use cases and proceed to the next step.

![IAM role for CodeDeploy](https://cdn-images-1.medium.com/max/2048/0*z8VehvLlgkupyhdF.png)

Now, you can see AWSCodeDeployRole policy is the only policy available, and it’ll be chosen by default in this (Permissions) step. Let’s proceed to the next section.

![AWSCodeDeploy Permission](https://cdn-images-1.medium.com/max/2048/0*YRtV0uoEl1Zxwp06.png)

Enter a name for your IAM role. It’s advised to choose a meaningful name to identify this in the future. I’m calling it *service-role-for-code-deploy*. Review the permission in the JSON and Click Create role button at the bottom.

![AWSCodeDeploy Permission Review](https://cdn-images-1.medium.com/max/2048/0*VATtmLmZdYwvlym5.png)

## Create IAM role for EC2

Let’s create the next role. This role is for EC2. Choose AWS service in the Trusted entity type, EC2 in the Common use cases section and choose CodeDeploy in Use cases for other AWS services. Click Next to proceed to the next section.

![IAM role for EC2](https://cdn-images-1.medium.com/max/2048/0*AGWIHKepVhQLHWfb.png)

There are lot of policies available for EC2 and CodeDeploy. In the Add permissions section, search for *codedeploy* (No space between code and deploy) and select “AmazonEC2RoleForCodeDeploy” and proceed to the next step.

![Adding AmazonEC2RoleForCodeDeploy permission](https://cdn-images-1.medium.com/max/2048/0*r9vdF62wINQnuRlB.png)

No change in this step. Review and give a meaningful name for your role and click Create role button.

## How to Attach the IAM role to EC2

Once the IAM role for EC2 is created, we have to attach it to the EC2 instance.

![EC2 instance before attaching the IAM role](https://cdn-images-1.medium.com/max/2048/0*JsdRkAqD-FbsVDsw.png)

To attach the IAM role to the EC2 instance, open your EC2 instance, click on the “Actions” button on the top right and select “Security” in the drop-down, then select “Modify IAM role”.



![Modify IAM role for EC2 instance](https://cdn-images-1.medium.com/max/2048/0*-sTWQyWDSkVKVJ7K.png)

Select the IAM role which you created last and click the “Update IAM role” button. Reboot the EC2 for the changes to take effect.

![Update IAM role for EC2 instance](https://cdn-images-1.medium.com/max/2000/0*EFt9giubn58fsLRh.png)

![EC2 instance after attaching IAM role](https://cdn-images-1.medium.com/max/2048/0*JoBELFKchKxbh6xd.png)

After rebooting the EC2, login to EC2 with SSH and run pm2 resurrect command to restore the pm2 processes. Missing to do this may land you at "PM2 Process or Namespace not found error".

![PM2 process restore](https://cdn-images-1.medium.com/max/2048/0*UjYtHo5i90ZfJk9N.png)

![PM2 process or namespace not found an error](https://cdn-images-1.medium.com/max/2048/0*t4kOVXVyjvyBwdH-.png)

## How to create CodeDeploy Application

In the AWS Console, search “CodeDeploy” in the search bar at the top. Select “Applications” in the left pane. Click on the “Create application” button on the top right.

![Choose an application in the CodeDeploy](https://cdn-images-1.medium.com/max/2048/0*PQ7I_HdTOZfwGsVg.png)

Enter the Application name, choose the “EC2/On-premises” compute platform, and click the “Create application” button.

![Create CodeDeploy application](https://cdn-images-1.medium.com/max/2048/0*oBuFVjrWU6a83g7E.png)

![](https://cdn-images-1.medium.com/max/NaN/undefined)

Once it’s done, you’ll automatically be redirected to the Deployment groups section. We have to create a deployment group. Click on the “Create deployment group” button.

![Create CodeDeploy Deployment group](https://cdn-images-1.medium.com/max/2048/0*qNivJ2JVX1dIKiky.png)

Enter the deployment group name, select the service role (1st created role) you created, and select the deployment type as In-place

![Create CodeDeploy Deployment group](https://cdn-images-1.medium.com/max/2048/0*qXUnOTSy5HifDfn0.png)

In the Environment configuration section, select “Amazon EC2 instances” and select the key as Name, enter your EC2 instance name in the value.

![Code Deployment Group Environment configuration](https://cdn-images-1.medium.com/max/2048/0*rl7ijnK_P6-a3paz.png)

In the Agent configuration section, select Never, as we installed CodeDeployAgent already. Select “CodeDeployDefault.AllAtOnce” in the Deployment settings section. Leave the “Enable load balancing” checkbox unchecked. Finally, click Create a deployment group button.



![CodeDeploy deployment group configurations](https://cdn-images-1.medium.com/max/2048/0*O3qfYFD5DWdYSU3r.png)

## CodePipeline

AWS CodePipeline helps you to automate your release pipelines for fast and reliable application and infrastructure updates. Now it’s time to create CodePipeline. In the AWS Console, search for “CodePipeline” in the search bar.

Select “Pipelines” in the left pane and click on “Create pipeline” button.

![Create CodePipeline](https://cdn-images-1.medium.com/max/2048/0*W92nx5Xh1KouLo2j.png)

Enter the Pipeline name, and enter the Role name. Remember, we created a Role for EC2 and CodeDeploy and not for CodePipeline. AWS by default creates it from here and we have to enter the Role name.

![CodePipeline settings](https://cdn-images-1.medium.com/max/2048/0*kCK02VW-cRRYGAeF.png)

### Add Source Stage

### 

We have to connect our repo with CodePipeline to deploy the changes immediately after the code is pushed.

We’ll be using GitHub as our source. The next step is to add a GitHub repo to CodePipeline. Choose GitHub(version 2) in the source provider, and click on the “Connect to GitHub” button, which will open up a new pop-up window, click the “Connect to GitHub” button.

![CodePipeline adding source stage](https://cdn-images-1.medium.com/max/2048/0*NGb8FoNCKR_WLuS2.png)

This will take you to the GitHub authorization page where you have to sign into your GitHub account, and click the “Install a new app” button.

![CodePipeline Github Authorization](https://cdn-images-1.medium.com/max/2048/0*aZBDCiIvDFyItNtl.png)

Choose “Only select repositories” and choose your repository below that.

![Installing AWS connector for GitHub](https://cdn-images-1.medium.com/max/2048/0*RN1wQldBACslpIe3.png)

Once installed it will prompt you for the password. Click the “Connect” button once you’re done with your authentication.



![Connecting GitHub to AWS](https://cdn-images-1.medium.com/max/2048/0*Yodiv9TbYeU6zNY4.png)

After connecting to GitHub, select the Repository name and branch name. To start the CodePipline on code change, it’s important to select the check box “Start the pipeline on source code change” otherwise auto deployment will not happen. For “Output and artifact format”, select “CodePipeline default” and click the “Next” button.



![CodePipeline — Select source code repo](https://cdn-images-1.medium.com/max/2048/0*gONeHoexzHSDVgmu.png)

The next step is to add the build stage, since we’re deploying a simple app we don’t need a build stage. If you want me to write about CodeBuild let me know. I will cover it in my upcoming blogs, but for now, let’s keep things simple and simply skip the build stage.

### Add deploy Stage

In the deployment stage, choose “AWS CodeDeploy” for the “Deploy provider” and select the region where you created the above CodeDeploy application. Then select the “Application name” and “Deployment group” that we created in the previous steps and click the “Next” button.

![CodePipleine — Adding Deployment stage](https://cdn-images-1.medium.com/max/2048/0*Us3l30aBKhRhpw__.png)

The last step is “Review”. Review everything carefully and click on the “Create pipeline” button. Once the pipeline is created it will start the deployment process. If you followed all the above steps the pipeline should be “Succeeded” on your very first build.

![Pipeline Succeeded](https://cdn-images-1.medium.com/max/2048/0*Prs-h3J2y7EDZ5GM.png)

![](https://cdn-images-1.medium.com/max/NaN/undefined)

## Verifying Auto Deployment

Now let’s verify if the Auto-deployment works properly. This is the Home page of our project.



![Next.js Hello World App](https://cdn-images-1.medium.com/max/2048/0*9a54nNX9p_UB_jbx.png)

Let’s change the text from “Hello World” to “Welcome to 5minslearn” and push the code to GitHub.

![Git code diff](https://cdn-images-1.medium.com/max/2048/0*7qtqnJsgCckdniUt.png)

Here we go!!! The CodePipeline has triggered automatically and the changes are successfully deployed.

![CodeDeploy getting triggered automatically on code changes in Git](https://cdn-images-1.medium.com/max/2048/0*Jd5pGfIpzQLLAEm7.png)

Now head to “http://ec2-public-ip-address:3000/", you will see the below page.

![](https://cdn-images-1.medium.com/max/2048/0*P-aA5WTqsbEJicq7.png)

Congrats! 🎉 We successfully completed setting up the auto-deployment for Next.js app.

## Conclusion

In this article, we learned how to deploy Next.js manually on EC2 and set up auto-deployment using AWS services such as CodeDeploy and CodePipeline.

Hope you enjoyed reading this article! If you are stuck at any point feel free to drop your queries to me at my [email](https://www.gogosoon.com/cdn-cgi/l/email-protection#4b2a393e250b2c242c243824242565282426). I’ll be happy to help you!

If you wish to learn more about AWS, subscribe to my email newsletter at my [site](https://5minslearn.gogosoon.com/?ref=devdotto__aws_autodeploy) ([https://5minslearn.gogosoon.com](https://5minslearn.gogosoon.com/?ref=devdotto__aws_autodeploy)).

*Originally published at [https://www.gogosoon.com](https://www.gogosoon.com/blog/how-to-deploy-next-js-app-setup-ci-cd-using-aws) on March 28, 2023.*