Deploying a Scalable Node.js App on AWS: EC2, RDS, Load Balancers, and Route 53 for HTTPS!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone and welcome to the new course section now in this section uh we will learn about how we can move the project which we have created and Prisma to the production so till now we have done everything on the local host and all these things like creating the database locally and running the server locally now in this lecture we will move everything to the production and we'll see how we can configure the Prisma Migra the database tables all these things in a production environment so for this course like I'll be using AWS as a cloud provider so here is a diagram which depicts like what all services we are going to work with so let me explain you so here are a couple of users so consider them as end users so they will request to the load balancer so request can first go to the load balancer and that load balancer will be connected to some ec2 instances so if you're not aware about ec2 instances consider them as a computer in a cloud which will be running your nodejs application so they are just a cloud or maybe you can see it's like equivalent to some sort of VPS so it's kind of a computer which sits on a cloud and runs your ec2 instance or the node application sorry so once your uh control reaches to this ec2 instance it will process your request it will connect to the database which will again run on MySQL so we will be using RDS service of Amazon to spin the mys square in St and all these things will then return the response to the users so this is the bare minimum architecture which we are going to create in this course so uh I'm pretty excited for this course you will also learn a lot like how things are done in production how to spin up a machine and deploy your project inside the AWS so let's catch up inside the section now here is a quick disclaimer for the score section so the services that we are going to use inside AWS do incur some cost so please please follow the steps along with me so that you can get the minimum cost possible and if you are signing for the first time you also get some free tier so that you can try out the things on your own in the end of the course I will also teach you how to dispose all the resources so that you don't incur any extra cost and for other resources like if you do anything on your own you are the only one responsible for all the costs so please uh be very careful and spending any resources read the documentation read the pricing Plan before uh spinning any resources apart from the things that are mentioned inside the course and also again at the end of this course section I will teach you how to dispose or delete all the resources so that they don't generate any extra cost so here is the signin console for AWS now if you have already created account you can directly login into the account but for the first time users you have to click on this create a new account so so there are some pretty basic stuff which you have to perform and you have to some give some basic details so just register this as a root user email here you have to give your email ID and some account name maybe you can give your own name as well and in the next steps you will be ask what type of account do you need so choose personal there because you will be needing it for just for your personal ins not for any business or anything else so keep that's in mind uh I have already created my account but if you're creating it for the first time you also need some credit card or debit card so please keep that in mind you have to attach that credit card or debit card don't worry it will not cost you anything it will just charge 2 rupees or something else very bare minimum amount to verify that card but apart from that if you're signing for the first time there is generous F offered by AWS so you don't need to worry about that part but obviously in case you are spinning up any services on your own you have to check the documentation and pricing plans now here is my a account so if you will log in you will see this console page so what I have done is in my Aus account I have first this Route 53 service so if you don't see these all these things here you can click on search and type Route 53 I will give you bare minimum basic details of what each service do because this is not obviously the course of AWS so uh Route 53 is used for DNS management so here you can see that I have one hosted zone so so here I have added one domain which I have bought recently to Showcase all the things to you so I have added this domain so Route 53 is considered as a DNS Management Service of AWS so this is what all you need apart from this you can see that here is one more surface which is known as certificate manager so if you also don't see this just type certificate manager now certificate manager used to generate some SSL certificates for https so here also I have one SSL certificate so just click on this uh list certificates in the left hand section and it says you don't have any uh certificate so just switch the region I guess okay so the certificate is in the Frankfort region regions are also important in n because we will be spending our resources in one region so in our case I have this certificate in this Frankfurt region but I need this in North Virginia region the reason I'm using this region is because it uh first of all it is very fast and secondly the pricing for this region is pretty low so that's the reason I'm using this region so I will request another certificate here in this region as well so just click on request certificate and request a public certificate click next and then you will have to add a fully qualified domain name so just copy the domain name from here you can give another name to the certificate so I want to generate the certificate for this domain as well as subdomains for us so this is the subdomain so wildcard do the domain name so app do domain app. domain name api. domain name so all these things will be covered with this wild card so we use the DNS validation as a uh validation method and leave everything as default and click on request so once uh this is done you can click on this certificate ID and what we have to do is to perform the validations we have to create the record in Route 53 now as soon as you create the record and go back to the Route 53 and refresh this as of now you can see that we have three records now uh you can see that uh the C name record which it asked to create which isore 86 okay as of now it's not created I guess okay sorry uh the record value is underscore 7907 so this is the same thing uh if you click on this you can see that underscore 86 andore 7907 so this is this will be used for validation of the DNS so it will take some time for DNS validation but uh sometimes it pretty fast like you can see that it's already issued so now we have uh a certificate in this region as well the North Virginia region so we will be using this certificate for all our tasks so now everything is cleared like this is the basic setup that I have uh hosted Zone in like the domain in R 53 and a certificate as certificate in ACM or the certificate manager now in the next lecture we will see how we can spin the database now uh before creating the database we have to create the security group so for creating the security group navigate to the ac2 now for Security Group are used to define what all ports will be accessible for that instance so uh you will like understand once I will create that so in the left hand section you can scroll and inside the n network and security you can see the security groups so just click on it and here is one default Security Group which is provided by the ec2 Sorry jws by default so we need another one so just click on create Security Group and create security group name we will say e-commerce secur Security Group you can give name whatever you like and give the description Security Group for for eCommerce at and just leave the VPC as default now we have to create the inbound rules now inbound rules are the rules in which we Define all the ports that can be accessed from the outside word so just click on this first we need uh SSH because we need to SSH into the ec2 instance to perform all the commands and all these things so it can be from anywhere second it will be HTTP so we need HTTP access as well so ipv4 anywhere you can also restrict this to custom IP address for example uh you want only one person to SSH inside this cc2 instance so you can give the IP address of that individual but as of now we can uh give the anywhere apart from this we need https as well and it should also be from anywhere now we need the port for MySQL as well so 3306 it should also be from anywhere at last our uh app will be running on I guess 3,000 Port so we just need the custom TCP which will be of Port 3,000 and it will also be from anywhere and for the outbound roles uh we will leave it as it is so these are all the ports which will open for the outside work to access our instance so this is what all Security Group is so just click on create Security Group now our security group is created now we can create the database now for the database in the search type RDS which is relational database service click on RDS now once it's opened we want to create the database instance so as of now you can see that we have zero instance so click on the database instances and click on create database now we want the standard create we don't need the easy create because easy create in easy create like a lot of things are done by the AWS in the background but for our purpose we need some extra control over it uh the instance will be MySQL now you can choose the engine version or you can leave it default you can click the free tier now it is very important just click the free tier here else you will like incur some extra cost and just give the database identifier we will say eCommerce username will be admin and we will click on autogenerate password so it will generate the password automatically instance size will be T2 micro because we need the bare minimum instance for scaling purpose we can like switch the instance type anytime in the future 20gb is enough so we'll leave everything as by default uh connectivity will also leave it by default Public Access will say no because we don't want someone to access our database publicly we just need our ec2s to connect to the database so that's what it says so if you want to read you can read it here as well now for the security group we will like cancel this default and we will select this e-commerce security group that we have created in the previous uh lecture leave it as it is everything is added as it is and the additional configuration data database Port is also good 3306 we need the password authentication so it is also good in the additional configuration we now need to give the database name so first we'll say e-commerce so it is the initial database name retention period is one so it will create the backup of the database so that uh if something happens to a database it crashes so we have the backup of the data so it will retain every day so one is the day like on which it will retain the backup and we don't need anything else just keep everything as by default and click on create database you can say that here it says uh 750 hours of Amazon RS is free so you can AVN this for free per month now it will take some time to create it I will get back to you once it's created so so if you click it's still creating so once it's created you will see the end point and all these things here now you can see that the database creation is successful and we have this endo and the port here and if you will go above this uh you can also see this button which is view connection details so just click on it so from here we will copy all these things somewhere and we'll keep it safe so username is admin so go to the WhatsApp and just keep it safe somewhere so username is admin sorry password so just copy the password as well and URL so we will need all these things during the connection of the database so now close it and we have the database in place now now in the next lecture it spin a ec2 instance Now to create the ec2 instance go in the search bar and type ec2 and go to the ec2 service ec2 stands for elastic compute Cloud so it will give you a computer inside the cloud on which you can spin your on your resources so as of now you will see that we have zero running instances click on the instances and we don't have any running instance so click on this launch instance button so we'll name it as e-commerce and for the image we will select ubu and you can see that for all these images it's giving the free tier eligible or not so just click on this one give you 750 hours monthly of free and apart from this we will need this T2 micro as well which has this free tier for the keyer we need to create a keyp so just give a key pair name e-commerce and it we and leave everything as default and create the key pair now the keer is downloaded you have to keep this safe because we will use this keer to authenticate inside the easy to inst s using SSH and once everything is done Network set settings click on select existing Security Group and Security Group will be Commerce Security Group leave everything as default everything is fine I guess just review so we have this name we have a image 22.4 yeah everything is fine just click on launch instance so it will take couple of uh minutes to spin this instance so it's in pending state so when like it's uh in succeeded like the launch is succeeded we will SS into this E2 instance and we'll install nodejs and all these things so see you inside the next lecture now you can see that the E2 instance is running now let's go to the terminal and try to connect to this ec2 instance so you can see that I have this e-commerce PM file on my desktop so first in order to connect with this we have to change the permission of this e-commerce PM so we will say CH mode 400 and part to this file so just drag and drop here so we have now changed it now in order to connect to the ec2 instance we have to say SSH Das I and the path of this keyp file and then we will say the username of the ec2 instance so so username for all the ubu related instances ubu only ubu at the rate and then the IP address of that e to instance so if you scroll to the right you can see that we have the public IP V4 address so just copy this and paste it here so we will say yes now you can see that the instance is connected so you will see this here two atate IP so it means like you have SSH into the E2 insts now let's install the node on this E2 inst so we install this note like the AWS has given this beautiful documentation here as well so just go onto this link and follow all the steps so we have S into the E2 instance copy this command then it will install NVM so NVM is the node virtual version manager so it's used to manage multiple version of node so once NVM is installed we will save this so just copy all the steps and through NVM we will install the particular versions of N and we say NVM installed the latest version of nde so it installing the node and it's installing version 18.1 n.2 so it's installed I guess and now we will say no- V you can see that the node is installed and if we say npm DV so the npm is also installed so this is how you install node on E2 instance now in the next lecture we will pull the code from GitHub and we will try to install all the dependencies and connect to the database let's prepare this project for production so in the project go to the package. DS file and we will add an extra script here so the script will be built now build script will be used to build all our typescript code into the JavaScript so we will say npx TSC so we will use typescript compiler to build all the typescript now for that we need one more change so go into the TS config.js and here you can see this outd so it determines in which folder our compile JavaScript will be stored so change this so first uncomment this and change this to build so we will store all the JavaScript inside the buildt folder now go to the terminal and run the command and PM run build now it will take a little bit of time so and after that you can see that we will have a new folder here build which will store all the JavaScript so you can see that it's completed and it has all the JavaScript code now we will push this to GitHub but before that we want to exclude this build folder from uh the Version Control so we'll say build and we will exclude all the JavaScript files as well but I don't think so that's needed so let's keep it as it is so we'll use build because we will anyways generate this build folder again on uh like the server the E2 server so that's why we are not pushing it through GitHub so we'll say get add get commit and we'll say finished prodad setup and just say good push origin main so it's completed now we will go to the ac2 instance and we uh clone the repository there now in this video we will clone the GitHub code on the C2 instance so here is the repository you can find the link here so github.com nishma 17 Prisma resources so just copy the SSH URL from here go to the E2 instance we I have already SSH into the E2 instance so simply type get clone and the URLs so it was pretty fast just see do uh let's see if it in which directory is it so it's Prisma resources so I will say CD is my resources and you can see that we have all the source code here now in order to do this first we have to create the environment file as well so we will say Vim I will use the Vim editor do tnv now let's open the vs code and copy the template from there sorry yeah open the vs code and copy the template from there so the template is this we have database URL port and JWT secret so we will go into the insert Port by pressing I and we'll paste this now we have to uh replace uh couple of things here so JWT secret and Port is fine but we need to replace the database URL so the username is admin in our case we will copy the password from our Notes app so here is the password now we will replace the Local Host with the with the UR of the database so this is the URL and Port is 3306 which is fine the database name is also e-commerce and everything is fine for me but I think we have to change the username as well so this will work so we have this here sorry I think I have made a mistake oh my bad so I have edited in this file I have to edit this inside this so my bad so let's copy the password here from here again and go to the ec2 and we will replace the password so this is the password and we will replace Local Host with the database URL so the database URL just copy it from here Port is fine e-commerce is also fine schema is equal to public now I guess everything is fine so we will say Escape C and WQ which will write and quit so everything is there now we will build this so first you will say npm install here install all the dependencies now once this is done we will build the code using npm run build so you can see that it's installed now we will say npm run build so I guess it should be pretty fast okay so just list all the files you can see that we have a build directory so just go into the build directory and let's there okay and you can see that but we don't have any uh EnV file here okay so first uh let's run this so we will say node index.js so it says App working okay makes sense so I'm not sure if this EMV file is not listed or what so I guess it's not listed here now in the previous lecture we have built the source code now here uh we will try to run the migrations in the production so the only difference is the command is different for running the migration in Dev and production so in Dev we were using npx Prisma migrate Dev but for production we'll be using a different command and that will be npx Prisma migrate deploy so this command is used in the production environment now you can see that it's applying all the migration to the production database so the migration is completed now what you want to do is we want to install a separate package so as of now like we can run the code using node index.js but that's not ideal in the production we will be using pm2 which is the node like manager for running the node app in the production so go to the Google and just type pm2 flot so you can see that here we have process manager for not JS application so it's the advanced production process manager for not JS application so let's copy this command and install this so it's installed globally and to run the nodejs application we will be using pm2 and we will say build index.js and you can see that it's saying command not found okay I suppose pm2 I guess it's pm to start yeah now it's working so it's not just pm2 IND index.js it's pm2 start build index.js now you can see that it's online and if you go to pm2 logs uh you can see that it's saying app is working now just for test purpose I'm not sure if it will work or not without https go to this IP address and and go on Port 3,000 you can see that cannot get if I access any random route you can see that it says cannot get/ me all our apis are deployed on/ API prefix okay it is still saying cannot get me I'm not sure what's the route for me is it all okay now it's saying unauthorized so this confirms that our application is working now you can see that it's not secured because it's running on HTTP now in the next lectures we will see how we can move this to https which will involve the use of load balancers so let's see you in the next lectures now in order to move the app to https we will be proceeding with that we will be creating load balancers and Target groups so first refresh this and you can see that we have one instance running now for that first let's create a Target group so inside the load balancing you can see this heading as Target group just click on this so we will create a Target group and it will be for instance Target group name will be Ecommerce Port will be 3,000 since our app is running on 3,000 so just change this it will be ipv4 keep everything as default and uh yeah everything is defa and click on next so the port is 3,000 and here we want to register the instance so we will register our instance on which the app is running so click on include as spending below now it says our app is running on Port 3,000 on this instance so just click on create Target group so Target group simply means if request comes it will go simply on Port 3,000 on that particular instance so this is what Target group is all about so now you can see that our Target group is live now let's go ahead and create the load balancer so click on the load balancer we will open it and a new tab create load balancer we will be creating an application load balancer give the load balancer name so it will be e-commerce it will be internet facing it will be ipv4 leave everything as default now here are the uh availability zones so if you go to the E2 instance uh there is an availability zone for each ec2 instance so you can see that our ec2 instance is running in Us East 1B so we have to select that here so USD 1B is compulsory and you can select any other as well sub so the one in which ec2 instance is running that one is compulsory but you can choose anyone apart from that so for the security group we will cancel this default and use our own Security Group Port is Port will be https and we will forward our request to this target group so everything is fine yeah and we will now attach a https certificate so we have created one for this domain in ACM in the first lecture you have seen so we will use this here because without uh SSL certificate https won't work so that's why we have to do this either you can use from ACM or you can import your own certificates as well everything is fine so just click on create load balancer now load balancer takes uh a bit of time to spin up so I'll see you once that's finished now you can see that our load balancer is active now so if you click on this load balancer uh you will see this load balancer are and all these things and here you can see the DNS name so just copy this DNS name from here place it here it's still saying the site can't be reached and go to the advanced and proceed to e-commerce so you can see that our load balancer is working so it says cannot get slash so it means our nodejs application is working now in the next lecture we will see how we can use our custom domain to point to this load balancer so that we can directly use our domain so we will do that using K 53 so let's catch up inside this inside that lecture now let's see how we can set up Route 53 to use the custom domain so go on Route 53 open it in a separate tab now we will go to the hosted zone so we will go to our domain which we have added here now here we will create a record so this is how we can manage all the domains inside the Route 53 so here you will say it will be an a cord so check mark the Alas and here we will see on which service we want to point this domain so we want to point this on alas to application and classic load balancer so we will select this because ours is application load balancer we have to select the regions so our Is Us East one and here we will select the load balancer so our e-commerce load balancer is listed here so just select this and click on create record now this DNS will take a bit of time to propagate now after creating this if you go to The Domain so our domain name is this so just copy this and paste this here now you can see that it's saying cannot get slashed now it's protected as well so it's running on https and it's running on the domain name so that's how everything works so you have seen how we can deploy the njs application on the production and move it to https using the custom convening now that's it we will be learning inside this section now in the next lecture I'll will see how you can dispose all the resources so that you don't get any additional cost now our app is running successfully on the domain so we will start with the cleanup so first go to the Route 53 and we will clean all the resources here so we have created this additional one so click here and you will see select delete record and we'll say delete so the record set is deleted from Route 53 now go to the load balancer and click on the load balancers here and we will say delete load balancer type confirm and say delete now load balance is successfully deleted go to the Target groups select this one and we will say delete Target group is also deleted now go to the instances click on the instance and in the instance State you can see we have this terminate instance option so just click on this and it will terminate the instance and same thing we have to do for the RDS as well so go to the RDS s database service click on the DB instances select the e-commerce one and in the actions we will say delete and delete the sance so this is how you can do all these things so it will take couple of seconds or minutes to delete this instance database instance and just some time to terminate this instance as well so this is how you like delete all the resources from here so please follow this instructions and delete all the resources else you will be responsible for our own cause so that's it uh the the thing like we studed in this section are pretty useful and will be useful in your career as well so you have learned how to deploy GS application on AWS how to move it to https how to use database and all these things so yeah hope you enjoyed the section
Info
Channel: Evoqys
Views: 1,565
Rating: undefined out of 5
Keywords: prisma, prisma client, prisma orm, orm, nodejs, typescript, javascript, development, mysql orm, mysql, migrations, prisma studio, programming, backend, database, raw, queries, enhanced search, searching, aws, aws deployment, node js deployment, ec2, rds, load balancers, https, route 53, amazon web services, production deployment, production, deployment, DevOps, EC2, RDS, LoadBalancers, Route53, HTTPS, DeploymentTutorial, AWSDeployment, AWSBestPractices, CodeDeployment, ScalableApps
Id: GMDFDl7oM7I
Channel Id: undefined
Length: 38min 6sec (2286 seconds)
Published: Fri Feb 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.