Deploying Spring Boot Application in AWS using EC2 & S3 | Microservices Deploy | Maven Clean Install

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to sub code and today we are looking at another cap of code um I would say not quite because we're looking at actually deploying our um backend code or our spring boot application so we want to see how we can utilize AWS S3 as well as AWS ec2 instance to deploy our application and have it live um on an environment where it can be tested and we can have some users play around with it and see uh get some real uh feedback real test scenarios from real users and we can see how that can um that can assist us in improving our application and improving um the test in our application so let's get right into it we don't want to waste um a lot of time so the first things first what you want to do is to have a spring boot application um you can create that through the spring initializer and you can basically create or scaffold some um controllers uh that you want to play around with and you can have those controllers um doing whatever you want or whatever you prefer so basically what we have is our Spring Bank application which we've been um utilizing in this series so now we will be deploying this uh in AWS so let's see how we can get around doing that and um you will see some commands that that will run you see some scripts that I will run so those will find in the description down below and also uh I will show you uh one other thing um let's just get right into it and then I will run run you down uh it's it's each each of the steps uh sorry about that so uh the first things first once you have your application it's very important to have your spring profiles defined so by profiles I mean environments that you have so if you have Local Host test um QA uh production and things like that so in our application we have our applications properties which is what we have here and in applications properties we essentially have by default we say spring profiles uh is Local Host which is where we develop our application and test it that is a default now if we don't set a profile it will just assume Local Host but if we have a profile that is defined uh then the application will run based on that particular profile and based on the profile that we've defined it will then utilize the property is defined in that particular profile so if here I have test and um test will then be this properties file so it will read these properties right here so uh please do pay attention that the profile do active is only defined in this particular file right here which is spring profiles active and uh these spring profiles active that add is coming from a pom file which um I will show you just in a few so it's coming from our pom file this value right here I will show you just now so all of these properties will be read for the Local Host uh profile and then uh the test profile will have uh these properties over here so these are test profile uh these are test property and everything looks like this so what we want to do now is to see how we are going to have this value set so what we do is in our pom.xml uh over here um what we want to do is essentially add the profiles tag which [Music] is uh which starts from here and it has all of the profiles that we have and as you can see the profiles has the first profile is Local Host and it has an activation which is active by default and that is set to true and the properties is string profiles is spring profiles active Local Host and then the next profile that we have is um the test profile that we have right here so now what we are going to do is to add another profile so that you can see how we can achieve this um to run different um environments uh for our application and you will see this coming in handy when we run our application in our easy2 instance that will create in AWS in a few so for now what we want to do is add a profile here so I'm going to copy this and I'm going to add the profile then I'm going to say uat or this will be your QA uh things like that then I'll will call this uat and I do have a um a video where I discuss the profiles in detail so please do check that out as well so I won't dive deep into this I'm just showing you um as we add another environment within our AWS how we we go about achieving that so this profile file has now been added we're going to save it there and then what I'm going to do I will take everything that is here in the uh test properties and I'm just going to paste that and what I want to do I'm going to say this is uat and I'm going to say okay and then I'm going to go to uat right here and the app based URL I'm going to add the ac2 instance that I want and then the database I'll also I'll change this but I won't change the database because it'll take longer to like create the database within the new um AWS um RDS so what we need to do now we have our application we need to um configure um the maven build for this so we're going to say right click here and we're going to say run configurations so our configurations as you can see here it has uh spring boots uh it has okay the base directory is project um One bank which is basically the spring banks that we the Spring Bank application that we have and what we want is actually not the spring boot to run which is old we want the spring. Run profiles do active which is Local Host so we want this parameter to be there uh when we run our application so now we have Local Host we have test and we have uh uat and we will basically apply this and this is this is our Maven build and we can run this build so what it will do now it will build our J file and package it with all of these uh profiles that we've defined and when we run the application we just Define the property uh of environment so spring profile will tell this application which environment to run for so if you say test it's going to run for test if you say Local Host going to run for local host and you say um uat is going to run for uat so now everything ran through fine and let me show you as the application started what it does so if you look at this line here the following one profile is active and if you look at the profile that is active is Local Host because our build did not Define any profile and we had said by default just put the Local Host profile right there so that is looking good and our application is fine so just to make sure that everything ran through let's just do this what we want to do is right click here uh let me just click on this because I wanted to give us more options like that so so you can do a mve and clean and then to clean the um the target uh folder of the of the deployment artifact which is Target uh and then what we want to do is we then want to M even install then it will give us a new deployment artifact with all the changes that we just did right now so let's just run that and let's just wait for it all right so it's coming through and let's run that boom boom boom and let's see so as you would see um okay all right so build success so now we will find the uh the artifact in the Target as and snapshot. jar that's what we are looking for so now let's go to um AWS where we will plug in this artifact of our application so now I've shown you how you can create the application to have OA for different environments and now we want to create an environment uh in AWS so that we can be able to run the application where we want it to run so I'm going to create uh let me go to services and I'm going to go to ec2 which is that and uh let me go to instances running which is just going to give me a table of my current instances that I have and you can see I have no instances here uh on this particular region which is the Cape Town region which is fine then I can say launch [Music] instances and then I will just call this uh Spring Bank uat environment something like that uh that's just the name and let's see Amazon Linux that is fine because I'm going to free free here so I want everything to be free tier and everything else I can leave as is for now I don't need to change anything Auto assign public IP enabled create um a security group that is fine uh and allow SSS uh traffic from anywhere that is good HTTP traffic from the internet uh HTTP traffic from the internet also that is fine uh because we also going to add some rules um over there so everything else is fine we can just use we can just use the default settings and let's just see key pair uh let's just say create a key pair uh let's say proceed without a key pair I don't really need one at this point and this particular point I don't need one uh because I know you would maybe see like tutorials where um they would also show you how to SSH into that particular instance so that you can be able to um make some um run commands in the particular um U machine and you'll be able to deploy the artifact and things like that but in this case I want to show you that you can utilize a WS itself just to connect to the instance and run the same uh type of um type of deployment without even having to um to do all of these over overhead so let's just say view all instances so let's see so the instance is running so everything is looking good and now what we want to do is essentially um uh create a S3 bucket so before we do that status check ini initializing yeah so on once this is spinning up so let's create a S3 bucket for this particular um deployment so we're going to go to S3 and we are going to create a bucket here and we're going to name this bucket uat uh um let's say say uat environment Spring [Music] Bank uh bucket like that so that bucket is fine U environment Spring Bank bucket and that is there and let's see uh ACL disabled so that is fine but get own overhead uh block all public access we actually don't want to block anything so let's just leave that uh uh I acknowledge that so this just tells you that you a that the bucket will be public we do want the public the the bucket to be public so that is fine and then let's see what else we have here so everything else here we can also use the defaults so we just create the bucket and let's see what we get so now we have a uat environment spring uh Bank um bucket then we want to click on this bucket and the way that now you make a bucket public um I know the older way there wasn't easier you just say um uh you just say like bucket public but the current way you have to add the uh public script into uh the the bucket policy so let's just go to properties uh we should see bucket policy let's just see what we have here object Lo uh let's see uh bucket key bucket versioning let's say permissions block all so that P bucket can be so yeah that is in permissions bucket policy right here so we can edit this script and if you want this script to make your bucket public you can just get it from your uh from the description down below and you can basically use the same script so this is the script to allow our bucket to be public so we just paste that script over there and we have um public read get object allow and a stre get object and the bucket name we basically get it from right here and we just copy that bucket name and we just change this to the bucket name like that and everything else is fine so now what we want to do is save changes just like that so now our bucket is public and what we want to do from here on is just go to objects and we want to create let's create a folder and call it applications uh where we will be uploading our artifacts uh do not specify encryption key so we can just create the folder just like that so now our bucket is public and now it has a folder so let's take this folder just like that and um upload um these particular artifact so we're going to upload the artifact that was deployed by our Maven right here so please note this is in S3 which is very very important so this is in S3 and we're doing this in in the bucket so add uh files from here and the file that we want to add is the jar not the jar original we want to add the jar uh snapshot so let's take the jar and we open that and now that's the snapshot that we created from our Maven build and we just click uh upload that and it's going to take a while to upload the artifact [Music] all right so artif effect uploaded so everything look looks good now so we done with S3 now this is all we need to do with S3 and we done with it so the next thing that we need to do is go to our ec2 instance so let's go there and it should be up by now so let's say instance the running instances let's see so everything looks good status checks passed everything is fine so now let's just click on that and what we want to do from this particular screen is just click on um connect that's what we want to do and Connect using H2 instance connect and we want to do that let's connect now this is going to give us a terminal that we can use within the browser which is as responsive as quick as reliable as we need everything to be so there's no hustle of you using party because you're going to get the same thing uh using the terminal on the browser which I feel is much more efficient um and also it minimizes the overheads that you have so now we have our ac2 instance so what do we do from here so the one thing that we want to do is uhse sudo I let's do that so now we are on root IP so that is fine so now we want to run a couple of commands so the commands that we want to run is first let's get yam so that we can be able to install Java we are going to run a Java um a Java um application so let's do that and that is sorted and let's just do that uh run that command boom so now that everything is fine now we want to install Java so I'm installing Java 17 and that is primarily based on if you look at the pom file uh from my application uh let's go there let's go up there H you will see that my uh ja my Java version is 17 so I'm getting that um um particular Java version so let's go there and let's run that uh let's run that so now what we want to do is wait for that to finish and let's see what we have then we're going to run Java version okay so now let's just do DVA Java version let's see that okay so now we have um Java version uh 17 and that is all good right there so now that we have that everything is looking good now we want to import our deployment artifact so our deployment artifact is in S3 so we're going to run this command with the with the link to our artifact so what we want to do is essentially just run a w get and we going back to uh we're going back to our s Tre right here and we're going to we're going to go to our bucket that we created and go to our artifact so let's click on the artifact right here and we will take the object URL that's what we want copy the object URL and then let's go back to our uh console right here and we want to run that and voila quick like that now we have our um artifact uh over there and the last thing that we want to do is basically run the application with this particular command so as you can see here what we're running is Java jar and we passing this particular parameter of string profiles um active run uh uat instead of test now we want to run uat and we click enter and we wait for the application to start up and you can see profile active is u over there which is good and let's see what happens there boom boom boom everything's created started uh Wonder Bank application so the application is started and everything is running as we expect it to start and basically once the application starts we are able to then um interact with the application and um do whatever we need to do so with the test um environment um let me just uh go to with the test environment um I'm able to come to my Postman and say uh instead of Local Host I want to go to test and I can go to log in and I will just do a login and I'm able to uh let's see there okay I think my application is down so let's just see the test environment yeah application is down so let me just uh run the let me run yeah let me run the application right here so oh crap uh that is cool I didn't want to run that anyway I wanted to run that yes that's what I wanted to run okay so now we running the test one and you can see that the profile is active and this is running test so this instance that we just deployed now is running uh uat and the other one is running test and I'm able to now on my post man just go to environments so you can see even on Local Host there's a local host um um uh URL here and on test there's a test URL here as you can see and then on uh now we're going to add add a new environment and we just going to call this environment uat and that looks fine and we just going to add the variable base URL and we are going to um let's say we will add a URL just like this one but obviously we're going to take the one within our new instance um over there so let's put that then let's save this this so for now let's go back to our application here and so we done with uh the S3 object um object URL now we want to go back to our ec2 instance and go to that particular instance and let's uh take the public um the public URL so the public IP V4 is that so we can uh basically uh copy that one and we can come back to our Postman and just paste that uh we can paste that right there uh right uh only took the IP address so let's just paste that over there and let's copy this and let's paste it over there which is uat then we save this so now before I even change anything in terms of like security groups and stuff let me just show you um so here this refused so now the application started in test so if you do a login again it should be fine now um so let's get a response boom so we're able to log in now I'm going to switch to uat just like that and now I'm going to try and log in and let's see what that gives us and just wait for a response and see what that does so it's taking longer than usual and it's going to fail the reason why it's going to fail we need to allow traffic to be able to um come into the server and interact with the server so that is expected because this request is going to time out so now let's go back to our ac2 instance and what we want to do now in this instance is go [Music] to um security and we want the inbound rules crap let's open this so inbound rules uh outbound rules it has one and allows all all this one allows TCP on all but Port 22 so we want to edit that so let's go to we going to go to the security groups because that is where the um access rules are basically being handled so let's go to that particular Security Group and let's check the rules there and you can see inbound rules is basically um SSH so we want to change that say add rule uh we say custom that is fine um let's say do do I want give all or just give 8080 let's give 8080 for now because we want to point it to that particular um um application to straight to application so then we're want to say the um the source can be anywhere so that should be fine let's save that um please sign in again let's just say reload let's see I did it log out um let's see let's try again again here sign again going reload to sign into AWS Management console okay so let me let me just log in and then it will come back again all right so now we have logged in and we want to add our rule as we're adding it 880 just like that and we want to allow traffic from anywhere and we want to save this Rule and voila so our rule is saved as expected so let's see um let's go back to our Postman and now we're going to try um log in again to uat now let's see what it's going to do boom so now we get a response back and you can see that we were simply blocked by this particular rule that did not allow our traffic to access the port 880 within our application so now we're able to tell it that uh please open um the instance uh for this particular traffic and everything now is running smooth we're able to get a response back from the server in uat as well as in test so you can basically uh apply the same um um steps to create a new environment and be able to uh create the uh different profiles for the different environments and you run your instance the way that you want so if we go to um let's go to the instance itself and uh we go back to the instance and we want to connect to the particular instance so that we can see what's happening there say connect but now we won't be able to follow up because we basically lost that session um within the browser which is the only um drawback but you'll hardly lose connection um I think it's just an issue on my internet but everything is fine uh so you'll be able to follow up with the logs and what's happening uh right there so there's nothing hectic you can still do a Java J then you want to pass in string uh profiles um active and you want to essentially do a uat since it's a uat instance and you want to run um the Wonder Bank snapshot uh let's just take that from there since that's a long name let's take that from there and then let's just paste that over there and you'll be able to do that unable to access uh jav file because it's being used by the instance that we lost but besides that it should be able to run it should be able to be fine and you can follow the logs as the application goes or what I can do is basically just bounce the instance right here and then it will um close any an open session and then I can come back and run it from this instance and then can follow up with what's happening in the application logs and basically that's it but besides that everything is running smooth as expected and we're able to um interact with our application so just to prove that we we were able to get the data and now let's just get uh let's just do uh let's do something here and let's just say save let's say let's get maybe dashboard something like that uh get dashboard let's see uh what is this oh this is test uh let's see there uh the test user still fine this should be uat yes and the login should be a u login so let's log in in test right let's log in in uat right then let's take uat URL just like that the uat um JWT now let's put the JWT right here save that and then we're going to come back and get the balance here and let's just say send that so as you can see we get the same response and that's because because I'm using the same RDS database in both profiles so that's why you get the same data but what you would want to do let's just go to databases here RDS databases and you can see Spring Bank DB uh it's available and it's a pro database and you can see over here on my um uat properties I'm basically using the same database uh as I using on my test uh profile right here but essentially everything is running as expected uh the only thing is now just changing um the database to point to the correct database of that particular environment then voila you are ready to um connect to your application and run um your application uh like that so I hope this came in handy and I hope it was helpful so make sure that you have your database in the RDS or available um over your network so that you can be able to interact with database and perform your crude operations and uh once that is Set uh then you have your E2 and your S3 and essentially you basically follow these commands as we did and everything should run as smooth and I hope that you like this one and uh I do um hope that you also um check out the other um video where it has the profile in detail the profiles in details and how you can set those up um in your spring boot application so the front end of this application it is the ionic mobile application that we are busy with and as you can see I'm using the ionic framework um platform so that allows me to uh do a number of commits so each commit that I do I'm able to start a build and basically choose which build I want so I can build an IOS and Android or web and basically this then um speaks to the um spring boot application which is the back end of what I'm doing so please do um watch the ionic um series as well so that you can see how we integrate how we log in um how we do security authenticate users um also doing um forgot password onetime pin um or OTP verification and things like that so please do check the ionic um front end um mobile um development we're using ionic and uh we are using angularjs and typescript uh with that so please do um watch out for those as well and I do hope that you do subscribe and like the video also do comment any concerns any questions if you we have muff for now cheers
Info
Channel: Cypcode Studios
Views: 720
Rating: undefined out of 5
Keywords: aws, spring boot, spring boot in aws
Id: grjqzyS7vZY
Channel Id: undefined
Length: 37min 32sec (2252 seconds)
Published: Mon Mar 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.