Apps Development, AWS, Cloud Computing

4 Mins Read

A Guide to Deploy a Node.js Web App on AWS: Part 2

Introduction

In the previous part of Node.js Web App deploy on AWS – Part 1, we went through the key features of Elastic Beanstalk. In this second part, we will learn about the permission to deploy the sample app into the environment.

Adding permissions to the instance environment

The application runs on one or more EC2 instances behind the load balancer, which helps serve HTTP requests from the internet. When it receives a request requiring AWS services, the application uses the permissions of the instance to run and access those services.

The application uses instances permission to write data into a DynamoDB table and notifications to an Amazon SNS topic with the SDK for JavaScript in Node.js.

And adding the managed policies to the default instance to grant permission to the EC2 instances in the environment permission for accessing the Amazon DynamoDB and Amazon SNS:

  • AmazonDynamoDBFullAccess
  • AmazonSNSFullAccess

To add different policies to our default instance profile, below are steps to be followed:

  • Open the Roles Page in the IAM console.
  • Choose aws-elasticbeanstalk-ec2-role.
  • Then, On the permissions tab, you need to choose Attach policies.
  • Now select the managed policy for the additional services that the application uses.
    g., AmazonDynamoDBFullAccess or AmazonSNSFullAccess

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Deploying the application

Now the environment is ready for the applications to be deployed and run. Now that we are ready, we can use our application or test the environment with a sample Node.js app from Github – Download the source bundle from Github: eb-node-express-sample-v1.1.zip

To deploy the source bundle

  1. Open the Elastic Beanstalk console, and in the Regions list, select the AWS Region.
  2. Now, in the navigation pane, we need to choose the name of the environment from the list.
  3. In the environment page, choose “Upload and deploy”.
  4. Then an On-screen dialog box will appear, use it to upload the source bundle.
  5. Choose ‘Deploy’ here.
  6. And when the deployment completes, we can choose the site URL to open the website in a new tab.

Create a DynamoDB table

To use an external DynamoDB table with an application running in Elastic Beanstalk, we first need to create a table in DynamoDB based on the application requirement for which you are using it. When we create a table outside of Elastic Beanstalk, it’s completely independent of Elastic Beanstalk environments and will not be terminated by Elastic Beanstalk.

To create the table, we need to follow the following setting:

  • Table Name – ‘nodejs-app-deploy-tutorial’
  • Primary Key – email
  • Primary Key Type – String

To create the DynamoDB table:

  • Open the Tables page in the DynamoDB management console
  • Then, Choose ‘Create table’
  • Enter the table name and the primary key
  • Choose the primary key type
  • Finally, choose ‘Create’

Updating the applications config files

The configuration files need to be updated in the application source to use the ‘nodejs-app-deploy-tutorial’ table instead of creating a new one.

Now, To upload the sample application to production

  1. We need to extract the application/project files from the source bundle:
  2. Open .ebextension/options.config and change the values for the following settings:
    • NewSignupEmail – email address.
    • STARTUP_SIGNUP_TABLEnodejs-app-deploy-tutorial

Example .ebextension/options.config

The above details configure the application to use the ‘nodejs-app-deploy-tutorial’ table instead of the one that is created by .ebextensions/create-dynamodb-table-config, and then sets the email address that Amazon SNS topic uses for notifications.

3. Now, we need to remove the .ebxtensions/create-dynamodb-table-config.
~/node-app-deploy-tutorial$ rm .ebxtensions/create-dynamodb-table-config

4. Then, create a source bundle from the modified code.

Now, let’s deploy the nodejs-app-deploying-tutorial.zip bundle to the environment.

To deploy the source bundle

  1. Open the Elastic Beanstalk console, and in the Regions list, select the AWS Region.
  2. Now, in the navigation pane, we need to choose the name of the environment from the list.
  3. In the environment page, choose “Upload and deploy”.
  4. Then an On-screen dialog box will appear, use it to upload the source bundle.
  5. Choose ‘Deploy’ here.
  6. And when the deployment completes, we can choose the site URL to open the website in a new tab.

When we deploy, Elastic Beanstalk runs the configuration updates in the Amazon SNS topic and deletes the Amazon DynamoDB table created when deploying the first version of the application. When we terminate the environment, the nodejs-app-deploy-tutorial table will also be deleted. This will let us perform blue/green deployments, modify configuration files, or take down our website without risking any data loss.
For final tests, we can open the site in the browser and verify that the form works as expected. Create a few entries and then check them into the Amazon DynamoDB to verify them.

Conclusion

After completing both blogs, you will understand the “Launch of Node.js web application using AWS Elastic Beanstalk and Amazon DynamoDB”. Deploy the web application with the help of Elastic management services along with the use of Amazon SNS.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Node.js and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. Can I customize the app after it is hosted on AWS?

ANS: – Most of the settings on the Elastic Beanstalk can be changed after launch. But still, a few things can’t be changed, e.g., Application Name, environment name, platform, and VPC assignments cannot be changed.

2. How do I scale my app in the event of more traffic?

ANS: – Configure your environment’s Auto Scaling group to launch more instances when traffic reaches a predefined threshold.

WRITTEN BY Guru Bhajan Singh

Guru Bhajan Singh is currently working as a Software Engineer - PHP at CloudThat and has 6+ years of experience in PHP. He holds a Master's degree in Computer Applications and enjoys coding, problem-solving, learning new things, and writing technical blogs.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!