{"id":753,"date":"2020-12-31T03:24:27","date_gmt":"2020-12-31T02:24:27","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=753"},"modified":"2021-01-21T09:37:54","modified_gmt":"2021-01-21T08:37:54","slug":"aws-how-to-deploy-angular-with-nodejs-app-on-elastic-beanstalk","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2020\/12\/31\/aws-how-to-deploy-angular-with-nodejs-app-on-elastic-beanstalk\/","title":{"rendered":"AWS \u2014 How to Deploy Angular With NodeJS App On Elastic Beanstalk"},"content":{"rendered":"\n<p>AWS provides more than 100 services and it\u2019s very important to know which service you should select for your needs. If you want to deploy an application quickly without any worry about the underlying infrastructure, AWS Elastic Beanstalk is the answer. Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.<\/p>\n\n\n\n<p>In this post, we are going to deploy Angular application with nodejs environment. There are other technologies or environments that AWS supports such as Go, Java, NodeJS, .Net, etc.<\/p>\n\n\n\n<h4>Introduction<\/h4>\n\n\n\n<p id=\"a176\">If you want to deploy an application without worrying about the underlying infrastructure, Elastic Beanstalk is the solution. When you build the app and upload the app in the form of zip or war, Elastic Beanstalk would take care of provisioning underlying infrastructure such as a fleet of EC2 instances, auto calling groups, monitoring, etc.<\/p>\n\n\n\n<p id=\"6c61\">The infrastructure provisioned by Elastic Beanstalk depends on the technology chosen while uploading your app. For example, we are going to deploy the Angular with NodeJS backend on Elastic Beanstalk so we need to choose the NodeJS environment.&nbsp;<a href=\"https:\/\/docs.aws.amazon.com\/elastic-beanstalk\/?id=docs_gateway\">If you want to know more about Elastic Beanstalk here is the link.<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/miro.medium.com\/max\/1754\/1*6Hw9jIN9qCa-U9ocZ4XlgA.png\" alt=\"Image for post\" width=\"371\" height=\"148\"\/><\/figure>\n\n\n\n<p>As you see in the above figure, we build our project and create a zip. Once we build the zip, we upload that zip on the Elastic Beanstalk environment. If you have a custom domain you can point that to the elastic beanstalk URL so that your app can be accessible to the public through that URL.<\/p>\n\n\n\n<h4>Example Project<\/h4>\n\n\n\n<p>This is a simple project which demonstrates developing and running Angular application with NodeJS. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/miro.medium.com\/max\/1320\/0*HncvE4Qb5VEJqnH7.gif\" alt=\"Image for post\" width=\"379\" height=\"220\"\/><figcaption>here is a related link (Article) to this project : <a href=\"https:\/\/nguenkam.com\/blog\/index.php\/2020\/11\/17\/how-to-develop-and-build-angular-app-with-nodejs\/\" data-type=\"post\" data-id=\"710\">How To Develop and Build Angular App With NodeJS<\/a><\/figcaption><\/figure>\n\n\n\n<h4>Prerequisites<\/h4>\n\n\n\n<ul><li>AWS account setup: AWS offers a free tier for one year&nbsp;<a href=\"https:\/\/portal.aws.amazon.com\/billing\/signup?redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#\/start\">here is the link to set it up.<\/a><\/li><li>Once you set it up you have a root account. It&#8217;s not a best practice to use your root account to do any tasks instead you should create an IAM group that has permissions to deploy on Elastic Beanstalk and add a user to it and log in with that user.<\/li><li><a href=\"https:\/\/gulpjs.com\/\">Gulp for building the project and zip it.<\/a><\/li><\/ul>\n\n\n\n<h4>Build the Project<\/h4>\n\n\n\n<p id=\"24a4\">Firstly,we have to build the project and get it ready for the deployment in the Elastic Beanstalk environment. We are using&nbsp;<a href=\"https:\/\/gulpjs.com\/\">gulp<\/a>&nbsp;to automate building the project and these are the following steps we should automate with a&nbsp;<a href=\"https:\/\/gulpjs.com\/\">gulp<\/a>.<\/p>\n\n\n\n<ul><li>Clean the directory if exists<\/li><li>Create a directory if not exists to put all the production build<\/li><li>Build Angular code with ng build<\/li><li>Place the Angular code into production directory<\/li><li>Build the server code with the webpack<\/li><li>Place the server code into production directory<\/li><li>Finally, zip all the code.<\/li><\/ul>\n\n\n\n<p id=\"50a0\">Here is the file gulpfile.js in which we created tasks for all the above points.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { src, dest, series, parallel } = require('gulp');\nconst del = require('del');\nconst fs   = require('fs');\nconst zip = require('gulp-zip');\nconst log = require('fancy-log');\nvar exec = require('child_process').exec;\n\nconst paths = {\n  prod_build: 'prod-build',\n  server_file_name: 'server.bundle.js',\n  angular_src: 'my-app\/dist\/**\/*',\n  angular_dist: 'prod-build\/my-app\/dist',\n  zipped_file_name: 'angular-nodejs.zip'\n};\n\nfunction clean()  {\n  log('removing the old files in the directory')\n  return del('prod-build\/**', {force:true});\n}\n\nfunction createProdBuildFolder() {\n\n  const dir = paths.prod_build;\n  log(`Creating the folder if not exist  ${dir}`)\n  if(!fs.existsSync(dir)) {\n    fs.mkdirSync(dir);\n    log('?  folder created:', dir);\n  }\n\n  return Promise.resolve('the value is ignored');\n}\n\nfunction buildAngularCodeTask(cb) {\n  log('building Angular code into the directory')\n  return exec('cd my-app &amp;&amp; npm run build', function (err, stdout, stderr) {\n    log(stdout);\n    log(stderr);\n    cb(err);\n  })\n}\n\nfunction copyAngularCodeTask() {\n  log('copying Angular code into the directory')\n  return src(`${paths.angular_src}`)\n        .pipe(dest(`${paths.angular_dist}`));\n}\n\nfunction copyNodeJSCodeTask() {\n  log('building and copying server code into the directory')\n  return src(&#91;'package.json', 'server.js'])\n        .pipe(dest(`${paths.prod_build}`))\n}\n\nfunction zippingTask() {\n  log('zipping the code ')\n  return src(`${paths.prod_build}\/**`)\n      .pipe(zip(`${paths.zipped_file_name}`))\n      .pipe(dest(`${paths.prod_build}`))\n}\n\nexports.default = series(\n  clean,\n  createProdBuildFolder,\n  buildAngularCodeTask,\n  parallel(copyAngularCodeTask, copyNodeJSCodeTask),\n  zippingTask\n);<\/code><\/pre>\n\n\n\n<p>All we need to run this command&nbsp;<code>gulp<\/code>&nbsp;at the root of the application and it creates the final zip file under the&nbsp;<strong>prod-build<\/strong>&nbsp;directory.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1201\/1*gedni9QlVF4BT-6J6LcqIg.png\" alt=\"Image for post\"\/><figcaption><strong>zip created under the prod-build directory<\/strong><\/figcaption><\/figure>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">Deploy on Elastic Beanstalk<\/span><\/h4>\n\n\n\n<p id=\"1789\">Once you have the zip file ready it\u2019s time to deploy this zip file to Elastic Beanstalk environment. First, we need to create an IAM user which has access and permissions to deploy on an Elastic Beanstalk environment.<\/p>\n\n\n\n<p id=\"b95f\">I created an IAM user called&nbsp;<strong>developer1<\/strong>&nbsp;and added to the group&nbsp;<strong>WebDevelopers<\/strong>&nbsp;which has permission&nbsp;<strong>AWSElasticBeanstalkFullAccess&nbsp;<\/strong>and<strong>&nbsp;AmazonRoute53FullAccess.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1724\/1*IEoTkE8w-1rcW9ws7R6qUw.png\" alt=\"Image for post\"\/><figcaption><strong>IAM user<\/strong><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1695\/1*9IMrOsldmc1lizoH0u7NkA.png\" alt=\"Image for post\"\/><figcaption><strong>IAM group<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Let\u2019s login to AWS console with that IAM user and you can go to Elastic Beanstalk and create an application. Since it\u2019s nodejs project we have to choose a platform as Node.js and upload the zip archive that we have created in the above step.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2013\/1*AafO8nqJUI92rvNb99ZkVw.png\" alt=\"Image for post\"\/><\/figure>\n\n\n\n<p>Once you click on the create application it will take a few minutes to set up the environment. Once it is deployed and if everything goes well you can see the following screen.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2765\/1*FnST8vQiZrsR2G3r7hCzQw.png\" alt=\"Image for post\"\/><figcaption><strong>Successful Deployment<\/strong><\/figcaption><\/figure>\n\n\n\n<p>You can see the URL for this application on the top. Just copy that URL and hit it in the browser and you can see your app running on AWS.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2350\/1*LrpVMTAq25PZ7EyoNzR53g.png\" alt=\"Image for post\"\/><figcaption><strong>Accessing your application through URL<\/strong><\/figcaption><\/figure>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">Debugging and Update the Deployment<\/span><\/h4>\n\n\n\n<h4 id=\"f7c0\">Debugging<\/h4>\n\n\n\n<p id=\"020f\">When you deploy the application sometimes it might go wrong. Let\u2019s how we can debug the deployment. First, if something goes wrong you can see the deployment environment as red.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1907\/1*CmLoaJbomfx6B1y2WORnkg.png\" alt=\"Image for post\"\/><\/figure>\n\n\n\n<p id=\"7bd0\">If you can click on that you will be redirected to a details page with the status Degraded. We can see what is the error here it is actually missing package.json in the zip archive.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2767\/1*CPUpOb8Lv3Vs8yLQHrEl_A.png\" alt=\"Image for post\"\/><\/figure>\n\n\n\n<p id=\"0108\">In this scenario, we caught the error on the details page itself. But, this is not the case all the time. We can request logs to go into more details about the application.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2762\/1*e-HWZDUSj3qQvt0lFbFSOA.png\" alt=\"Image for post\"\/><\/figure>\n\n\n\n<h4 id=\"d8ad\">Update the deployment<\/h4>\n\n\n\n<p id=\"ad54\">Updating the deployment is very easy all you need to do is upload the new zip file with the version label so that you can go back to the versions later. For example, if something goes wrong and not working properly with the latest deployment you can always go back to a specific version.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2317\/1*kS0TzVxI0uXVQuqKMWpvWg.png\" alt=\"Image for post\"\/><figcaption><strong>update and deploy<\/strong><\/figcaption><\/figure>\n\n\n\n<p>You can actually see the versions here and you can select and deploy a specific version.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/2818\/1*89S9q_j7p4Ga-4hhHeeyYw.png\" alt=\"Image for post\"\/><\/figure>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-green-cyan-color\">Things To Consider<\/span><\/h4>\n\n\n\n<p id=\"ac58\">I just want to mention some points here which anybody can encounter these and saves you a lot of time. You can always change these settings in the configuration section.<\/p>\n\n\n\n<h4 id=\"c85a\">Zipping<\/h4>\n\n\n\n<p id=\"eaea\">When you are zipping all the files make sure you zip the files, not the entire folder. We tend to right-click on the folder and compress it but you should select all the files inside that folder and compress it.<\/p>\n\n\n\n<h4 id=\"6926\"><strong>Package.json<\/strong><\/h4>\n\n\n\n<p id=\"dcec\">Make sure you have a package.json file in that archive. As soon as you upload the zip file elastic beanstalk unzips it and looks for the package.json to install any dependencies.<\/p>\n\n\n\n<h4 id=\"25f9\">start command in the package.json<\/h4>\n\n\n\n<p id=\"3878\">Make sure you have a start command in the package.json and it is pointing to the right file.<\/p>\n\n\n\n<h4 id=\"7165\">Express port<\/h4>\n\n\n\n<p id=\"dac7\">On the NodeJS server make sure you are listening on the port 8080. You can configure this port by using the PORT environment variable in the application code like this&nbsp;<code>process.env.PORT.<\/code>&nbsp;<a href=\"https:\/\/docs.aws.amazon.com\/elasticbeanstalk\/latest\/dg\/nodejs-platform-proxy.html\">Check out this link for Configuring the proxy server<\/a><\/p>\n\n\n\n<h4 id=\"542f\"><span class=\"has-inline-color has-vivid-green-cyan-color\">Summary<\/span><\/h4>\n\n\n\n<ul><li>If you want to deploy an application quickly without any worry about the underlying infrastructure, AWS Elastic Beanstalk is the answer.<\/li><li>Elastic Beanstalk would take care of provisioning underlying infrastructures such as a fleet of EC2 instances, auto calling groups, monitoring, etc. when you upload the zip or war file.<\/li><li>Once you create an AWS account, do not use root credentials. It\u2019s always best practice to create an IAM group with limited permissions and add a user to that group.<\/li><li>Use&nbsp;<a href=\"https:\/\/gulpjs.com\/\">gulp<\/a>&nbsp;to build the project and create a zip archive for the upload.<\/li><li>Choose an appropriate platform while uploading your zip archive. It will take a few minutes to set up the Elastic Beanstalk environment.<\/li><li>You can debug the environment if something goes wrong on the details page or you can request logs.<\/li><li>You can update and deploy the environment with version labels. You can deploy any version by selecting that specific label.<\/li><li>Cleaning up the resources is very easy all you need is to terminate the deployment.<\/li><li>You need to have package.json in the zip archive and make sure the nodeJS server listens on the port 8080 or you can configure the port using environment variable PORT in the application code.<\/li><\/ul>\n\n\n\n<h4>Tuto-video <\/h4>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Node.js Deploy to Amazon Web Services (AWS) Tutorial (Elastic Beanstalk, Express, Git, CI\/CD)\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/b0g-FJ5Zbb8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h4 id=\"0fc5\">Conclusion<\/h4>\n\n\n\n<p id=\"2565\">Elastic Beanstalk is the right solution if you want to deploy web apps quickly without worrying about any underlying infrastructure.<\/p>\n\n\n\n<p><strong>reference :<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/medium.com\/\">https:\/\/medium.com\/<\/a><\/p>\n\n\n\n<p>                           <\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS provides more than 100 services and it\u2019s very important to know which service you should select for your needs. If you want to deploy an application quickly without any worry about the underlying infrastructure, AWS Elastic Beanstalk is the answer. Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":786,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,145],"tags":[183,149],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/753"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/753\/revisions\/785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/786"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}