How to deploy a Node Express API to AWS EC2 instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome today we're gonna try to create a really really simplified um node API with Express and we're gonna deploy this node API into ec2 instance inside of AWS ec2 instance is basically like a virtual machine inside of AWS that allows you to run any code on those machines so we're gonna push our code in there so that it runs from there okay so but let's go ahead and get started let's create a really simple node API well so let's get started with like make directory and we can call it node Express cc2 and then we could go in there no Express ec2 for the cc2 and then we could create a couple of files like index.js and Dot get ignore and then we could do npm init Dash y so we could initialize I think basically create this package Json file and then we could um just in PMI Express and right away we could also just do a git init well not not yet let's just open it in code let's see what we got so far in the meantime you can go and start creating your um repository in GitHub so we could call it here like node Express um um ec2 and I'm gonna select me as the owner and make it public and we're going to create a repository what that's cooking we'll go ahead and go into our API here and make sure that I don't get ignored we add the node modules there and we do we could do a kit and it's now okay and then from here on the index.js we can go ahead and just import Express from Express but if you're gonna do importing you have to make sure that in your package Json that you do bring in the type module so that you could use the new es6 import like this okay so once we do that we could do just a cons and from this Express function create this app where now we could use the listen and we could listen to board um let's say 5001 and this gives us a call back and we could just say console log my browser there and say like API running on four five thousand one okay and then once we have that we can also use the app to do it gets and we're gonna just listen to that root route and the second parameter that it takes is a callback function where if we're going to do it in one line we don't need the curly so we could um use the rest I'm sorry rack and res here and for the for those the response we could send some Json and we could say like um my API um running and then just put a little happy face some little cool glasses not up here but inside there we go all right so if we run nodemon um like this it should run if you do not have Road node mod it's okay you could just he'll just do no dots dot because index.js is the root one okay so right now it's running and I'm gonna just hit it with um with this guy here for example um localhost not 3000 but 5 000 and and one and there we go my API is running okay so that's locally nice so now that we have um get it knit all the good stuff I'm gonna do a good and get commit um first commits and then I'm going to go ahead and just copy and paste this thing here basically that it sets up your remote origin and changes my Branch from Master to Main and then it pushes up into the main so if I refresh here my um code is up there really simple API nothing too fancy awesome so the next thing is let's get started inside of AWS if you haven't created an account just yet um go ahead and create one um it's free once you're inside of AWS what we want to do is find ec2 instance like this and we click on the ec2 instances there and then once we're there we could go ahead and start launching a new instance so let's click on launch instance like this and once we launch it um we could call it like um node Express ec2 just to keep it consistent um Amazon Linux no we're gonna select Ubuntu now this is the free tier eligible and um I'm just gonna keep the bear bear bare minimums uh T2 micro free tier that's perfect now this is important because um basically a key pair is a like a certificate that allows you to connect into your ec2 instance so let's go ahead and create one here real quick and we're gonna select pen and let's just say node um Express easy to instance like this RSA is fine keep it Pam and you want to make sure that you put it somewhere that you're going to remember where it is at all times Okay so I'm going to go ahead and um for right now I'm gonna put it in my desktop maybe no I'm gonna put it in my in my um I have like a search um folder here inside of my inside my user profile um folder and I downloaded the sir nice so now this is the certificate the certificate that's going to allow me to connect into my ec2 instance okay so once we do that um we also need to create a Security Group which let's not um worry about that for right now right now the only one that needs to be checked on is allow SSH traffic from anywhere so this will allow me to connect into my ec2 instance via SSH protocol okay so that's good we got um eight um gigabytes real volume that looks okay Advanced details yeah we're not gonna touch any advanced details for now and let's go ahead and click launch instance and let's see what happens there once that should be launching you should be able to um click on the instance itself here and then now while we're there it's still pending Okay so let's check out what options we have here for our ec2 instance so if you notice our ec2 instance right now is in the Virginia region okay make sure because if you create it in the high or anywhere else then you won't be able to find it so make sure you're in the East Virginia and it's run right now the instance is running and if we select it here we're able to see a lot of information about our instance right so a couple of things to note is that you have a free public IP address that you get um as soon as you launch an instance so this is really good because this is the unique address to my little server my little instance that we just created okay this is private you don't want to use this one unless you're inside of your VPC and you need to connect um through different ec2 instances okay so there's a lot of other information down here but those are the two things that I want you to know for right now now what we could do is Select our ec2 instance and from here we could try and um connecting so we could click and connect here okay and we click on connect it's going to prompt us to hear how do we want to connect and usually we want to connect the issh client okay before we connect to SSH clients I want to note that you have to follow instructions here so open your SSH client and basically open an SSH client meaning just opening your terminal just like this okay this bash terminal window is your SSH clients okay um now I want to know that this guy here this search certificate was dropped in my in my folder in my home folder then my from my home folder so the HD my hard drive users my home folder then I got some search in here okay and then the cert is stored in here okay so just remember that now um it says locate your private file keyword which we just downloaded we just located it just now now check this out it's asking the key used to launch is node Express pem well we downloaded that CER so not a pem so maybe he's a little off the instructions on here but you got to just make sure that um you always use the extension that you downloaded with doesn't matter if they told you it's a pem okay would you download is a CER okay so then it's asking you to change the um the admin rights to this particular file it's asking you to change it to 400. so let's do that so what we need to do is we need to um let's go back home real quick here and it's going to search because that's where I have my cert right and you can see it here uh this is my node Express ec2 search so here I need to change mod to 400 to that node expresso okay what that does is that it gives a particular rights that are needed for that okay now it's asking you to connect to your um instance using the public DNS yeah you could copy this right but this is a better one to use okay so let's do this one together so we're going to from within my search file folder what I could do now is I could go into SSH okay Dash I meaning use a certificate and I could use that node notice that I hit Tab and autocomplete for me okay Ubuntu and yeah that that I could copy and paste so um we could copy and paste this part here and now you Ubuntu but Ubuntu like this and then now it would allow me to go ahead and create um not create but Connect into my ec2 instance so you sure you wanna connecting yes let's do it primarily at it wonderful now this means that I'm already connected to my Ubuntu instance okay so this little machine here it's it's a um it's a um it's like a like a little virgin machine right it has nothing inside like if you look at it inside um it does not have much happening in here okay so what we need to do now is we need to install um we need to install curl okay curl you install it with this command sudo app get install curl okay and you can have I'll give you a link to my um medium on this where we now need to install um node okay and this is a way that we can install node and I'll mature link a um the medium article in this tutorial here okay so once we install that um it's fetch reading the packages working okay great the third one we need to copy and paste is the sudo app get installed node.js okay we run that and that installs um node.js for us and basically this is the um the medium article that I'm following here and I'll make sure I'll add that to there so then we need to install pm2 okay and I'll show you what team to um does later on but let's go ahead and just get that installed with that code there copy and paste into that Ubuntu and basically pm2 is going to keep our um API running at all times okay our API code all right so that's the idea all right cool so once we have that running there then um we have already node installed and we have version 16 for right now we have npm version also version 8.15 okay great so now we need to get a copy of that code so we could start running our our API right so that what we do is we could go into our um where's our node Express API and we could do like git remote Dash B and we could get that link right or you could get a link from your um from your GitHub right but inside of our Ubuntu so we can do now is say git um clone and we're cloning that um entire repo returning the repo now we have that repo inside of our ec2 instance so I could go inside and do a notice press I could see what's inside we have an index we have everything that we created locally now it's from GitHub now into Ubuntu so I could literally just do node index and it's going to give us a couple of items because it cannot find Express right so remember this is a brand new setup so we got to make sure that we do npm init not init but install Okay so it needs to install all those module packages so now if we do node dots now it's running report five thousand in one beautiful so for right now our API is running in our Ubuntu right but if we go into our our settings here of our um node Express API and I click on my address I'm going to copy this address I want to put it into a new tab here I go to it I'm going to get an error because it's not it's not we're not we're not hitting that API just yet okay well you said John that was going to run on import for 5001 well let's use that Port 5001 let's add that Port there and it's still not hitting it why well because we need some additional security that um is set up in ec2 instances here so we could go to security and under security we notice here that our inbound rules it only allows for Port 22 to go TCP allow from anywhere okay that's that's okay but now we need to um edit the security group and allow anybody to hit our API with Port 5001 okay that's the important part of it okay so in here we're gonna go into edit inbound rules and we're going to add another rule here and we're going to do custom TCP that sounds good I'm going to use 5001 and then from where from anywhere and then we save the rule once we save that rule then if we refresh this guy or that's where we go okay well let's get the IP address again let's go to ec2 and then from the instance running node Express we grab the public IP address we go to a browser and then we do 5001 and voila our API is now oh I gotta keep doing that it's now running in the ec2 instance in AWS again if you have any questions any doubts um please go through the medium or leave me a message leave me a comment if you like these kind of videos please just give it a thumbs up and I'll keep creating more for you guys all right hope you enjoy and good luck with your deployment
Info
Channel: JonathanSanchez
Views: 43,427
Rating: undefined out of 5
Keywords: Node, Express, Node Api, AWS EC2, AWS, Node Express API
Id: T-Pum2TraX4
Channel Id: undefined
Length: 18min 17sec (1097 seconds)
Published: Tue Sep 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.