CI/CD from Github to AWS EC2 Step by Step | Node.js Express Backend API as an Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back to another episode of aws tutorial and today i'll be talking about continuous deployments from github to an aws ec2 instance and we're going to use code pipeline and code deploy to help us get there i'm just going to use the node.js express app for the demo but the general process is the same for all other applications so after the video you're gonna be able to do the same thing for all types of applications so without further ado let's get to it alright so step one is to create a new github repo for our project so what i'm gonna do is click new and then let's just name it node.js express on aws ec2 i'll just keep it public add it with me get ignored and we're gonna choose note create repo and right now we are ready to download this into our local machine so we can edit it so let's copy this url and then open vs code terminal new terminal and then i'm going to get clone and then paste the url we just copied enter if you don't have git installed in your machine you can just simply go to the website and follow the instructions to download that based on your operating system and right now what i'm going to do is i'll cd into the project first thing i'm going to do is i will create a package.json for our dependencies name i would just name it i guess the same name as the github repo and then we have the version to be [Music] 0.0.1 and then dependencies the only dependency we're going to need is the express dependency and that i believe is the latest version all right so next we're going to create the app dot js file first thing we're going to do is we're going to import the express module and then we define app as a constant and then we're going to host that we're going to listen import 3000 and then let's define a get method for the root directory an error function and then we just simply send it let's do let's send h1 that says express demo app close it and then followed by uh let's do h4 a message that says success and finally we're going to have a paragraph that we can specify the version number version 1.0 and then let's define another path that it's also we get let's just do products and then an error function request response and it will return a list of products and each product has a product id attribute enterprise attributes let's define another one product id 102 and then price let's say 150. and then finally we're gonna have the app listen to our port number and then just print out something like demo app it's up and listening to port the port number and that is it that's all we need for our api and now let's get to the code deploy part what we need to do is we need to create a ymo file called appsback and it needs to be in the root directory of your project and what we need to define is the version number 0.0 and then operating system we are going to use linux and then files have a space the source is going to be everything in our root directory and then the destination that we're gonna put all the files in our ec2 instance and we're gonna just put it in to our ec2 user and then create a new directory to host our app over there and then the next section is the hook section which defines the action or the scripts that we're going to run for the deployment step number one is we're going to have application start location is we're going to define all the scripts inside the script folders we're going to define those later we just call it application steps and this this file will run before before anything happens and then let's set the timeout to be um 300 seconds and then we're going to run it as easy to user and then step number two is before installed so before we install anything we're gonna run this script and same thing is going to be in our script folder and we're going to call it before install.show and then it has a timeout of 300 seconds as well and run as easy to user same and then the last step is the application start and that is the script we're going to run for starting our node.js application and the script location is going to be inside scripts and it's called application start the shell and we're gonna have a time out of 300 seconds and i'm going to run it as easy to user all right that is it and right now let's create a folder called scripts but we're going to define all our scripts that i just mentioned all right the first one is going to be application stop that shell i already have this written out earlier so i'm just going to copy and paste it but what it does is it first prints out what the script is going to do and this command over here is it stops node servers in the ec2 instance if there are any running over there and then we're going to create another one i believe we called it before installed.shell and what we're going to do here is we're going to download nvm from the web which is a note version manager and then we're going to use that to install node and the second thing is that we're going to check if this directory exists in our ec2 user directory if not we're going to create that and the next what we're going to do is we're going to create one last one i believe we call it application start show and what we're going to do in this script is we're going to first navigate into the express app directory we just created in the previous step and then what we're going to do next is we're going to add npm and node to the path so that we can execute that in the express app directory and then right here what we are doing is we're going to install all the modules that we need to run the application and then finally we're going to start the application by issuing this command over here so normally we just need to do node app.js but that sets up the node app to run in the foreground which is not what we want we want it to run in the background so it will not be terminated due to inactivities and what it does is it just tells it to lock the normal logs into this file and then the arrow logs into this file right here and that is it and right now let's test our app locally before we do anything in the aws ec2 so what i'm going to do is npm installed oh by the way if you don't have note installed in your machine you can just go to the page and then follow the instructions to download and install it it's pretty simple so right now i just installed it the express module and then what we can do is note app.js hit enter alright demo app is up and running listening to a few thousand seems like it's working so let's go to localhost 3000 and hey there you go our app is up and running and let's go to the product path and we can see our products all right so right now let's move on to step number two which is to create an ec2 instance but before we do that we need to create two i am rows for us to use later on so what i'm going to do is just tie in i i am click on it and then click on rows create row so the first one that we're going to create is for the ec2 instance so i just click on ec2 permissions and then type in code deployed and we are going to select the amazon hc2 row for aws code deployed hit next next review give it a name i just call it easy to code deploy row create row and then we're going to create another one for the code deployed and then hit code deploy just code deployed and then permissions hit next next type in code deployed row create row alright so we just have two rows created right now we are ready to create our ec2 instance so just type in ec2 right click open that into a new tab click on instances hit launch instances i'll just select amazon index ami one thing i want to mention is that remember all the scripts that we just wrote and right here in the apps back emo we specify we're gonna use the linux operating system so when we choose the image type we have to choose a right here when we choose the image type we have to choose a linux operating system so just choose this because it's free hit next configure instance details so on this page we only need to change two things the first one is the item row we're going to choose the ec2 code deploy row that we just created and then and then in the events detail section we're going to add some scripts to it for installing decode pipeline agent into our too i'm just going to copy that from my notes but i will include that in the description down below so you have it as well it's right here so what it does is that you do the updates in your system and then it will install ruby which is required to install the code deploy agent and then we're gonna install the wget and then navigate into our ec2 user directory download the code deploy from the website and then install it so copy paste it here hit next hit next alright so this is very important we need to add the tag because we need that to identify this ec2 instance when we configured our code deployed so i'm just going to call it name and i'll just call it express app hit next what we're going to do is we're going to add raw we're going to open port 80 so that we can download things from the internet and then we're gonna open port 3000 because remember our express app is listening to port 3000 hit next review on launch launch right here we have the option to choose a key pair we don't really need to but it's useful for debugging if you run into any issues because you can ssh into your ec2 instance to look at the logs and stuff if you don't have a key pair saved in your computer you can just hit create a new keypair but already had one from before so i'm just going to select that one so hit launch all right let's view the instance the state is depending i think it's going to take a couple of minutes so i'm just going to pause the video and then come back in a couple of minutes all right so it's been a few minutes and right now our instance is up and running and it passes the status check so it's healthy but one thing i forgot to do in the previous step is that in the code we just have it locally we did not push it to github so let's go ahead and do that what i'm going to do is get add everything git commit let's just say build a simple api get push oh we also have a typo here it should be hooks no coke let's typo push it again i'll have another typo it should be installed fix typo push it again all right so right now we just pushed it to github let's go back to the aws console right now we are ready to move on to the next step which is code deployed so just type in code deployed opening a new tab go to applications create an applications i will name it express app choose a platform we're gonna deploy that to an ec2 instance create an application and then hit create deployment group enter the group name let's call it express app group enter the service row we're going to choose the code deploy row that we just created and then we have the option to choose whether we want to use in place or blue green i just use in place aws easy to instance and then right here we can select the one that we just created the ec2 instance and then next let's leave it as default and then why not just all at once for the load balancer section we only have one instance so we're not going to have any load balancing create deployment group all right it's done right now let's go to pipeline to create a pipeline for the deployment create pipeline express app pipe line leave everything as default in the event section you have the option to choose your custom s3 location to host your your code i'm just going to use the default and then hit next source we're going to choose github version 2 because our code is syncing hub if this is the first time you do this you're not going to have anything here so you would just do connect to github connection name let me just do express app connection connect to github and again if this is the first time you're doing this you're not gonna have anything here so you have to do install new app and then sign in confirm password let's open it up and then select repo that we just created i believe is this and then hit save and then connect and right here you have the option to choose that repo you just connect it to and then it's the main branch and then hit next for here we're going to skip this because we're not going to have any build stage hit skip and then right here we're going to choose code deployed and then select the application that we just created in the deployment group hit next if everything looks good create pipeline and that should do it let's refresh and that it's done what i like to do is i just click on details and see how the process goes scroll down and view events oh that's quick so everything's done and right now let's go back to the ec2 instance that we created right here let's copy this url open a new tab paste that and then go to port 3000 and there you go your express app is up and running and it's successful and right now let's make some changes to the demo app to make it say success again version right now is 2.0 let's just do updated version hit push and right now if we go back to the code deployed deployments we should have a new deployment created and in progress right now if we click on it view events it should be ongoing and everything successful if we go back here if right now we refresh the page it's gonna change to version two and there you go it says success again and version two and let's just go to the product browse and products route and we're able to see a list of products that we defined and that is it everyone i hope you have learned something from this video if you like this video i hope you can give it a thumbs up and i'll see you in the next video
Info
Channel: Felix Yu
Views: 14,538
Rating: 4.9155674 out of 5
Keywords: aws, CodePipeline, CodeDeploy, continuous integration, continuous deployment, CI/CD, Github, EC2, Node.js, Express, Backend, API, github action, github hook
Id: Buh3GjHPmjo
Channel Id: undefined
Length: 23min 22sec (1402 seconds)
Published: Fri Jan 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.