How to deploy Next.js app on AWS Elastic Beanstalk via Travis?
Tags: nextjs, deployment, elasticbeanstalk, aws
Published: 2020-04-27 06:13:36

---
title: How to deploy Next.js app on AWS Elastic Beanstalk via Travis?
published: true
date: 2019-10-19 14:27:22 UTC
tags: nextjs, deployment, elasticbeanstalk, aws
canonical_url: https://medium.com/commutatus/how-to-deploy-next-js-app-on-aws-elastic-beanstalk-via-travis-333f66fe3102
---

![Tutorial on how to deploy Next.js app on AWS Elastic Beanstalk via Travis](https://cdn-images-1.medium.com/max/1024/1*9mxGLfXw4oMYnfUkty0EBQ.jpeg)<figcaption>How to deploy Next.js app on AWS Elastic Beanstalk via Travis?</figcaption>

Single-page JavaScript applications can be pretty challenging these days due to client-side routing, page layout, APIs, server-side rendering and mainly SEO. This is where Next.js helps you build server-side rendered, SEO friendly apps with high customisability.

#### Why deploy to AWS?

Next.js was built by Zeit back in 2016 and has quickly gained traction to the point of becoming one of the most popular tools of its kind. The same team built [Zeit Now](https://zeit.co) — the easiest way to deploy websites and host your web projects with zero configuration. Unfortunately, it doesn’t give us full control of AWS instances and lambda functions where our apps are hosted. Since most companies like to host apps on their own AWS servers for privacy concerns, let’s see how we can deploy a Next.js application on AWS.

> If you are building a **serverless** Next.js app, do checkout [Serverless Next.js Component](https://github.com/danielcondemarin/serverless-next.js/tree/master/packages/serverless-nextjs-component) by [Daniel Conde](https://medium.com/u/d6927da06a80) which allows you to deploy Next.js apps to your own AWS Lambda@Edge functions in every CloudFront edge location across the globe with almost no configuration. But if you are using Express server in your app for other functionality, you might want to host it on AWS Elastic beanstalk.

#### Steps to host your Next.js app on AWS Elastic beanstalk via Travis:

**Step 1** : Make sure your app has a server for example Express and next.config.js **doesn’t** have the target set to serverless.

![](https://cdn-images-1.medium.com/max/1024/1*ra6jJ1JZ8YhnIgoqPGJi0A.png)<figcaption>Next.js app on express for AWS hosting</figcaption>

**Step 2** : Edit package.json to execute your scripts for development, staging and production environments.

```
"scripts": {

        "dev": "NODE\_ENV=development node ./server.js",
        "build": "next build",
        "start": "next start",

        "deploy-staging": "NODE\_ENV=staging next build && NODE\_ENV=staging node ./server.js",

        "deploy-production": "NODE\_ENV=production next build && NODE\_ENV=production node ./server.js"

},
```

**Step 3** : Time for AWS! Go to your AWS dashboard and click on Elastic Beanstalk.

![](https://cdn-images-1.medium.com/max/1024/1*au9HrwmAuhyJL7FKvOyqlQ.png)<figcaption>AWS Elastic beanstalk for Next.js</figcaption>

**Step 4** : Click on Create New Application and enter a name for your application.

![](https://cdn-images-1.medium.com/max/1024/1*F1QwgHfQJ2NaLvRVob0Fxg.png)<figcaption>Create an application on AWS Elastic Beanstalk for Next.js</figcaption>

**Step 5** : Once you have created the application, under Actions dropdown, click on Create environment

![](https://cdn-images-1.medium.com/max/828/1*8tQQh0xJsWuTeZPhDsYPyw.png)

**Step 6** : Choose Web server environment and click select.

![](https://cdn-images-1.medium.com/max/1024/1*33CQck9_Q4I0zPxvZqjlCA.png)<figcaption>Elastic beanstalk web server environment for Next.js app</figcaption>

**Step 7** : Now, enter the environment name for example next-app-staging or next-app-production and choose a domain. Make sure to select Node.js under Platform settings and click on Configure more options at the bottom.

![](https://cdn-images-1.medium.com/max/1024/1*FsEa6mexoMdCUYwBJLcloQ.png)

**Step 8** : Click on modify of the **Software** tab and type npm run deploy-staging or npm run deploy-production in the **Node command** field based on your environment.

![](https://cdn-images-1.medium.com/max/1024/1*lCa3_KBxQZlKsITea3wJgA.png)<figcaption>Deploy Next.js on AWS</figcaption>

**Step 9** : Click on Create Environment and wait for a few minutes to let AWS set up everything for you.

**Note** : Don’t worry if your application Health status shows **Severe** because we still didn’t upload our app to run the node command. At this point, if you want to upload the app manually, you can do so by simply zipping the entire project folder without next build folder. But, come on just one more step to automate the whole deployment process.

**Step 10** : Now let’s integrate Travis for CI. You can check out several online tutorials on how to set up Travis with your GitHub repository. Create a travis.yml file in the root directory and configure it as follows:

{% gist https://gist.github.com/chandan-reddy-k/58c1da8ae354754d0287ab95a2e7816a %}

**Note** : The above configuration lets you deploy to different environments staging and production.

**Step 11** : Login to your Travis dashboard and click on your project repository. Go to more options > settings > Environment variables and add your AWS access keys and S3 bucket.

![](https://cdn-images-1.medium.com/max/1024/1*UO6e1busMVPsUWQJKVJL6w.png)<figcaption>Travis for next.js app deployment on aws</figcaption>

**Note** : You can copy the S3 bucket name after **Step: 9 is** completed by going to AWS Console > Services > S3. We just need to copy the bucket name and not the path.

**Step 12** : That’s it! Just push your code to Github, sit back and relax. Travis will automatically trigger a build and deploy the latest version of your Next.js app on AWS Elastic Beanstalk.

**Bonus tip** : To avoid any downtime of 5–10 min during the deployment process, create multiple EC2 instances/load balancers and enable rolling updates.

#### **Additional Information: (Alternative build process)**

The above steps will build and run your Next.js application on AWS Elastic Beanstalk and Travis is just used to push your code from GitHub to AWS. But if you want to build your app on Travis and then run it on AWS, here are the configuration changes you will have to make:

In package.json,

```
"scripts": {

"dev": "NODE\_ENV=development node ./server.js",

"build": "next build",

"start": "next start",

"build-staging": "NODE\_ENV=staging next build",

"deploy-staging": "NODE\_ENV=staging node ./server.js",

"build-production": "NODE\_ENV=production next build",

"deploy-production": "NODE\_ENV=production node ./server.js"

},
```

In travis.yml, add the following lines before deploy:

```
before\_script:
 - npm install

script:
 - npm run build-${NODE\_ENV}

before\_deploy:
 - cd $TRAVIS\_BUILD\_DIR
 - sed -i '/.next/d' .gitignore
 - git add . && git commit -m "latest build"
```

The above code will install **npm** on travis virtual machine and build your application. You can add an NODE\_ENVenvironment variable in travis available only to a specific branch. For example:

![](https://cdn-images-1.medium.com/max/1024/1*6khs54jhK7UpLm3XluFTWQ.png)<figcaption>Env variable to branch in travis</figcaption>

This will ensure the right build (staging/production) is made on travis. Finally, the before\_deploy: will go to your build directory, remove .next from .gitignore and commit the changes temporarily on travis.

Now that your application is built on Travis, you just need to start your app server on AWS as mentioned in this tutorial.

> Phew! Feeling tired ? Clone my [Github repo](https://github.com/chandan-reddy-k/nextjs-aws-elastic-beanstalk). 😉

![](https://cdn-images-1.medium.com/max/1024/1*OvcEv8uJ7iI-nSi9Ej6s1g.jpeg)<figcaption><a href="https://www.gehnaindia.com">Gehna (E-commerce jewellery platform) built with ❤️ at Commutatus using NEXT.j</a>s</figcaption>

#### Conclusion:

If you find this article useful as a guide to deploying your Next.js app running on the express server to AWS Elastic Beanstalk via Travis CI, give an 👏 and leave your queries in the comment section below? I’d love to answer them 😋. Also, do check out the awesome E-commerce website [Gehna](https://www.gehnaindia.com) that we built at Commutatus using Next.js running on AWS Elastic beanstalk.

* * *