Running a SpringBoot Rest API Java application on AWS EC2 instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
but in this video we'll be learning on how to run a simple Springwood application on an ec2 instance so let's get started so before we dive into the nitty-gritty details let's get a high-level overview of what Amazon ec2 services so easy to is one of the most popular a double yourself offering it's also one of the oldest as well so it's basically a computer on cloud here we'll be configuring the system according to our needs and Amazon takes the responsibility to keep it available and maintain the hardware so the primary advantages of ec2 are it is scalable it's available and it's also distributed it's easily compatible with other services of AWS and that's one of the biggest advantage of ec2 it is also easy to setup and very easy to terminate so with that in mind let's see what we would be doing in this particular video so first we'll be logging into the Amazon console and then we'll be creating any ec2 instance after that we'll come back to our system and create a simple spring boot application so we'll be creating a REST API with just a single kit endpoint so after we finish our application we'll be creating a jar of the application and copying the jar onto Amazon ec2 so once that is completed we will deploy the jar on Amazon ec2 and we'll get the very PA up and running on the ec2 instance by doing this anyone with an internet connection will be able to access our API by making that kit call so let's see how we do this particular operation welcome so as part of step one will first be creating an ec2 instance so in order to create an easy-to instance you have to first log in to the AWS account so I have logged in and once you log in you go to the AWS management console and the page just looks like this so in order to create an ec2 instance you have to go to services and under compute you can find ec2 but you can also use this text box and type ec2 and then you can find it the first option the tailgaters here is the one that where we have to go so once we click on ec2 we get a dashboard something like this so the dashboard has lot of metrics in it it usually tells you like how many instances are running like how many elastic IPS we have and other stuff so as we see we do not have any running instances so let's first create a new instance by clicking on this launch instance button here so as soon as we click the launch instance button we go to this particular page so here you have a lot of choices for choosing the operating system that we need so based on the comfort based on your comfort level you can choose the operating system you want there are some operating systems that are free tier eligible and some are not so for this demonstration I'll be using the Ubuntu server 18.4 LTS so there's free T eligible so I don't have to pay any additional money for it so once you select the operating system or am i we have an option to choose the compute capacity of the instance that we want to configure so I'll be going with the t 2 dot micro which is free tier eligible but based on the kind of application that you would be running on your ec2 instance you can go for the bigger ones so big higher the computational capacity higher is the cost so let's just choose this and go to compute instance details so there's a page where you get a lot of options to configure but this is this has to be configured on a need to basis based on what you are doing for this video where we are deploying a simple REST API application we don't need to change anything here so let's go to the storage so we'll go with the default values for the storage as well as we do not we do not have any need for a higher storage capacity instance so here you can add some metadata to your instance so if you click on add that you can add a key and a value value to it so we do not we do not have a need to add any metadata so let's go to configure security so security group so this particular page is important because a security group acts as a virtual firewall for your ec2 instances so here we can control the traffic that comes in to your ec2 instance and goes outside of your ec2 instance as of now we can see that SSH is enabled by default so with SSH we would be able to gain access of the ec2 instance and run our applications there so this is mandatory but if you are running an application on a particular port then you need to enable that particular port by choosing add rule and if you are installing my SQL or an Oracle RDS on your ec2 instance then you have to choose that and then this port would be free for anyone outside the network to access the database running on the instance so as of now will not enable any other port but later on once we deploy the application that we are intending to do we would need to deploy we will need to free port eight zero eight zero but let's do that later doing later enables us to understand what we are doing this might be confusing as of now but later on we'll get a clear picture of what we are doing so let's just go to review and launch so there's a page where you can review the configuration that we have made so everything looks good so let's launch it so there's a page wherein it gives you an option whether to choose an existing keep it or to create a new key pair so if you have used getting an instance for the first time then you would probably have to create a new key pair so I keep it as as it states here a key pair enables us to securely connect to the ec2 instance using an SSH client so we would be downloading a private key file but so that is very much required in order to gain access to the ec2 instance so let's create a new keep it and we will name it open - and let's download the cable so as we can see a key called a file called open to dot M has been downloaded and it's there not only three so that's about it for creating an instance so let's launch the instance it usually takes some minute to a couple of minutes in order for the instance to be up and running so in the meantime let's go to the Downloads directory and copy this temp file into the AWS that I will directly have created on the desktop I want everything to be at one place so that I can easily access it so once the instances created the next step would be to access the instance so in order to access that we'll do we'll be doing it using an SSH client I'll go through the steps in order to do that so as you can see that the instance is running so now let's try to SSH into our instance so in order to SSH into an instance we need a terminal so if you are a Windows user then probably you should be using putty but it's very straightforward so like if you are doing it for the first time and you need instructions to do that then you can click on this instance and then click on connect so here you get a step-by-step instruction two acts to ssh into your instance so let's do it so the first step I would be doing is to go to the directory where I stored my PEM file so so if I run the LS command I see that the open two dot M file is here so before we even run the SSH command let's do a CH mo d 400 on the pimple so this is actually required if we do not want our key to be publicly viewable for SSH to work so this is an important step that we have to do otherwise you won't be able to know in gain access to the ec2 instance so after we are done with that particular step let's do let's execute the SSH command so this is H - I then we need the key name and then we need to mention the name of these attentions that we are logging into so the username would be Ubuntu that's the default username so here we need to provide the DNS name of our ec2 instance so where do we get that information [Music] let's close this window and if I scroll down you can see that you can find all the information of the instance that is currently selected so there's like instanceid ipv4 address private DNS address so the information that we need as of now to gain access to the instance is the Public DNS so we can see Public DNS here and its corresponding value let's copy that value and paste it here so once we click enter it will ask whether we want to log it let's say yes so there we are we are now inside the ec2 instance so you can see that the IP address the DNS name is open to at whatever the IP address of the ec2 instance that we have created is awesome so the next step is to create a spring good application is to create a REST API using a spring boot so in order to do that let's go to spring initializer so I use spring initializer to just create the base of my project but this can also be done using any IDE but I feel that there's easier way and I'm more comfortable using a spring initializer so as we can see we are creating a maven project using Java and we are using the latest springboard version which is two point three point one there's a stable version and if you want if you are specific then you can give specific names but we and we are just doing it demo application so I'm not really bothered about these names one of the more you can also specify the dependencies that you need for this particular API to run and one of the most important dependency that we would be needing is spring web as we are creating a list API application without yet endpoint of it so let's generate our project as we can see the zip of the project is in our downloads file let's go there as you can see the demos dot zip has been downloaded let's extract it let's go to our intelligent so this can be done on any ID so the most prevalent IDs are IntelliJ and Eclipse I am using IntelliJ but this straightfoward process in Eclipse as well so input projects [Music] let's import the project so you have to choose the external model lesson eben let's go next next finish usually this does not take this much time but there is some process that is running in the background it's slow in my computer down so let's close the window as you can see it's preparing the project it's downloading all the dependencies it's doing some indexing [Music] so everything is updated and downloaded so let's go to a project so here we'll be creating a simple REST API application with one gate endpoint in order to do that after first create our controller class [Music] and this new it does control [Music] the tenets of this class as a mystical [Music] and its leader handed [Music] so this particular method would just be returning Finland [Music] so let's edit this with [Music] and the endpoint would be a double yes awesome so our controller is ready we just have one endpoint and that is a WS and it's a gate endpoint and this would just be returning our string while you called success so let's run this locally [Music] as you can see the application is executed and it's running on port each 0 8 0 so let's go to our browser [Music] secretes it'll smash this [Music] so as you can see the message success so our application is running fine so let's go back to the springboard and let's create a jar of this particular application so now what we'll be doing this we'll be generating a jar and then we'll be copying that jar on tour ec2 instance and then try to deploy that jar over there so in order to create a jar and go to me even tab and then click on lifecycle and then you can see install so we have to run this particular option so once you click on there it starts running and then you'll see that a new folder called target has been generated so we can find the jar in this particular directly as of no it's not yet generated but it'll come as soon as this operation is completed so the build is success and we can see our jar here so let's see let's go to our CW's directive a restore a pinpoint and copy this jar [Music] so it's copied so the next step would be to copy this jar onto our ec2 instance so in order to do that let's go let's create another tap so I've opened another terminal window and I'll be placing it side-by-side so I have two windows open so one window has we have SSH into our ec2 instance and in the other window we will be copying the jar file from our local file system to our ec2 instance so let's create a directory on in our ec2 instance so this is the place where we would be copying our jars to so mkdir jars and if you do an LS you can see that there's a Thai pickle jars so here let's go to the directory where we have our jar CD desktop either place and if you don't know unless command we can see that there's a jar file and then open to PEM file so let's use SCP so SCP is a command it's a short form for secure copy so we do FCB - i and then we need that key in order to transfer anything are easy to instance and then we would need our DNS name [Music] so we can see that we have written the SCP - I there's the key file and after the key files we have to mention the file that we need to copy so here so there's the file that will be coming and will be coming into our ec2 instance in the jars folder so once you click on enter you can see the status of the copy and once that is an hundred percent we'll do an LS here and see if the jar has been copied so let's go to the directory called jars we do an LS you can see that our jar has been successfully copied so the next step would be to run this particular chart on our ec2 instance on the clouds so once we run this jar and if it is six successfully executed then we can access our API from anywhere in the world so in order to execute this particular jar we need to execute the command called Java dash [Music] as we can see we just created this particular instance and Java is not installed on our ec2 instance so we need to first install Java and then we can run our jar over there so in order to install Java they have already so once they've already suggested few commands called sudo apt installed default GRE so every install open JDK so let's try one of this command in order to install Java [Music] as we can see I think the installation was not successful if you read the message here unable to fetch some articles maybe run a pitted apt-get update okay so we need to run this particular command called apt-get update so that it downloads all the packages and information from all configured sources so let's do it so now we can see that we have updated our easy to instance on the packages there and let's try that table sued every installed default GID once again we can see this time that it there is something happening I think it's installing successfully so all it needed was update of all the files on ec2 distance and once that was done we are now able to install Java so Java is successfully installed so let's try to run our jar [Music] as you can see that the jar as successfully is successful running and it's running on port 8000 as it was like it's behaved exactly as it behaved in our local system so let's go to our browser on our local computer and we need the DNS name again so let's go to DNS slash EWS as we can see that it's not running at the moment now whenever the connection times out like this when we are running something on any ec2 instance so one thing that you have to remember is to verify if our security group is configured properly so if you remember when we tried to configure the security so security group can be found here at the same place where you can find all of the details of this ec2 instance and if you remember when we configure our ec2 instance we just configured for port 22 so that we could if such into our ec2 instance and run our application but we have not configured port 8 0 8 0 where our application is currently running on ec2 instance so in order to fix this issue we just need to go to a security group and we can see that the inbound role is just easier as such TCP 422 so it is the only port 22 is open on that easy to instance so we need to edit path in orders and then we need to add a rule so it should be custom TCP and we have to specify the port number as eight zero eight zero so it doesn't always need to be eight zero zero if you configure the port there as nine thousand you have to put in nine thousand here so here you can see an option called custom so custom means you can choose which system can talk to this particular instance so suppose if you want only your local system to talk to the ec2 instance then you can put a custom IP address over here but further for this demonstration I just want any computer to access the access that endpoint so I'll choose anywhere so here they have given text box through add description but that is not required for the time being so let's save these rules and see if this fits the problem for us let's go back and refresh as we can see it's a successful so that was the issue with it that we had not configured our security group properly while not properly on purpose while we are creating our in-situ instance so now we understand how security group works and how we need to configure it in order for our app in order for us to access our application running on our ec2 instance hey guys thank you very much for watching if you feel that you got any value out of this video then please click the like button comment on what you liked and also on what you want a video of in the future and also subscribe to this channel for similar content you can also get in touch with me on instagram at my other pixels until then have a good one bye bye [Music]
Info
Channel: Deekshith Mayanna
Views: 2,421
Rating: 5 out of 5
Keywords: Spring, SpringBoot, AWS, AmazonWebServices, EC2, Cloud, Java, REST, API
Id: hzxD-a_vs7A
Channel Id: undefined
Length: 28min 15sec (1695 seconds)
Published: Tue Jul 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.