DEV Community

Boryamba
Boryamba

Posted on • Updated on

Deploying dockerized NextJS App to AWS Elastic Beanstalk

Table of content:

Hello everyone.

In previous part we created dockerized NextJS app. Now it's time to deploy.

Everything in here is within AWS Free tier.

Also please pay attention that all links to AWS I provide use my region of choice, if you're using different AWS region make sure to change it before making any changes!

Deployment to Beanstalk

There are two ways to deploy our app to Beanstalk:

  1. Manual upload to Beanstalk
  2. Beanstalk CLI

Manual upload to Beanstalk

In your project folder delete node_modules directory (and .next if present), archive leftovers into zip archive. Switch to AWS Console.

New Beanstalk application

If you don't have a Beanstalk application, we need to create one:

  1. Open AWS Console Beanstalk welcome page
  2. Click Create application button
  3. Fill in application name and select platform Docker application name and platform section configuration

In Application code section select Upload your code, in Source code origin select Local file and choose created zip archive.

application code section configuration

Finally, click Create application. It will take a few minutes to deploy our app.

Existing Beanstalk application

If you already have a Beanstalk application, we need to create a new application for our NextJS app (skip to create new environment if you want to use existing Beanstalk application):

  1. In EB Console jump to Applications tab in the sidebar
  2. Click Create new application
  3. Fill in application name and click Create



Now it's time to create a new environment for our Next app:

  1. Open created application (if you was following previous step, you already there)
  2. Tap Create a new environment
  3. On Select environment tier step leave defaults (Web server environment) and click Select
  4. Select Docker platform and upload your zip archive create environment step

Finally, click Create environment.

After it's done you'll be provided with a URL for your application.

To update application, open environment in EB console and click Upload and deploy

Elastic Beanstalk CLI

First you need to install EB CLI.

Follow instructions there to install it using script.

If for some reason it's not working (i.e. you're using Arch linux), follow the procedures there.

Initialize application

After it's installed and checked to work, we need to initialize our app:

eb init -r eu-west-1 -p docker my-dockerized-next-app

-r eu-west-1 flag tells what region to use
-p docker flag tells that we're using docker
my-dockerized-next-app sets application name

If you don't specify region flag, use eb status or check config.yml file to find what region is used.

You can use application name that already exists in AWS EB Console - every environment you create with EB CLI will be created within existing EB application.

Deploy

Before proceeding, make sure all your files are commited - EB CLI will produce incorrect result if they are not.

Now we can deploy our app to AWS:

eb create --elb-type application my-nextjs-env

--elb-type application tells to use Application Load Balancer - without this flag Classic Load Balancer (which is outdated) will be used
my-nextjs-env sets environment name

If environment with provided name already exists within selected application, it will be updated.

Update

To update application, commit your changes and execute eb deploy command.

In the next part we are going to connect our domain to EB application. Hope to see you there =)

Thanks for reading.

Top comments (0)