Voiced by Amazon Polly |
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.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
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.
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
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
- Open the Elastic Beanstalk console, and in the Regions list, select the AWS Region.
- Now, in the navigation pane, we need to choose the name of the environment from the list.
- In the environment page, choose “Upload and deploy”.
- Then an On-screen dialog box will appear, use it to upload the source bundle.
- Choose ‘Deploy’ here.
- 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
- We need to extract the application/project files from the source bundle:
123~$ mkdir nodejs-app-deploy-tutorial~$ cd nodejs-app-deploy-tutorial~/node-app-deploy-tutorial$ unzip ~/Downloads/eb-node-express-sample-v1.0.zip
- Open .ebextension/options.config and change the values for the following settings:
- NewSignupEmail – email address.
- STARTUP_SIGNUP_TABLE – nodejs-app-deploy-tutorial
Example .ebextension/options.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
option_setting : aws:elasticbeanstalk:customoption: NewSignupEmail: you@emaple.com aws:elasticbeanstalk:application:environment: Theme: “flatly” AWS_REGION: '`{"Ref" : "AWS::Region"}`' STARTUP_SIGNUP_TABLE: nodejs-app-deploy-tutorial NEW_SIGNUP_TOPIC: '`{"Ref" : "NewSignupTopic"}`' aws:elasticbeanstalk:container:nodejs: ProxyServer: nginx aws:elasticbeanstalk:container:nodejs:staticfiles: /static: /static aws:autoscaling:asg: Cooldown: "120" aws:autoscaling:trigger: Unit: "Percent" Period: "1" BreachDuration: "2" UpperThreshold: "75" LowerThreshold: "30" MeasureName: "CPUUtilization" |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
~/nodejs-tutorial$ zip nodejs-tutorial.zip -r * .[^.]* adding: LICENSE (deflated 65%) adding: README.md (deflated 56%) adding: app.js (deflated 63%) adding: iam_policy.json (deflated 47%) adding: misc/ (stored 0%) adding: misc/theme-flow.png (deflated 1%) adding: npm-shrinkwrap.json (deflated 87%) adding: package.json (deflated 40%) adding: static/ (stored 0%) adding: static/bootstrap/ (stored 0%) adding: static/bootstrap/css/ (stored 0%) adding: static/bootstrap/css/jumbotron-narrow.css (deflated 59% adding: static/bootstrap/css/theme/ (stored 0%) adding: static/bootstrap/css/theme/united/ (stored 0%) adding: static/bootstrap/css/theme/united/bootstrap.css (deflated 86%) adding: static/bootstrap/css/theme/amelia/ (stored 0%) adding: static/bootstrap/css/theme/amelia/bootstrap.css (deflated 86%) adding: static/bootstrap/css/theme/slate/ (stored 0%) adding: static/bootstrap/css/theme/slate/bootstrap.css (deflated 87%) adding: static/bootstrap/css/theme/default/ (stored 0%) adding: static/bootstrap/css/theme/default/bootstrap.css (deflated 86%) adding: static/bootstrap/css/theme/flatly/ (stored 0%) adding: static/bootstrap/css/theme/flatly/bootstrap.css (deflated 86%) adding: static/bootstrap/LICENSE (deflated 65%) adding: static/bootstrap/js/ (stored 0%) adding: static/bootstrap/js/bootstrap.min.js (deflated 74%) adding: static/bootstrap/fonts/ (stored 0%) adding: static/bootstrap/fonts/glyphicons-halflings-regular.eot (deflated 1%) adding: static/bootstrap/fonts/glyphicons-halflings-regular.svg (deflated 73%) adding: static/bootstrap/fonts/glyphicons-halflings-regular.woff (deflated 1%) adding: static/bootstrap/fonts/glyphicons-halflings-regular.ttf (deflated 44%) adding: static/jquery/ (stored 0%) adding: static/jquery/jquery-1.11.3.min.js (deflated 65%) adding: static/jquery/MIT-LICENSE.txt (deflated 41%) adding: views/ (stored 0%) adding: views/index.ejs (deflated 67%) adding: .ebextensions/ (stored 0%) adding: .ebextensions/options.config (deflated 47%) adding: .ebextensions/create-sns-topic.config (deflated 56%) |
Now, let’s deploy the nodejs-app-deploying-tutorial.zip bundle to the environment.
To deploy the source bundle
- Open the Elastic Beanstalk console, and in the Regions list, select the AWS Region.
- Now, in the navigation pane, we need to choose the name of the environment from the list.
- In the environment page, choose “Upload and deploy”.
- Then an On-screen dialog box will appear, use it to upload the source bundle.
- Choose ‘Deploy’ here.
- 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
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.
Click to Comment