Transcript for:
CI/CD Process for Spring Boot App Deployment on AWS ECS

hi everyone welcome to Javi in this tutorial I'll walk you through the end to end continuous integration and continuous deployment process for deploying a spring boot app on AWS ECS using Code Pipeline and code build okay all right so if you are a new to this term then I would strongly suggest you to check out my AWS playlist where I cover the basic to Advan concept you can find this particular playlist I'll will also say the link in video description for your reference cool before we start configuring AWS Services let's understand the challenges we faced without cicd then we'll move on to how to set it up fine so let's assume you have created a spring boot app then you want to deplay this particular app to AWS elastic container service then what all basic steps you need to follow straightforward right so what you need to do once you have a spring boot app with you the first step you need to create a Docker image to create a Docker image you need to create a Docker file so once you have created the docker image then you need to push that image to AWS ECR now once you push the image to AWS ECR then the next step you need to pull that image from AWS ECS and you need to run it this looks good right now let's assume I got some update I did some code changes then I want to again deplay that latest changes to AWS ECS then again I need to repeat this St I need to create the docker image again then I need to push that latest Docker image to ECR then I need to run that image again in AWS ECS so every time I do the code change I need to follow these steps manually which could be overloaded for a developer right which is a repeated task rather than I do it manually why can't I automate it so what I want once I commit the code to the GitHub then I want creating the docker image and pushing it to the ECR should be automated okay so to solve our issue AWS provided one service called AWS code build who will help you to do your continuous integration so as part of this particular architect picture we are creating the we are expecting someone to create the docker image and push it to ECR that is part of our continuous integration okay and AWS code build will helps to build your project or build your configuration but how AWS code build will know what steps to follow so for that you need to write a file called buildspec do yml file and in that particular yml file you you need to tell to the AWS code build hey I want to build a Docker image and I want to push it to the ECR I'm giving this command to you you please execute okay so this build spec. yl will contains the instruction for AWS code build so once the image is available in elastic container registry then next you need to deplay that to AWS ECS which you can consider as your continuous deployment or continuous delivery flow now how can I perform that so to help you on continuous delivery or continuous deployment AWS offered you another services that is called AWS code pipeline using AWS code pipeline you can inform to AWS who will build your project and where you want to deploy your project okay so these are the basic steps you need to follow to perform cicd in AWS cloud or just run your app on AWS ECS using Code build and code pipeline okay so if you look into the flow we are using two different service to perform cicd in AWS that is AWS code build and AWS code pipeline fine so the destination could be anything whether you want to deploy your app to AWS ECS or eks or you want to run it in simply elastic Bean Stu that is up to you but to perform cicd you must need to use AWS code Pipeline and AWS code build to automate the steps fine so we'll follow the same steps in action but before that try to picturize this particular image on your mind and we'll follow the same steps as it is okay don't worry I go step by step [Music] so at very first step we need to create a spring boot app then only we can run that app on AWS ECS right so if I go to my ID I already created a small springbot app so if you'll check the controller class I have defined a crow endpoint for course subject adding the course retrive all the course update the course and delete the course okay and if you'll check in the service class I have used simple inmemory collection and I'm performing all the crowd operations specific to the course object okay there is no rocket science in this code this is simple crowd operation and the same app I want to run on AWS ECS okay so if you follow the flow now I have the application ready with me now the next step I need to create the docker image okay so who will create the docker image for me AWS code build but to create the docker image by AWS code build the basic steps I need to define the docker file so the first step Define a docker file in your application to dockerize your app so let's create the docker file just Define the input so I'm using the open jdk 17 and I want to create a working directory uh SL app then I want to copy the jar file from Target to the working directory and then the expose port on this particular Port my app will run on the container and command to execute my jar okay this is are the Ducker file content I have added now if you'll go to the flow you have created Docker file so that AWS code build can build a Docker image for you now what is the next step once it create the docker image for you the next step you want to push it to ECR right so we need to create a elastic container registry in AWS console so just go to the browser then just go to the elastic container registry just open this then just create a new repo click on create Define any name I'll give Java registry then nothing to do just click on create repo now you can see here right your repository is also created fine so if you'll go to the flow we are done with the step one and step two now we have defined the docker file so that AWS code build can create a Docker image for me I have created a ECR elastic container registry so that AWS code build can push that image to ECR but how AWS code build will understand what I want to do I need to define a build. yml file okay in the build spc. yml file I will provide instruction to AWS code bill to perform the step one and two so for that go to your code and Define a file you can name it I mean you must need to name it buildpc do yl okay now in this build spec. yml file you need to define the command to create the docker image and push the image to HUB fine so let me add the commands anyway you'll find the source code on video description so you can also use the same steps no need to remember the entire command but you need to understand what we are doing in each phase okay so there are three phase pre-build build phase and postbuild phase in the build phas we are generating the jar file because to create a Docker image Docker file need a jar right so first step we are uh just uh generating a jar file then we are trying to log to elastic container registry which we created just paste that particular URL just copy this Ur and just paste it so once you have the repository URI with you you want to log to AWS ECR using this particular command now if you see here the repository ECR I created on the region AP South 1 so I also need to specify that log to the E which is running on AP South 1 and this is are the repository URI once that is done I'm asking here to generate a commit has I'm just defining here cut hyphen c one hyen 7 it means I'm just telling that extract the first seven character of the commit has okay then I'm also generating a image tag for um what I can say it will generate a unique tag okay based on the build ID fine this is the simple step we are doing in the prease or pre-build we are just log to AWS e here and we are generating the commit has and image tag now the next phase is build phase so if You observe in this particular build phase what we are doing nothing we just building the docker image and we are just tagging that particular image okay now the important step that is the post build step where exactly we are pushing the image to ECR can you see here so Ducker push repository Ur with the latest tag with the unique image tag okay so if You observe here we are pushing the image to ECR twice the reason the First Command which is with the latest this command push the docker image tagged as a latest to the Amazon ECR so the latest tag is commonly used to signify the most re recent version of image okay and the second one Docker P repository URI image tag this command push the docker image tagged with a unique identifier that is nothing your image tag to the Amazon ECR repo so basically the the image tag what we are generating based on the build ID or commit house that is what we are doing on the phase one right then once that is done once the push is successful I'm also defining what is the container name what is the container name we have created just copy the name Java registry just paste it that's it so it's just a simple three steps We are following preface we are just log to and adding the commit as image tag in the build phase we are building the image and tagging the image in the post pH we are just pushing the image to ECR okay this is what our goal and we are doing in the build spec. yml file now just go back to the flow we are done with the step three so we have defined the build spec. yml file now let's give that buildpc yml file to AWS code build to perform a continuous integration for me okay so again the fourth step is create the AWS code build just go to the AWS console then just search your code build before that what I want to do I want to push this changes to my Branch okay so I will add a valid message Docker file so I have push the changes so if you'll go to the repo I already created a repository in that repo I have just added the docker file and build spec. yml file okay so this is the same repo if you check here if will go to the terminal you can see here right this is the same repo fine now let's open the build AWS code build and start creating a project because if You observe what AWS code build needs he need a build spec. yl file and whereever build spec. yml file is there it is in the geub okay so I need to give the complete info about where EXA L my build spec. yml located to AWS code build so just go here give the project name I will give something like course service or course project then who is the source is my GitHub what is the G of repo is the aw cicd this one okay then nothing to do here now you need to create a role so I'll just follow some pattern to create a role role name upend with service hyphen role okay now here either you add the builds p. yml manually by switching to editor or just use the buildpc yl file which is there in this particular repo okay fine now nothing to do just scroll down and create build project okay so this role is already there course app something like that I can give so you can see here project created now just close it now I can start the build okay but this rule need few more more policy to attach in this particular role okay so for that either go to IM am and search for this particular role or just directly navigate from here now here click on ADD permission attach policy search your container then just choose Amazon ec2 container registry full access and Amazon ec2 container registry power user these two policy we need to attach to our rule just add the permission all set okay now if you will go to your code build project now if will go to the course project now you can start the build so once you'll start the build what will happen a code build will read your build spec. yl from the GitHub and you'll execute the steps you have defined okay what steps we have defined create the docker image and push it to ECR so let's run the build and we'll validate whether our CI steps is working correctly or not continuous integration specific step St is working correctly or not okay just go here start a fresh build just click on tail locks to check the last locks you can visualize the phage currently it is on pre-build phase it will download all the dependency because it will execute mbn clean install so it will just download all the dependency then if You observe running command equal logging into Amazon ECR okay that is what we have trying to log to e in the pre-build now it changed to build phase can you see here and in build phase whatever the steps we have defined it is doing that it is building the docker image and now it is in the post build it is trying to push the image to E so you can see here the status is succeeded now let's go to our e and validate whether the image is pushed or not okay just open the registry yeah can you see here we can see the image with the two different tag one is the latest tag and another one is the with the image tag okay that is what we have pushed the two command of Ducker P okay we can see the image that's great so if you check the flow we're done with AWS code build it means our continuous integration part is working as expected we are successfully able to dockerize the image and push it to elastic container registry using AWS code build now the next step we need to focus on continuous delivery and continuous deployment so for this flow I want to deploy my application to AWS ECS so I need to create this elastic container service infrastructure in my AWS first okay that is what my fifth step once the environment will be ready then only I can perform the CD step I mean I can deploy the changes fine now we need to create the AWS ECS just go to the browser go to the AWS console open this elas container service now here you need to follow some steps you need to create the task definition then you need to create the cluster then in the cluster you need to add a service and from the service you need to run the task definition again if this is confusing you stop the video and check out my AWS playlist I have clearly explained about ECR ECS all the services just have a look and come back to the video again okay so first let me create a task definition create new task definition you can give any name let me give like course task and launch type I'm using a forget because that is serverless I can use that and I want to reduce the task size memory allocation then I need to to give the container details okay where exactly my image are present so my image are present in this particular registry what is the name of that registry Java key hyphen registry just give that name and what is the image URI I'll directly give the path of my registry okay so that when the build will be initiated it will automatically pick the latest image from the repo I could have any number of images here right always it should take the latest image so rather than hard code the image URI I giving the repository URI so this is what the URI just give it in a task definition fine then just add the port 8080 to access by The Host then nothing to do just click on create now the next step create a cluster click on create cluster then give the cluster name I'll give Java cluster you can give any name okay there is no restriction now choose the infrastructure as AWS forget then nothing to do I believe all good click on create it will take few second if you want you can check the status of your cluster creation just go to this cloud formation and you can visualize the status now can you see here the create state is completed now if you'll go and check here you can see the cluster here now from this cluster we don't have any task no task is running right either you map task directly to this particular cluster or just create a service and map the task to that particular service both the way you can do that okay so just go to the cluster now here you have option to map the task and you have option to create the service okay so just click on this create service the cluster name is this launch type I want to change it to ESS file Target then application type is service to this particular service I want to assign the task which I created that is what I mentioned right you can do it on two different way directly play with the task or create a service map your task okay then what is the service name you want to give I'll give something like course hyphen service B2 something like that okay then anything else let's see expand the networking you need to create a new security group and here you need to allow all the traffic coming from outside to access your resource okay that bypass you need to add here all TCP from anywhere that's it just click on create again it will take few minute to create a service for you so you can see here right the service has been deployed successfully now go to the cluster and just click on this link then if you'll come to the service inside service you will find a task okay can you see here now just open that you'll find a public IP just open that address so this particular project which I'm trying to run in AWS ECS I have added a open AI dependency so that it will give me the Swagger that will be easy for me to testing rather than hit the end point from the postman okay so let's try to access the Swagger Endo 808 8/ swager hyen e. HTML so if You observe here it is loading my end points let's try accessing the post since this is inmemory collection we don't have any record prepopulated so let's add few record let's say course ID one name is equal to Spring okay price 500 let me add another two so we have added two records the status is 2011 created now let's get the endpoint I mean get all the course we got the result okay so endpoint is working my application is successfully able to run in AWS EC is but the deployment step currently we have did it manually okay for example let me show you one thing now if let let let's assume I want to do some code change that I don't want to enable the health check just comment it for now now if I'll commit these changes disable I'll just commit and push these changes now as for the flow once you push the changes immediately your AWS code build will trigger the pipeline will be initiated here okay and then we'll observe whether the new image what is pushed to ECR is being deployed to AWS ECS or not okay because we created AWS ECS instance let's see whether the new changes is being reflected the image which is there in AWS ECS or not okay so if you'll observe here it will immediately start a build let me show you just go to the course project build project so if You observe here let me refresh this the build is not getting started so if we let's let's check the config ation Project G okay so we just need to add the we hook reveal every time a code changes is pushed to the repo that is what we need to inform to code build saying that any push you found or any new commit you found to the repo just start a single build okay remove the filter group we don't need it let's not touch anything just update the project project just go to the build project now let's add some fake commit and we'll see see here it immediately started the build the build number is two now if you'll open this down download Source pH then it is pre-build once pre-build is done it will again execute build and post build so you can visualize the logs here so if You observe here build is succeeded so the phase is completed now to validate that go to your registry and just refresh it you will find the different image okay the recent image not this one the latest one okay now as for the flow this particular latest image which is there in ECR needs to deploy to ECS then only whatever the changes I have just added disable the health check should display in the response right so let's validate it just go to your Swagger just refresh it the changes are not there still my elastic container service pointing to the old image which is there in ECR it means we have done the build process automated but we have not automated the deployment steps yet whatever the image is the latest one I want that to Auto deploy to AWS ECS so creating a AWS code Pipeline and conf configuring the deployment step is the last step okay in the a code pipeline you need to inform who is the source from where you are getting the code that is GitHub now who is your build tool for me build tool is the AWS code build now where you want to deploy your changes that is AWS ECS all these three information you need to provide to AWS code pipeline Okay so so what we will do go to the console and I will just open code pipeline next just create a new pipeline give some valid name I'll give course pipeline B2 something like that then service rule let it be let it create a new rule for you then anything else to add no so if You observe here we need to follow five step step one create the pipeline definition two just Define see here add Source stage just tell to the code pip planine who is your Source from where you will get the updated code that is nothing your G geub right similarly you have a third step that is build stage who will build code for you or the task for you AWS code bill right you need to Define that now add deploy stage where you want to deploy your changes that is what you need to Define in the fourth step once you have defined all then just review and complete it okay so let's see now click on next now Source provider is GitHub for me connect to the GitHub you need to authorize your AWS account from the GitHub then what is the repo my repo is AWS I cicd the branch is main I'm going to use the GitHub web hook Let It Be click on next then who is your build provider for me if you understand it correctly who is our build provider here AWS code build who is doing our continuous integration step CI right so that is my code provider or build provider if you have genkins you can also do that now we have created a build code build project right if you remember we have created this project this one right so just you don't need to copy it will be Auto appear to you just go here choose the project name then anything else no click on next now what is your destination where you want to deploy your changes I want to deploy in the Amazon ECS elastic container service now what is the cluster name you have created Java cluster what is the service name we have created here if you'll go here what is the service name course hyphen service hyen V2 right it should Auto suggest here yeah can you see here then nothing to do just click on next submit and just create the pipeline so you can see here right this is the pipeline First Step was source so you can see here source is GitHub now the you can see the this buffering right it is on the Fage two that is what the build Fage who is doing the build for us AWS code build if you want to see the details you can just click on this view details you can just find the logs here okay that's fine just close it once the build will be done the Mark will move to the next one that is deploy stage yeah can you see here it came to the deplay stage and where it is deing the Amazon ISC is let it deplay then we'll validate whether the changes are there or not as part of the changes we have just disable this right let's validate that then we'll do some code change and we'll see whether that is really automatically getting deployed to AWS ECS or not let's see this deployment might take one or 2 minute so we need to wait see the last comment we can see here right if you see here click on view details view in ECS to watch the complete log just wait it to complete so it looks it's taking more time so if you want just check the logs so it's loading the project let's see so it could be because of the network glitch but let's wait it to complete to valid idea so I believe it's done let's see yeah it's done right you can see here all the three status is green Mark I mean the build is succeeded so let's our goal is to validate the changes is getting reflected or deployed in AWS ECS or not so just go to the ECS close the other one which we open now in the Cs go to the task go to the service or just open this you can find the IP just open the address I'll copy the URL okay can you see see here we disable the health endpoint which is not appearing in the latest version which was deployed to AWS ECS right it was Auto deployed now to cross validate again let's do some more changes and we'll validate in a one go that how CA is working and CD is working whether really the things what I just commit the changes to the GitHub is getting auto trigger the build and deploying it to ECS yes or not okay so what I'll do I'll just enable it again along with that I will also add some test endpoint okay tast or welcome something like that fine now let me commit these changes so let's give the valid message uh commit message new end point added something like that okay tick mark what you want to push commit and push commit anyway and push the changes okay so once the push is done if you'll go here first go to your code build okay see here the Let me refresh this yeah can you see here why there are two different builds going on let's see the commment message for this okay let it be anyway the latest one is also going on so you can see here the status is Success now all good close it and it should trigger the pipeline automatically see here the build is done right let me refresh it so automatically it is started the first one is done second one is nothing your build AWS code build which is going on actually if you see here build project it's done here so it should be reflected here within a second yeah it is done the build is done now it is in the deploy pH so let it complete it will take again 3 to four uh minute you can see here new endpoint added that is what the latest push we have added to our git so now if you let it be let's go to the flow now if you understand here as part of this particular cicd being a developer I focus on writing the code and pushing my changes to my repo okay next to that do the build generate the image push it to ECR then get the latest image from AWS ECR then deploy it to AWS ECS the entire process is automated I did the infra setup once going forward I just need to commit the changes or push the changes to my repo everything will be automated by this cicd tool AWS code build and AWS code pipeline so let's validate that once the deployment is done so we need to wait so if You observe the deployment is successful it almost took uh 3 and A2 minutes for me to complete that's fine let's wait let's let's check the result okay let's open this then just go to the task open it get the IP just type 8080 SL Swagger so here is the result the health we enabled again and we have added this new endpoint right so try it out just execute just a string message fine it means CI and CD both are automated using AWS code Pipeline and code build so this is how we can perform cicd in AWS Cloud infrastructure using AWS code Pipeline and AWS code build whatever the source and destination you are using that doesn't matter you must need to use this code build and code pipeline services from AWS to do a continuous integration and continuous deployment so this is really de D if you'll understand this particular flow you can connect the dot correctly and you can configure the service correctly okay so you need to understand only the flow what sequence you need to follow okay so that's it do let me know in a comment section if you guys have any doubt that's all about this particular video guys thanks for watching this video meet you soon with A New Concept