AWS EC2 with Node.js and MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is aaron white in this tutorial i'm going to show you how to launch an ec2 instance in aws install node and and run a web server so to get started you need an aws account everything i do throughout this tutorial will be free tier eligible provided that you're in that grace window so let's get started uh in the top type in ec2 navigate to the ec2 console and launch an instance so your screens might change but the functionality will be pretty much the same we're going to select an amazon linux 2 ami that's free tier eligible and we'll just use a t2 micro that should be fine as long as it says free tier eligible that's probably what you're more concerned with we don't need to change any of the instance security or instance configuration details i don't need tags but what i do need to do is open up port 80 so access to the server can be gained from the internet so what i'll do is call this ec2 node mongodb and i'll say the same for the description and then i'll add a role on port 80 and that can come from anywhere so we want to say ssh here http here so uh this video is really tutorial purposes is educational this is to tinker around with an ec2 instance and get stuff up and running but this is by no means a best practice uh you don't want to expose ssh on an ac2 instance to the world typically what you want to do is have a load balancer in front of an ec2 instance and that ec2 instance is inside of a private subnet so nobody can access it except for for you and and other authorized people so i keep that in mind as we go through this so i'm going to launch this and it's going to tell me that i don't have a key pair to where i can ssh into it so i will create a new key pair and since i'm in the u.s west 2 region you can see it up here and these keys are region locked us west 2 that's what i'll do let's create it there i'll download it and launch the instance now i realize i did that a little bit fast but it just goes into your download folder and then i'm dragging it into this ec2 folder that i have on my computer so all the code for this tutorial and all the snippets i used they'll be available on my github repository here you can access that github.com aaronwht okay so it's going to take it just a few seconds to get this thing going and if i pull it up i'll get an ip address but i won't be able to connect to it just yet and i just want to pause for a second and wait for that to become available because you have to do some permissions to that pem file that i just downloaded in and moved so i look at it here it's uswest2.pem it doesn't have the correct permissions to ssh into my ec2 instance so if i open up terminal i go right here uh and and i try and run the commands to do that it will give me an error so let me just do a refresh up here i want to demonstrate this connect functionality and we want to go under ssh here and it will give us this command to modify the permissions on our file so we'll do that we'll just paste it verbatim right into here you'll probably get a request for your password for your computer go ahead and enter that in and that will give it the proper permissions and now it's telling you how to ssh into your ec2 instance so i'll just copy and paste that and now it's going to alert me that this isn't associated with my computer they can't validate the authenticity of that ip address but we can so i'll say yes and now i'm remoted into my ec2 instance so the first thing i want to do is an update to yum so i'll say sudo yum update and i'm just going to tack on this hyphen y and that way it won't ask me any questions it'll just go do everything and there are 67 packages but that goes relatively quickly here so again uh everything that i'm going to be using code wise is in this repository there are different mongodb instances and you can go to mongodb.com to look at them the one you want to use is this installation on amazon we'll create a file to do this and and i'll walk you through that whole process so let's see if that's done and it is so now that i have that i can actually use yum to install uh node version manager or actually what we can do is um curl and get it so i'll i'll do that here in just one second the first thing i want to do is show you that it is not installed right now so nvm node version manager not installed so what we want to do is use curl to go out and go get node version manager so that's the code you can either pause the video or look at my github to grab this so it went out and grabbed that and so now if i do an nvm version i should be able to see the version but interestingly enough i can't so i'll have to exit out of my ec2 instance it says it's closed i'll just push up on my keyboard on my arrow on my keyboard to ssh right back into my ec2 instance and i'll push up a couple times now it gives me the version so for whatever reason you just have to exit out and come back in and you have access to nvm so i'll do a command k to clear my screen and now what i want to do is install node so i'll say nvm install node it's going to go out and get all the files to do that all right great so now i have the ability to run a node application on my server um but the problem i'm going to run into are there a lot of permission issues with linux when you're trying to run applications on a port lower than 80. so you'll see here in my app.js file i'm running this on port 8000. the problem with that is that whenever i try and access the site by the ip address people won't know to put in colon 8000 to access that port so what i want to do is a redirect on the ip tables internally to this uh the cc2 instance so i'll say pseudo ip tables t and all i'm going to do is redirect any request from por 2 port 80 to port 8000 so we're going to say t nat uh capital a and you can look up what all this does i'm not going to explain it i'm just here to help you get this working tcp i always type that wrong now there are two hyphens here and you got to do d port space 80. hyphen j capital redirect 2 hyphens 2 ports 8 000. i know that's a lot of typing you can pause the video zoom in or it's on github run it okay so now i'm ready to upload my file and run it so let me go back and get this ip address copy that ip address and i'm going to use a software called uh filezilla here and what i can do in filezilla get it up and running is i can connect and delete that that was a previous instance we'll create a new site in here i'll put the host name and i'll change this to sftp my login type it's not going to be normal let's just use that key file that we're already using so i'll just select that and then i have to type in this user ec2 user and i should be able to connect and what i'll do after i say okay i can acknowledge that is upload my app js file just right mouse click and say upload now on my ec2 instance i should be able to do an ls and the files there so we've uploaded the file to the server and now i should be able to say node app and the app should run so uh if i go here and i paste in the ip address i should get a message that says success so that's pretty terrific we we're running this we're not doing anything with the database yet so that's our next step so i'll hit ctrl c to exit out of the node app and now what i need to do is go out and get installed but to do that i have to set up a couple things here so the first one is i have to create a file so i'll say cd into etc yum dot repos d dot repos dot d okay and now here what i have to do is create a file so sudo touch and the file name is org and we'll call it 5.0 repo dot repo okay so uh yours might be different but this should work for you and now we have to use uh vim to actually edit this file and paste in the content so i'm going to do sudo sue elevates my permissions to super user and i'm going to do vi if i just type in the m and hit tab it should bring me the file okay cool so now what i can do is copy and paste this code in i'll make it a little bit bigger just so you can see it again this will be in the github repo and now this gets confusing you have to hit escape and then colon and then wq exclamation mark uh for it to write all the contents of that into the file and now if i do cat there it is it will give me all the information so now that we're good with that what i should be able to do is a yum install y mongodb org and basically what we're doing if i if i show you this under the mongodb installation on amazon they have a little bit of code that's all i did i just grabbed it from here and it walks you through it so this is terrific they're they're trying to help you so now that i have that i should be able to install it's going to go out and get all those packages install it i'm going to shrink this just a little bit it'll just take a minute for this to go and while that's going on i'm just going to go in and upload the rest of these files this file the server file uh and then this build file because i'll forget if i don't do it so i'll just upload them all and that should be completing very soon so now the next step is we have to start the bongo service and to do that you have to make a couple folders so if i just try and start the service it needs two folders to operate one is uh the data folder so we'll do make dir data and i just change directories into my root folder and now i'll do a cd data and then i'll do a make dir db okay so now i can go back to home dc to user this is where all our files are and i should now theoretically have everything needed to run on this so what i'll do is say sudo actually i don't need to say sudo service d start and it looks like mongo's up and running so now what i want to do is uh to work with my application here so we have this running the next file we'll want to run is this file so in order to do that i need to do an npm install of mongoose and i'm going to also install express okay so i have to get out of root i have to go back just the regular user so let's try this again mongoose and express and it will install those packages for me i should it's saying permission denied okay let's try pseudo npm install long goose and express okay sudo sue npm install mongoose we'll try one at a time here okay so just doesn't want to work for me uh and sometimes that happens so let's try one more time okay so it's saying permission denied uh to it's because i'm in the wrong directory so i need to go back to uh what is that home vc2 user yeah i totally didn't do that right so now i can do npm install long goose and express and it should work fine okay yep you gotta be in the right directories i'm also just for brevity's sake going to install pm2 and i'm going to do that globally so pm2 will restart our node service in case of anything happens or you change the file a lot of different packages to use to do that npm or a pm2 will work fine for that okay so now uh we should be pretty much ready to go i should be able to say and now i have to say use mern so why mern well mern is the collection or the database that i'm using in my code codebase here so typically you know you wouldn't hard code this stuff you'd use a dot emv file but i didn't okay so uh db create user make sure you get that capital uh and i have to specify it as user i'm referring to some notes over here my user which is right up here and then pwd is my pwd and very important here we have to say roles db owner okay so now we have that that access to that user so let's exit out of this exit now let's do node it seems to work but it will probably not work right here i'll probably get in okay no members found right because we didn't add any okay so ctrl c now we'll go back into and now we'll have to say use mern again and now we'll do db dot insert actually it's db members dot insert and we'll do a first name and we'll just call it aaron last name we'll call him white and should insert that user so now if i could go back here and refresh it should be there let's add one more so i'm just going to use basically the same thing i'm just using option and then my arrow keys to tab through this i'll do bob smith okay and should insert him hit exit node okay that's really annoying already right but here we go we got we got bob we got aaron so it created the records in so you're actually up and running but this is really really annoying how we're doing this so this is where pm2 comes into play so the last file we want to use here is the server.js file and this will use an index in the build so imagine you have a react application you do a build you just drop those files they're probably already in there into your build folder here and i'm just using axios to go into the server and serve out the members it's just getting everybody and it's writing them out and it's using express here to do that basically so uh actually pretty simple you could connect concatenate these if you wanted to or however you want to to write out the files but we just want to get something up and running so this will do it for us and what we'll do is say pm2 start server sudo sue pm2 start server okay let's try that again server.js i can't remember the command okay it's got to be what is it p.m to start server.js that's got to be it okay so now what's that what that is doing in the background it's it's starting node running server.js and if i come in and refresh this i should get a couple names here and if you look at the file the index file is being loaded so it's running this in the background what's great about this is i can exit out of my server now and i can refresh and still running so before when we're doing node space or node space server you can't exit out of the ec2 instance you guys start using various tooling and things like that so pm2 makes that pretty easy so let's ssh back into it and we'll go to again because that manga service is still running and now i'll say use mirn and i'll say uh db dot insert members dot insert insert first name joe last name smith all right let's see if we just refresh joe should show up okay so you've got uh an ec2 instance running using node and you have all the commands here and you can access my repo to grab whatever you want from it and everything should be working so if you got this far congratulations again this is educational purposes only i wouldn't recommend using this for production other than tinkering around with an ec2 instance
Info
Channel: Aaron White
Views: 848
Rating: undefined out of 5
Keywords: AWS, EC2, Node, MongoDB, Mongo, Node.js, aws, ec2, node, mongodb, mongo, node.js, vm
Id: 7vf210p2tJg
Channel Id: undefined
Length: 18min 13sec (1093 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.