Deploy a Web App on AWS Elastic Beanstalk

GETTING STARTED GUIDE

Module 3: Deploy Web Application

In this module, we will deploy your application to the cloud.

Introduction

In this module, you will learn to package your NodeJS application so it can be deployed. You will also learn how to provision all the AWS Elastic beanstalk resources you created in Module 2 using AWS CDK.

What You Will Learn

  • Package your NodeJS application
  • Build and deploy your CDK application
  • Update the NodeJS application deployment

 Time to Complete

10 minutes

 Module Prereqs

  • AWS Account with administrator-level access**
  • Recommended browser: The latest version of Chrome or Firefox

[**]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

Implementation

Package your NodeJS application

In Module 2, you learned that we will be uploading the application in a zip file to S3 using the S3 Asset module from AWS CDK.

For that to happen, you need to create a zip file of your NodeJS application and place it in the root directory of your AWS CDK application.  Name the zip file "app.zip". To do this, run the following command:

zip -r ../app.zip ./*
gsg_build_elb_3

Bootstrap CDK in your account

If this is the first time you are using AWS CDK in this account, and in this region, you will need to bootstrap it. When deploying AWS CDK apps into an AWS account and region, CDK needs to provision resources that it needs to perfom deployments. These resources include an Amazon S3 bucket for storing the deployment files, and IAM roles that grant the needed permissions to perfom deployments. Provision these initial resources is called bootstrapping.

For bootstrapping your AWS account, and region, run

cdk bootstrap aws://ACCOUNT-NUMBER-1/REGION-1

This should look something like this:

cdk bootstrap aws://123456789012/us-east-1

You can get the account number from your AWS management console, and the region name from this list.

Build and deploy your CDK application

After you have packaged your NodeJS application and placed it in the root of your CDK application directory and then bootstrapped your AWS account and region, you are ready to build and deploy your CDK application.

The first step is to build the CDK application.

node run build

If there are no errors in your application, this will succeed and now you can deploy the CDK application in the cloud.

cdk deploy

It will first ask you to confirm that you want to do some changes in your account security level, because we created a new role.

gsg_build_elb_4

Respond with "y", and then the deployment will start. It takes a few minutes to complete. When it is done, you will receive a message of the ARN (Amazon Resource Name) of the CloudFormation stack that this deployment created for you.

If you open the CloudFormation Management console, you will see that there are 2 new stacks there.

gsg_build_elb_5

The stack called CdkEbInfraStack is the one that contains all the Elastic Beanstalk resources we created in the previous module: Elastic Beanstalk application, application version, instance profile, and environment.

The other stack (with the random string), was created by Elastic Beanstalk, and it contains all the resources the Elastic Beanstalk app needs to run - auto scaling groups, instances, Amazon CloudWatch alarms and metrics, load balancers, and security groups.

Viewing your application in the cloud

If you want to see your application deployed in the cloud, the first thing you need to do is to find the URL of the web app. You can find this URL by going to the Elastic Beanstalk service in the AWS console, and look for the environment called MyWebAppEnvironment.

gsg_build_elb_6

There you will see the URL. Click on it and the web app should launch.

Update the NodeJS application deployment

If you want to make a change to your web app, and you want to redeploy it to the cloud, you can follow these steps:

  • Make the change in the web app
  • Package it in a app.zip file
  • Place the app.zip file in the root directory of your CDK application
  • Build the CDK project - npm run build
  • Deploy the CDK project - cdk deploy

Now, you can verify that there is a new version of the Elastic Beanstalk app deployed. And if you visit the web app URL the new version will be deployed - this takes a bit of time, please keep an eye on the console to see when it completes.

gsg_build_elb_7

Common Mistakes

Zipping the application

If you get this error when your NodeJS application is uploaded to Elastic Beanstalk.

Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js.

One problem can be the way you are packaging your application. Remove the package-lock.json and node_modules directory and run this command inside the web app directory.

zip -r app.zip .

This will zip the files correctly for Elastic Beanstalk.

Node modules when packaging your app

You can package your node_modules directory with all your downloaded dependencies in the zip file of your web application.

When you have this directory, Elastic Beanstalk assumes that no dependencies need to be downloaded.

Read more on the instructions on handling NodeJS dependencies in Elastic Beanstalk packages.

Conclusion

In this guide, you learned how to package a NodeJS web application and deploy it with Elastic Beanstalk, and how to deploy all the infrastructure as a CDK application.

Up Next: Clean-up Resources

Let us know how we did.

Thank you for your feedback
We're glad this page helped you. Would you like to share additional details to help us continue to improve?
Close
Thank you for your feedback
We're sorry this page didn't help you. Would you like to share additional details to help us continue to improve?
Close