Deploy a Web App on AWS Elastic Beanstalk

TUTORIAL

Module 3: Deploy Web Application

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

Overview

In this module, you will learn to package your Node.js application so it can be deployed. You will also learn how to provision all the AWS Elastic Beanstalk resources you created in the previous module using AWS CDK.

What you will accomplish

In this module, you will:
  • Package your Node.js application
  • Build and deploy your CDK application
  • Update the Node.js application deployment

 Time to complete

10 minutes

 Module prerequisites

  • 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 Node.js application

In the previous module of this tutorial, you learned that we will be uploading the application in a zip file to S3 using the S3 Assets module in the AWS CDK.

Before that happens, you need to create a zip file of the Node.js application you created in the first module of the tutorial, Build a Web Application, and place it in the root directory of your AWS CDK application under the directory cdk-eb-infra.

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

Bootstrap CDK in your account

If this is the first time you are using AWS CDK in this account, and in this AWS 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 perform deployments. These resources include an Amazon S3 bucket for storing the deployment files, and IAM roles that grant the needed permissions to perform deployments. Provisioning these initial resources is called bootstrapping.

To bootstrap your AWS account and Region, run the following:

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 the AWS Management Console, and the Region name from this list.

Build and deploy your CDK application

After you have packaged your Node.js application, placed it in the root of your CDK application directory, and 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.

npm run build

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

cdk deploy

Because we created a new role, you will be asked to confirm changes in your account security level.

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 containing 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 two new stacks there.

The stack called CdkEbInfraStack 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), which was created by Elastic Beanstalk, contains all the resources the Elastic Beanstalk app needs to run—autoscaling 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 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.

Choose the URL to launch the web app.

Update the Node.js application deployment

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

  • Make the change in the web app
  • Package it in an 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. If you visit the web app URL, the new version will be deployed. This takes a bit of time; the console will indicate when the new deployment is complete.

Common mistakes

Zipping the application

If you get the following error when your Node.js application is uploaded to Elastic Beanstalk, you will need to package your application differently.

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.

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 Node.js dependencies in Elastic Beanstalk packages.

Conclusion

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

Up Next: Clean Up Resources

Was this page helpful?