AWS CodePipeline with ECS | Deploy Springboot app on ECS | ECS Fargate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks welcome to college in this session we will learn how to create automated AWS code pipeline we will create a simple spring boot application and we will deploy it into Amazon ECS using AWS Target as you see in the diagram we will first create spring boot application we will create a simple rest API we will add Docker file into our application so that we can brokerage our application later we will add builds Vector tml file into our application build spec.tml file is used by AWS code build it contains build instruction which is used by code build then we will push our code into GitHub after that we will configure code build we will configure source for the code build as a GitHub and code build will use build specter.yaml file to build our project and it will generate Docker image file and it will push that image into Amazon ACR after that we will configure AWS code deployed which will use image from the Amazon ECR and it will deploy into Amazon ECS after this we will create AWS code pipeline which will automate this whole process so that whenever we push some changes into our code it will automatically build a project and deploy it into Amazon ECS so here we are going to do a lot of configurations to make it simple I have divided this demo into multiple steps so our first step will be code setup the Second Step will be configuring code build third step will be configuring for deploy and Amazon ECS and fourth step will be creating a code Pipeline and automated so let's get started so step one to do a code setup in which we will create a code repository in a GitHub and we will create a spring boot application we will add Docker file then we will push that code into GitHub so this is my GitHub account let's create the Repository its Q2 name as springboot AWS deploy okay let's create the Repository okay now we will use spring initializer to generate our springboot project so let's use a maven let's give it the same name that we use for the Repository I will be using jdk17 now we will add dependencies so I will be creating a spring rest API so let's add dependency spring wave we will also require health check API so let's add spring actuator let's click on the generate now what we will do we will initialize git on the local machine let's go into terminal so I save my project into AWS demo directory so let's go into project directory okay so we are in the project directory let's do git init so it has initialized git Repository let's add the source code into git now let's commit okay so this will be our message now what we will do we will set the origin so you can get that command from the GitHub itself let's copy it and run it into terminal okay now let's push the code into GitHub okay so it is asking for username so you can get your username from your profile okay so my username is courage iPhone now to get the password we will generate access token so go into the profile in the setting select option developer settings click on the personal access token you will create a classic token click on Legend date of type classic and here let's give it a name m access okay we will assign a rights to it and let's generate the token copy that token and use it into terminal okay so we have pushed our code into GitHub repository let's go back into GitHub so you can see code is pushed into Repository now next what we will do we will open that project into IntelliJ foreign so let's add One controller into our application so let's go into our source into our base package and let's create one Java class I will name it as a test controller and let me add annotation that is rest controller okay now I will add one simple rest API here it will be of the type gate and it will return just simple message and message will be first message from AWS ECS okay we will add path it will be let's say slash demo we will also add path to our gate API let's give it slash data very simple so we have just created very simple rest API which will return a string message that is first message from AWS ACS now to make our application dockerize we will add Docker file into our application so just click on the root of your project and create the files file name will be Docker file so I already have a code for the docker file let me copy that okay now let me explain all the commands from the docker file so what does Docker file does it just contains the commands that is required to build the image so first command is from okay and this is our parent image and we will build our Docker image from this parent image next command is run that is APK add curl so what we are doing here we are installing curls command now why we required curl here because we are going to curl our health API okay and that curl command is going to run by AWS deploy service to check health of our application next step is we are adding temporary volume okay then we are exposing Port that is 8080 then last is we are adding jar okay so let me change the name of our jar let's go into form.xml and in the plugin called springboot Maven plugin after plugins add this line that is final name and our jar will be spring boot iPhone AWS hyphen deploy open service okay just copy this name and we will use same name in the docker file so let me copy it so what will this command do it will copy our jar from the target directory into image file okay and the last command is to run our jar file so let's change the name of the chart okay so this is how we have created our Docker file now what we will do we will build our project so what we can do just go into Maven section of your intelligent click on the install you can also build your project using command that is MVM clean installs so build its success now what we will do we will test our project now let's generate image so let's go into terminal so what will be common for building the image it will be docker build hyper t our name will be spring boot AWS deploy and add that dot okay so it is building it will take some time to build let me pause the video so it took around two minutes to build the image you can see image using docker images command and you can see our image is created here in the latest tag now we will run our Docker container for that what we will do run the command Docker run hyphen p 8080 is our exposed port and the image name now you can see our Tomcat is started on port 8080 now let's test our API in the browser okay so we can see our first message from AWS ECS now we will check one more API for the health of our application okay so what will be API API will be localhost colon 8080 slash actuator slash health let me write it you can see it is giving status as up that means our application is healthy so this is how we have completed the first step of our demo let's move on to step two so step two will be configuring AWS code build here we will first create a repository into Amazon ECR that is elastic container registry okay after that we will configure code build in which we will set up our source provided as a GitHub then we will also add a build spec.aml file into our application which AWS code build will use for the project build and at the end we will create a service role for the code build because code bill will require permission to push Docker image into Amazon ECR so in the Amazon console let's search ECR that is elastic container registry now let's click on the get started let's create the Repository so let's create a repository name as a spring and hyphen demo PCR okay and let's click on the create Repository so repository is created now what we will do next we will create a build spec.tml file in our application let me open the IntelliJ let's add build specter.yaml file at the root of our project okay so I have already code for it let me copy that okay now I will explain all the step from the build spec so build spec files basically consists of number of phases okay as you can see here it has pre-built phase then build and post build phase and at that you can see we have artifacts where we are going to copy some files and each phase going to run certain commands okay so let's go into pre-build phase so what it is doing it is just first logging some comments okay after that we are running ACR login command AWS code bill will push the image into AWS ECR for that it will require to login into service for that purpose we are logging into ECR depository okay so this is the command for that if you want you can get that command from the console itself so if you click on the your Repository view push command and you can see there is a ecl login command so let's copy that command and let's use that command in our build specter.yaml file okay now let's change the repository URI so from where you can get reposit to UI just go into Amazon console in the repository section you can see here there is a URL just copy this and paste it in our build spec.yaml file okay then next we are generating image tag now next phase is build phase so what we are doing here we are creating our jar using mvn clean install command okay and next we are doing Docker build okay it is the same that we done on our local system then we are generating tech for our image okay and after build phase we are doing post build phase what we are going to do in a post build phase we are going to push that image into our ECR so that's what we are doing here Docker push then the image with the latest tag then we are doing second pushed using the some other tag now why we are doing push for the two times here because whenever we push code into our GitHub we want our AWS deploy service to fetch latest image from the repository and it will fetch that image based on this latest tag okay now we are generating here some container name and we will push that name into this image definition dot Json file which will get used by AWS deploy service okay now let's push this image into our GitHub so let me open my terminal let's stop this container and let's run command git add okay then get commit message will be build spec added and let's get push okay so we have pushed with respect.yaml file in the GitHub now let's move into AWS console let's search code build let's click on the create build project okay so let's give it a project name so here we are going to configure our source so there are multiple option available we will use GitHub now here we are going to connect our GitHub with the access token and we will follow the same procedure that we did earlier let's go into GitHub into settings into developer settings personal token tokens gender token and let's queue the name AWS demo deploy okay let's assign some permission to it and generate the token let's copy it let's go back into build so just paste it here and save the token okay now we will need repository URL let's go into GitHub copy our repo URL let's copy it here so here we are configured our source Source version is optional so we will lift it to blank so let's configure our environment so operating system will be Linux too runtime will be standard image will be standard 5.0 now this is an important option okay so what it's saying enable this flag if you want to build Docker images or want your build to get elevated privilege so you want to build our Docker image right so let's select this option now this is important this is a role name just make a note of this role name okay because later we are going to require add certain permission to this role okay next option is build spec so we are using build spec from our project there is also option to insert build spec from here directly we are not using any artifact yes we want to use cloud watch let's assign some name spring demo okay and now let's click on the create build project now let's test it so here you can see the face details just click on that and you can see all the fridges currently happening in the background you can also watch the logs in the cloud watch as well just click on this view entire log so it is still fetching the logs let's go back into build we can see the face details here so build is failed now if you go into the cloud watch we can see you comment that we have added in the build spec let me go into IntelliJ select save this command okay so you can see it is getting logged now let's go back into our build if you see why this command is getting failed so codebill is trying to do login into AWS ECR okay and code build doesn't have permission to login into ECR so now you remember I asked you to note that service role so using that service role now we will add permission for the code bill so let's go into IM click on the roles and let's search that service role so this was our service role let's click on this and here click on the option for add permission attach policies so here let's search ec2 container you can see there are two policies Amazon ec2 container registry full access and second one is Amazon ac2 container registry power access let's select these two and add permission okay so we have added permission to our code build now let's go back into our code build and let's click on the retry build so you can also see the logs here as well let it build the project first but now you can see all our phases are completed successfully you can also see the logs here let's click on the trail log okay you can see whole logs here or you can see the lock in the cloud watch as well just click on The View entire log and you can see your log here as well now let's go back into our ACR Repository let's go inside this Repository and you can see image created by code build okay so this is how we have completed our step 2. so step three will be AWS code deploy in which we will configure code deploy if you go deep inside what we are going to do we are going to do this thing as shown in the diagram we are going to create a first cluster okay in the cluster we are going to create a service then we are going to create task definition task destination is a kind of a template okay and this template is going to use by AWS forget to deploy that application so let's see into AWS console let's search for the CS that is elastic container service okay let's click on the get started create a cluster let's give it a name spring cluster okay then infrastructure we are going to get AWS forget and we will keep all the option as default and let's click on the create till the time cluster is getting created we will create a task definition so let's click on the create new task definition let's give it a name spring demo task definition launch type will be AWS forget let's keep the option as default we will select CPU points five and memory is 1GB okay we want to create a new role yes in the container section let's give a name to the container okay so image URL will be let's go into our ECR Repository and let's copy this repository URI okay then next add as a container report so we are exposing 8080 and let's keep all the option as default one and in the hell check call to our health check API so I already have a command for it let me copy it okay so if you see here we are calling our actuator Health API and for this curl we have added curl in the our Docker file okay now let's go down okay so we will keep all the option as default one click on the create button okay so our tax definition is created let's go into cluster let's click on the spring cluster okay and let's create the service okay so we will be using launch type launch type will be forget you want service right then family will be spring demo task definition that we have created your excuse service name spring demo service okay then keep all the option default one let's go into networking we will select the default subnet and here we will select the create a new Security Group okay or else we will use existing one we will change it later on I want to show one error so let's keep it default one then we will not select load balancing we will keep all the other option as a default one let's click on the create so our service is created successfully okay if you go into the task you can see our task is running let's click on this task and you can see it has one public IP let's copy it and let's test our API so URL will be this IP address colon 8080 slash demo slash data okay so this will fail now and why it is failing because it don't have access so let's go back into the task configuration if you see there is a Ani ID okay let's click on that and if you see here it has Security Group as a default one just click on that and let's click on the edit inbound rule so here we will require to add rule for 8080 Port okay so let's add custom TCP okay or anywhere and here add 80 to 18. okay let's remove this one and let's click on the save rule okay now let's refresh our page now you can see we got the first message from AWS ECS right so our application is deployed successfully onto the ACs we have received a message from our application and if you go back into the services if you say this is service and if you click on it in the task you can see we are getting status as a healthy as well okay how this status is getting healthy because it is calling that actuator Health API okay so that command is important otherwise it will not show healthy status here so this is how we have completed our step three therefore is great pipeline so let's search code pipeline okay let's click on the create pipeline let's give it a name AWS demo pipeline okay let's assigned a new service role and you will keep other option as default one let's click on the next Source will be our GitHub so let's click on the GitHub version 2. and click on to connect to GitHub so it has opened the new pop-up let's give a name as a AWS code pipeline connection and click on the connect to GitHub okay let's click on the install new app now let me select the Repository I want to give access to our new repo that is spring boot AWS deploy click on the install now let's click on the connect okay so we have connected to GitHub successfully now let's select our depository name our Branch will be Main and let's click on the next and also this option is important start the pipeline on source code changes so it will detect The Source whenever we push the code into GitHub let's click on the next so build provider will be our AWS code build that we just configure so let's select the project name and let's click on the next deploy provider that we have created that is AWS ECS okay so our cluster name is spring cluster the service that we have created spring demo service let's keep all other option as blank let's click on the next so you can see we can review all the settings here and let's click on the create pipeline okay so our pipeline is created now it will start the pipeline okay so our first step is completed now it will do our next step that is a build step you can also view the logs by clicking here so you can see current phase is downloading the source another phase I changed to build okay so we get some error let's click on the invalid action configuration so it is saying container name spring demo container does not exist okay now let's go back into our build spec Dot yaml and you can see here a Docker container name is different okay so our container name is spring demo is here right so let's change it to spring demo easier now let's push this change and we can test our code pipeline that it is starting automatically or not so let me push this changes into GitHub so in the terminal let's add let's commit the change okay let's limit it and pushed okay so we have pushed our changes so now our code pipeline should start automatically let's go back into browser let's wait for some time it will start automatically so our first step is completed our build is started so our build stage is completed now let's go into elastic container registry and let's see how many images I created okay so you can see images are getting created the first image get created when we try the first time second image when we like we get the error in the deploy stage and this is the latest one let me push the code and this image is created just now let's go back into our code pipeline and let's wait for deploy Stitch to complete okay so our deploy stage is also completed now let's click on this Amazon ECS URL okay so we can see there is one task is running and let's click on the task click on this task and copy this public IP and let's close this tab let's run our API so IP address colon 8080 slash demo slash data okay so we are getting message that is first message from AWS ECS and we have successfully deploy our application using Code pipeline now let's do one more test we will add one more API into our springboot application so let's go into the IntelliJ in the test controller let's copy this method and we will add second message from AWS ECS let's change the method name to get message okay now let's push this code into GitHub so let me open the terminal let me add the changes commit the code new API okay let's push the code okay so we have pushed the code now our pipeline should start automatically let's go back into the browser so you can see it is started the first stage is started that is source okay so we have completed our first step then build stage started so build stage is completed the prostate is started now let's go into ECR let's check the new images created or not earlier we have three images let's refresh it now you can see here we have four key image that is with the latest tag okay let's go back into the pipeline and wait for the PlayStation to complete okay so our deploy stage is completed let's click on this Amazon ECS URL click on the task click on the task name and you can see there is a new public IP copy it let's hit our API so API will be colon 8080 last demo slash message okay so we got our second message from awccs so we have successfully deployed our application using Code pipeline okay now multiple things to remember here I have purposely added few issue to Showcase how to resolve that first thing when we configure our code build we need to add certain permission right otherwise it will fail to post or login into your ACR Repository that was our first issue then second issue was when we deploy our service we are not able to call our API so for that what we did we went into the task configuration check the eni ID and change the security group to allow 8080 code third issue was the container name okay so for that what we did we went into the Repository we check our spring container name and use the same name in our buildspec.yaml file okay so this container name we are going to push into this image definition.json file and this Json file is going to use by deploy service okay so these are the three main issue we saw so this is how we have completed our demo on the code pipeline in the next video I will show how to configure select notification folder for each stage okay also I will attach my GitHub repository URL on the video description that's it for this demo thanks for watching the video thank you
Info
Channel: kodEdge
Views: 8,681
Rating: undefined out of 5
Keywords: AWS Tutorials, CICD, DevOps, AWS Fargate, AWS Codebuild, AWS CodePipeline, AWS CodeDeploy, CICD Pipeline, how to deploy sprringboot application on aws, deploy springboot app on aws ecs, how to setup automated pipeline, deploy springboot microservice on ecs, Setup ci/cd pipeline on aws, How to setup aws code pipeline, Deploy web application on aws, aws code build and deploy
Id: ARGmrYFfv44
Channel Id: undefined
Length: 41min 49sec (2509 seconds)
Published: Mon Sep 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.