Deploy Node app on AWS EC2 Amazon Linux 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and i get a random lord of the rings character fantastic that worked first try that doesn't always happen [Music] in this video i'm going to show you how to deploy a node application on an ec2 instance running amazon linux 2. i've already logged into my amazon web console i'm going to start by selecting ec2 so that we can create a brand new ec2 instance so let's launch an instance i'm going to select amazon linux 2 i'm just going to select the t2 micro because it's part of the free tier and we're going to configure the instance details we're going to leave everything as the default we're going to leave all of the default settings you can change these if you want but all of these are fine for now uh adding storage the default amount of storage is fine i don't need any tags the security group always important this is the firewall so right now port 22 is open so i can connect to this instance using ssh i'm going to be deploying a node application on this ec2 instance that is going to be running on port 8080. so in order to connect to this instance i'm going to need to add a rule for port 8080. so just a custom tcp rule and just 8080 in here and i want to be able to connect this instance from any ip address anywhere in the world so i'm just going to select anywhere which is the default thing anyway and that is good maybe you should actually give this a better name let's go with uh a node app security group a security group for node apps i don't know whatever review and launch and then i'm gonna launch this and i'm gonna use my existing key pair i have more details on the basics of setting up an ec2 instance in another video that i'll link in the description if you want to go over those steps a little bit more and i'm just gonna set this up quickly and i can view my instances i already had a running instance from my last video so i'm just going to name this appropriately so i know the difference there we go and now i just have to wait for this to be up and running before i can connect to it okay so now this is running i'm just going to click the connect tab so i can copy this example snippet from the ssh client tab i'm just going to go over to terminal and paste that in and i just need to modify the key part because my key is located in my ssh directory and this should allow me to log into the instance using ssh so now i'm on the ec2 instance perfect the next thing that i want to do is just install node so there's three parts to this uh the first one is installing dependencies node relies on gcc and makes so i'm just going to install those and all of the code snippets that i'm entering right now i'm going to leave in the description so you can just copy and paste them after the second thing we need is to tell yum uh which node installation we're going to use so i've just uh pasted in a thing that says we're going to install node version 16 and the last thing is just to sudo yum install node.js and this should take i don't know a minute maybe maybe less oh that was way less than a minute okay cool so i should now have node installed let's see note version 16 is now installed on the cc2 instance so i can now start running node apps and i could just start writing code straight on the ec2 instance using something like vim but realistically i'm going to be writing a node app on my computer i'm going to be committing all the code into a version control system probably get probably pushing it something like github and really when i want to deploy the code i just want to take it from my version control system so the most common way that i probably want to get code onto my ec2 instance is by using github and in this video i'm just going to install git straight onto the ec2 instance so i can pull the code from my repo and then just deploy it like that so before i do anything else i'm just going to install git which is just sudo yum install get and then i'm just going to git clone this node app i have which is just an express app that will display a random lord of the rings character so if i list this directory i have this lord of the rings app if i cd into the lord of the rings app i can see there's you know some standard javascript files there's only a few of them your project is going to be bigger uh but yeah there they are so the first thing i need to do now is install all of the dependencies that are defined in the package.json file and i can just run npmi or npm install for that and that should have installed everything uh so now i should just be able to run node server js because this is the entry point to my application this is where i've defined the express application and it is listening on port 8080. so at this point i should just be able to visit the ec2 instances ip address at port 8080 and see my application running so i'm just going to copy and paste this into the browser i've got to remember port 8080 and i defined a test route just to make sure things are working correctly and there we go so that's just a few steps to get a node app running at a point where i can actually visit my web app on the public internet using the ip address now the actual application i have pulls some lord of the rings dates from a mysql database and just picks a random character from the database and displays it on the page and if i just delete the test route it should request this information from a database but i haven't actually defined which database it needs to connect to and this is a really common thing when hosting a web application some of the details in this case which database to connect to have to be left up to the system administrator the person actually setting up all of the infrastructure and this is done using environment variables so here i actually have the code for the node app and in order to connect to the mysql database the server needs to provide these four environment variables because this should be up to the actual server which database it connects to in a development environment it'll be a development database in staging it'll be a staging database production will be a production database so this just allows the sysadmin to customize this at the point where it needs to be customized it also adds a little bit more security because then you're never committing passwords into your version control system because they just exist on the machine and not actually in the code so i need to actually set up these environment variables on the ec2 instance before i run the node app for this to work [Music] so here i have my environment variables i've just put it in a blank file in vs code and this is a database that i set up in a separate video and i'll put a link to that video in the description but i already have the database set up it's ready to go i just need to define the details for the database so this is the host that it exists on this is the user the password and the database it needs to connect to and if this is a mysql database running on the exact same ec2 instance which is something that you might want to do you might want to set up everything on a single ec2 instance if it's a small web app then i would just change the host to localhost and the rest of the details could stay the same so all i need to do now is copy these environment variables and i'm going to go back to the ec2 instance and i'm just going to stop the node app from running and before i run node server because that's all i did before i ran node server that started the application before i actually run that code i'm going to run the environment variables so i'm just going to define each one give it a value and then at the very end like it's just all one line of code and i'll put all code examples in the description but when i run this the environment variables will be set the node app will start that didn't work oh that's interesting because uh double quotes in bash interpret exclamation marks uh as a special character i should have actually used single quotes for all of these but that should still work um node server actually you should use single quotes in bash if you don't want the stuff in the quotes to be interpreted by bash in any way um so this will set up those variables run the server there we go that should now work it's listening on port 8080 as well and actually one more thing i should mention uh port is a common one with an express app port can be defined uh by the sysadmin so i could change the port to you know i don't know 3000 if i wanted to 3000 just by defining an environment variable like that uh now it's listening on port 3000 instead but i don't want to modify the port i want it to stay on 8080 so i'll just leave that off for now but i now have these environment variables set up and it's running the node app so if i go back to my app and refresh now it should be able to connect to my database and i get a random lord of the rings character fantastic that worked first try that doesn't always happen so this is great this is now working i have my node application set up it's running it's accessible on the public internet it's connected to a mysql database fantastic the issue now is that if i close this terminal window or exit the app in any way and go back and try and refresh the page it can't connect to the application because the application isn't running still so we need a way of always having this node app running no matter what if the server restarts if the app crashes whatever it always needs to restart the app the app needs to be running always is the idea anyway and in order to do this we can use something called systemd systemd is a lot of things and it can do a lot of things but all we really care about right now is that systemd can manage our application as a service so it can start it in the background and it can make sure that our application is running every time our server restarts it can restart our application if it ever crashes so it's just gonna be responsible for making sure the app is always running and setting up our application as a service with systemd is incredibly easy uh first we just create a file so i'm gonna do that using vim in the slash etsy slash system system system d slash system system there we go slash and then we just create a service file so i'm going to call this uh i don't know lord of the rings dot service why not uh so i'm gonna create a new file here and then i'm just gonna copy and paste a code snippet and like i said i'll put all code examples in the description so at the top here we just have the description of what the service is i'm just calling this my node server i probably could have come up with a better description but oh well the after and wanted by sections we're just going to put in multi-user target this just means you know make sure that the system is set up that the networking is all set up before this service starts so it doesn't start too early uh exec start this is how our application actually starts so we're going to tell it the path to node and we're going to have node run server.js so this is just the long way of writing node server.js we always want our app to restart and we can have a restart time of 10 seconds just in case anything happens we're going to send all logs from standard out and standard error to syslog so this means any console log or console error or anything from the node application is going to be managed by syslog and will end up in the default syslog location which we'll take a look at in a little bit so this is really handy because we can easily view all the logs for our application uh on the ec2 instance but later on if we wanted to set up cloudwatch which is a service in aws that allows for better logging straight from the web console it becomes incredibly easy if our application is just directing standard out and standard error to syslog and then the identifier will just this is how we can see it within syslog the user that is going to run the node application this is just the default user we have set up on these two instances it's ec2 user we don't want to give it root privileges or root access or anything like that so easy to use is great and then environment file so if we need to define environment variables we can just put them all in a file and i've just defined that to be within the application directory itself just a file called app.m that i'll have to create in a little bit if you didn't have any environment variables you could just leave this line off so i am just going to save this file there we go um and i am let's see yes i'm in the lotr directory so now i'm just going to create that environment variable file i just called it app.m and i'm going to paste in those environment variables but i'm going to put each one on a new line looks a little nicer there we go so these are all the environment variables just in an environment variable file this is kind of nice uh so i'll save that as well and then i'm just going to start this using sudo system ctl start l-o-t-r-dot service and that should work so if i just check the status now we can see that the custom service is in fact running and this means that i should just be able to go and in my browser refresh the page and the app is running and if i refresh over and over again the app runs in the exact same way but i could actually close this terminal window exit the ec2 instance and it would still be running there's one more thing i want to do which is sudo systemctl enable lotr dot service and this will make sure that if the ec2 instance restarts if i reboot it for some reason that the application starts up again and it's always going to be running so this app is running that's great one more thing i just want to show you is that if we wanted to view the logs because right now i don't see any of the logs coming out no matter how many requests i make if there's an error whatever all my console logs all my console errors are not going to show up in this terminal uh but they are located at slash var slash log slash slash var slash log slash messages there we go that was a mouthful so if i want to view this i do need root access i'm just going to write sudo and then just cat to display it straight on the screen and there we go there's my my node server logs i can see them versus the other logs here and i can see all the get requests i made i should be able to see yet we're listening on port 8080. so if i ever needed to come in if there was an issue and i just need to view the logs i could come in here and view all of those that's it for this video we now have a node app running on an ec2 instance that's available over the public internet and it's always running as a service using systemd in my next video i'm going to show you how to set up nginx as a reverse proxy so that instead of having to access the app through port 8080 or some custom port we can just access this through port 80 the default http port [Music] you
Info
Channel: Sam Meech-Ward
Views: 76,301
Rating: undefined out of 5
Keywords: deploy nodejs app to aws, aws ec2, amazon ec2 example, nodejs systemd
Id: oHAQ3TzUTro
Channel Id: undefined
Length: 14min 51sec (891 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.