Deploy Dockerize Django App on AWS ECS Fargate using GitHub Actions CI/CD Pipeline and Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome again to my YouTube channel in this video we are going to learn how we can dockerize ajango app on awcs so we will be using GitHub actions and terraform for creating our infrastructure so if you have not subscribed to my channel just subscribe it okay so you get uh latest uh updated videos okay so no we have to get started so first of all I will be you know using lot of commands in this video okay I have also written article on on my LinkedIn okay which is added in this video description so you can get it so like all the commands that I am using so you can just copy and use it okay so first of all what we have to do here okay we have to create an aw cc2 instance okay because we are going to use GitHub actions as a self hosted Runner okay so first we have to create E2 instance then we will set up GitHub action self posted Runner there okay so let's move with our first step I will just go and create ec2 instance by the way this ec2 instance will be used for uh cicd pip plans like it will help to execute GitHub actions to get a build uh trigger and it will push images to the aw CCR also it will uh deploy application on AWS ECS forgate okay so let's uh get started with the ec2 instance I will just click on launch I will give a name um git because we will be using it for the GitHub purpose uh just one two okay I will be using uh T2 medium and here I will create a new keep here just give a name uh get yeah that's fine so I will just download this file this file we will be using to SSH to our instance okay then we have to create a security group okay so just allow the traffic okay next we have to configure the storage okay so I will be uh using 30 GB of storage okay and next we have to just click on launch instance so our instance is being created okay so I will just go to the instances okay so it will uh take approximately 15 seconds to get ready so then we are going to uh install some packages on AW cc2 instance it will be a Docker terraform and awli okay so yeah I think we need to only install uh Docker and uh yeah Docker is enough then tform and we can also install aw CLI so just we have to wait here okay so it is ready just click on connect Okay so so you have to first of all run these two commands which is at Point number three and this one example so I will just copy this command and I will go into my terminal in my terminal okay I will just go to the folder where my uh pin file was downloaded like recently uh 10 seconds ago we have downloaded okay uh that SS file okay so I will just go to the folder where it was downloaded okay now I will just execute that command next I will uh get the second command which is this one example one just copy this command okay and then you have to execute this command then you will be connected with your ec2 instance okay then what you have to do here we because uh we have to build images on our GitHub actions okay so we are need a Docker here okay so we will be installing Docker okay and also we will install aw CLI uh we don't need to install tform uh because uh we will be uh creating infrastructure from our local system okay so simply we have to just run few commands first of all we have to update uh our U obento System just run this command sudo AP update okay then we have to install the docker okay so just wait for this command to be executed okay done and next we have a command which is it will install Docker okay then we have to uh run another command to create a username for our docker okay so we have to run this command now after this command we have to Simply run two more commands to get a Docker system ready okay so we have to also execute Docker when our instance is started or stopped so our Docker system is you know set up okay so if you want to check the docker variant you can check it okay so you know this Docker Varian is installed okay next we have to install aw CLI so first of all I will just run a command here okay so we can install and then we have to execute this ZIP file next we have to unzip this file and then at the end we have to execute a command which is this command Okay so then our aw CLI will be installed so if we want to check I think we can maybe uh check the variant using a command okay so we can see if AWS is installed okay so aw CLI is installed on our system okay so next what we have to do here we have to configure our aw CLI on our uh open to server okay for this we have to create a secret key and access key okay so we have to go to the AWS console okay just search I am okay here you have to go and we will be creating a user okay so just create a user I will say YouTube okay YouTube video okay so next we have to click on next attach a policy I will attach administrative access next we have to create this user okay when this user is uh created then we have to uh create and you know access key and secret key just click on create access key okay so you have to you know give access as a uh AWS you know Comm online interface face okay that's fine then you have to just click next okay just give name I will give name uh YouTube create access okay so we have know this access key and the secret key okay make sure you download this file okay because this access key and secret key we will be using when we will configure uh ew CI on our local machine as well during terraform infrastructure creation so first of all what I will do here I have to copy this uh uh you know these two keys and execute on our uh UB to server okay so we have to first of all configure okay so just go to uh to the terminal of ec2 instance here we have to write AWS okay configure it will ask you first of all access key ID okay you have to just copy this first one okay and then you have to and then you have to go to the terminal then you have to copy the secret key so this is our secret key okay just execute it and then you have to choose the region okay so I'm going to to choose a region in this video okay so if I show you on ews okay I'm going to uh choose a region which is uh my I will say I have created a ec2 instance okay make sure you uh type A same region where you have created your ec2 instance so I have chosen Us West one California region okay so I will uh input Us West one region okay done okay so our aw CLI is integrated okay on our one to server next we have to set up our a GitHub actions as a self hosted Runner okay so just keep uh going with me okay it will be a you know nice experience uh how you can set up these things so next I am just going to my GitHub okay so this is my GitHub rvo if you want to clone my Rao just clone it okay then you can also create a GitHub actions all these stuff so I will just go to the actions uh sorry I will have to go to the settings so into the settings just click on Runner okay we have to create new selfhosted Runner okay select Linux okay so here you have to just run commands like uh you don't need to run this optional command not needed okay then in the configure section we will uh run only this command we will not run this last command Okay so first of all just copy this okay and you have to execute in your terminal okay done and next we have to copy the second command okay which is this one okay it will download the latest Runner package next you have to just copy the third command as well which is uh this one extract the installer okay so just uh run this command Al so next we have to copy uh configure this command only okay and just go to your terminal okay it will configure okay so it will authenticate with your uh okay just click on by default uh just enter press enter enter enter enter okay so our runner is successfully added to our ec2 instance okay but we have to know make sure how like if our E2 is stopped or restarted okay so our GitHub actions uh runner should be Auto started so for this we have to configure a few things okay so I'm just going to uh create a file here okay it will be a system service file okay so this file will help us to uh Run keep our uh GitHub actions uh running okay so it can trigger so okay so so I will be just uh going to paste some content here okay so we have this uh okay so this this is available in my article as well so you can just get copy okay so this is you know working directory as we have OB to home to which is our working directory okay and here our GitHub actions are downloaded this our GitHub actions folder name okay so and our username ISU okay if you're using ac22 server then these configurations are fine okay next what we have to do we have to just save this file and exit Okay then this file is saved okay so we have to run few command to execute uh this okay so first of all we have to reload okay okay so we have to reload okay and then we have to enable uh this script there script is enabled now we have to start this script okay then we have to check the status if the script is running or not okay so you can see our script is running in case if you have script not running you will need to check if you have uh the content of uh the the script okay run script. service file is fun or not okay so you can just check the status so our get GitHub self hosted Runner okay setup is also done okay next what we have to do here we are going to do some stuff okay uh we have to uh create a DB password for our Jango application like the passord we are we will be going to use post gql okay so we will be you know having uh some uh AWS secrets we can store onto the SSM okay so just run the this command I want to you know store my uh RS password okay uh secure okay so just you have to run this command but before you run this command make sure uh your region is fine okay uh this is a password admin 1 2 3 for my password for my DB password okay so like I will use this uh path in my terraform okay terraform and I will be creating AOS I will show you okay just create this password into the AWS ssmc secret okay so we can store this uh parameter value okay so as you know I'm using uh us vest one okay so this will be creating in this region okay so I'll just hit enter okay so it is created so if you want to confirm you have to just you will need to go to to the AWS okay and okay you have to search AWS parameter store uh the this one okay so you will see this uh DB password secret is being created onto the parameter store okay so yeah this one this is Jango API okay so we will make sure in the terraform we have this uh path at it okay so if I just go to uh my you know V Studio code okay so this is my Jango project and this is my terraform okay so if I have already added this uh command here as well if you want to use okay and next we have to just look into the you know I have a modules based okay like ECS RS everything okay uh and also the variables defined here okay so next we have to I will just show you one thing here which is uh if I go to the RS okay you can see this was the DB password okay this was the path I have given okay make sure this path when you run the command the PA is same okay so then you can just replace it okay next what we have to do here just follow me and we have to create AWS ECR repository okay so just go to ECR so we can create a Docker repository okay container registry so and then we can push our Docker images to ECR so this is our next step create a repository I will just give name Jango app make sure you all also give this name because I have used this in my terraform okay if you not if then you have to replace uh the repository name into your doc terraform infrastructure okay so just uh you know um you have to create this repository okay so here what you have to do you have to just copy this okay you have to just copy this path and if you go to the Tarra form okay and into the main.py file okay you will see this is the app image name okay so if you have to replace it here okay whatever the image name you are going to use okay so this is the tag I will be using for my production okay so this is uh you know all the ECS app configurations by the way like on which uh uh Port our app will be running our app our Jango app will be running on the Port 8,000 okay we have to push our image to the aw CCR that we have created okay so in this you know uh this is our uh deployment to the ECS forgate okay we have to first of all uh update few parameters okay so we can uh push our Docker image to the ECR okay so just follow me okay you have to just update this region name here okay Us West one okay and then our uh Jango app this is fine you don't need to change okay and then we have this uh configurations for pushing our Docker image to to the uh awcc okay because the paths are defined like in the Jango app I have a build process where my Docker file is okay so what I have to do here I have to uh I will just comment this because we are not we are first going to push our image to the ECR okay because we are not going to deploy image right now to the ECS because our infrastructure is not ready so okay so this is our GitHub actions for pushing image to the ECR okay so which is uh ready okay but before we uh you know push this code to the GitHub to trigger okay so you have to go to the GitHub okay and go to here you have to go to Secrets okay just click on secrets and variables Okay click on actions you have to add two Secrets AWS secret uh you know access key ID and secret access you have already downloaded this uh here okay so if you will just open this you can just uh use this okay if suppose if I open this okay and I will show you how you can also add it you have to add a new repository secret okay but I have added these two Secrets aw success key I will just update them okay so suppose these these are the access key secret okay I will just copy it okay and then I will just go to access key okay I will just update it okay so you will need to just add these parameters from the start just add you know click on new repository give a name and give you know uh this so we have to just click on access key as well okay so I can just copy the access key here this is one and so I want to just make sure I copy the correct key copy okay done so so I will just paste it okay so this is a two things you have to add into the GitHub actions variables okay so when you are done with it okay you have to just go to uh to the whe Studio code just push the code okay so now you will see the magic that how it is going to you know publish our Docker image to the uh AWS ECR okay by the way if I show you our Docker image which is a available into uh here okay in the build process folder these are the you know configurations I have written okay like uh it is getting python your Jango project and executing some commands Okay so let's go to uh to the okay so if you just refresh okay you will see a GitHub actions okay just okay you can see get up actions is uh started trigger okay no it is running on ec2 okay because we have already set up GitHub action self posted Runner okay no it is you know building you know uh it is it will build and tag uh the image okay and it will push to the ECR so if I also show you you know ECR you will see uh don't we don't have any image right now okay when this uh job will be executed and you will see then the image will be available onto the ECR okay so it can take you know 2 3 minutes okay uh when you're first time you know doing this uh image build so we have to wait here so here you can see our image is uh being pushed okay so so if I just uh go above here okay so you can see here and uh yeah it is just executing so it will just complete it so if I just go to ECR and refresh it you will see the image okay so this is image available okay so which is nice okay so yeah our uh GitHub actions also successfully completed okay so our image is ready next we have to quickly do one thing here we have to create our infrastructure okay so before you create our infrastructure make sure your AWS CLI is installed on your uh Mac on your whatever the system you are using you can just Google it how you can uh install aw CLI on your system okay if it's not installed okay then when it's installed you have to you know configure AWS uh CLI okay AWS configure okay so uh we have already the secret key access key and secret key so I will just copy that key access key okay copy and I will paste it okay then we have a secret key okay just paste it and I will give name Us West one okay this is our region name so we have successfully you know added it and then what we have to do we have to create a new workspace for our terraform okay so I will just close this terminal and open the terminal again okay now we have to go to the terraform folder here we have to create a new workspace I will just run this command and we'll give a name to the workspace I will just give name uh Us West one okay so this is my workspace for my terraform infrastructure okay so the workspace is created okay now I have to select this workspace so I can create infrastructure select Us West one okay then now we have to initialize our terraform okay so our terraform if I just show you the terraform folder you will see when we run the command you know uh a terraform in it okay it is just initializing okay all the states are being stored okay so we have to just wait here then we have to when this command is you know successfully executed okay by the way if if the terraform is not installed on your system you have to install it as well okay so uh make sure AWS CLI and AWS uh you know terraform is installed uh when you're installing on Mac it's a brew install terraform it's very easy to install terraform and uh uh aw CLI by the way I have a video on my YouTube you can just check it okay so you can install how to install these two things things next we have to you know execute a commands to check our terraform code is fine or not just run terraform plan it will show you all the you know uh changes that is going to be applied onto your ews account okay so that's fine okay so you have to terraform up apply Auto approve okay so this is going to create infrastructure on our AWS okay so you can see it can take approximately 10 minutes to create whole infrastructure AWS ECS and auds database okay all these things those are mentioned into the modules okay ECS okay networking okay VPC and the S3 image okay all these things are going to be created so so you have to wait it can take 10 minutes 15 minutes and then the based on your internet as well okay so I'm just um you know stopping the video here so we I'm I will back when it's ready so you can see still it is creating you know AWS D instance okay n Gateway okay so if I show you one thing if I go to my AWS okay I go to audus so you will see here like how the audience is being created okay so this is one example if I just uh you know wait here also I will just show you the how the aw ECS fargate is being also created okay so I will just click on De instance so you will see the whatever the instance is being mentioned into our terraform okay it is being created here okay so and if I just go to ECS AWS uh elastic container surveys so you will also see our ECS is also being created there okay also yes yeah you can see uh this ECS cluster is creating which was defined in our infrastructure if I go to the terraform uls ECS okay this ECS service okay and ECS everything related to the ECS Tas definition ECS service okay everything is defined here okay so yeah it will take you know 10 to 15 minutes to get your infrastructure ready okay so our infrastructure is uh created now okay so first of all if you uh see any error Okay so I I want to add a few things here maybe you can get error related to the buckets name only because buckets name are unique on AWS so like in a main.py file okay if you see this bucket name okay make sure you can just write a unique name okay in case if you get got got any error okay also in uh back end where our uh uh terraform states are being stored okay so here as well you can all Al replace the name uh uh which can be UN in case if you got any error okay so our infrastructure is created okay so next what we have to do we have to just go to the GitHub actions okay we have to uncomment our deployment code okay and just uncomment it okay then you have to just post your code new changes now you have to go to the uh GitHub okay so these were the two buckets that were created okay so now we have to go to the GitHub actions so we can see a new GitHub actions will be triggered okay what this GitHub actions will do this has two jobs one is it will build and push the image to the ECR and second deploy to the ECS okay so if I just go here to the S3 bucket uh ECR okay so we can see if a new image but every time uh you know um new repositories are being uh uh new images being pushed to the ECR okay it will be automatically stored okay so if I just go to this one yeah this is our image so it is being uh you know available here okay so we have to just know see how you know deployment is also successfully done okay what we have to do we have to just go to our ECR uh ECS so we can get our public uh uh host URL so we can access our Jango application okay so I will just go to the ECS okay and I will see if it is successfully deied okay okay it's in a pending okay so when a new when a task is being updated okay it is in a progress okay if I show you into the gab actions uh if I show you may be here okay you can see when uh New Image is post then what we are doing we are going to update our service ECS service okay so it will you know start a new deployment okay so which is currently in a process okay so this one okay it's saying in progress okay so if you see any error okay you can also check the logs okay so suppose if we go to the logs and we see uh one interesting thing which is production okay yeah one thing we forget to add which is a uh DB host DV host okay so we got this error okay we have to fix it so what we have to do we have to go to uh to the AWS database okay refresh it and then copy this in point okay and go to your Jango application if I go to my Jango application into the backend folder into the settings maybe I can see my database here okay yeah you have to replace this host name because every database has a unique uh uh host name okay other things like my database coulde with move everything in which is mentioned into the uh terraform infrastructure so you don't need to change anything just change this host name okay then you have to trigger at the build Docker image again and deploy new uh deployments on awcs okay new changes just I will commit it and post the code to the GitHub so now we have to just go to uh our that's where you can you can see it was you know uh creating issues that uh our deployment was not successfully you know done so we can see the logs okay log saying that DB is missing okay so now we have to just wait okay so if I just go to get up actions okay it is being no deployed a new image which is being posted done done done okay and now we have to just if I refresh my ECS cluster in a 10 second 15 second you will see a new deployment will be in a process uh wait here till it's deployed so yeah this image is yeah able which is fine okay by the way if you want to see how this image is being taken you have to go to the task definition okay and uh this is uh our task ECS app task okay which was created okay and here we have a image and all these things so if you want to just see click on a new revion okay you will see this was created by the terraform okay so this TOS configurations and this was assigned to the ECS cluster and ECS service this is this our cluster and in our cluster we have ECS service okay so ECS service okay new deployment is in a process okay if you want to see the logs you can also check the logs okay so it mean our application is going to be executed you can see all these migrations were applied uh yeah it's uh running now Okay so you have to just wait okay and one more thing like it is completed if I just go to the configurations here okay you will see the this is our DNS name because we are using uh load balancer here okay so if I just copy this and go to browser maybe if our application can be accessible so maybe we need to wait here uh sometime yeah that's fine yeah our application is not applied okay yeah it's very simple I just make it very quickly as soon as I can make this video okay so you have if you are just going to you know uh deploy your own application okay just make sure the structure of the project is like mine okay so you can you don't need to get any problems okay like this is my Jango project okay and here you have you know uh my project okay because the build process is very important here okay like in the docker you will see uh I am copying uh you know uh the requirements file to my Docker path okay and also uh working directory I'm setting okay and also I am you know copying these stuff just make sure you know to avoid any problem if you can you know handle this uh okay that's fine also if you want to execute this locally to test your uh Jango application okay there is a Docker compos file okay on my um GitHub okay there is uh uh Jango start water kit okay which is available if you want to use okay you can also utilize it how to execute uh you know on uh using Docker compose okay I have also created a video here okay on YouTube so if you want to run uh test Docker image locally so you can use that my video okay and yeah I think that's that's for today and uh yeah one thing more if you want to uh deploy your U you know if you want to set up a domain and SSL okay AWS Route 53 and AWS SSL certificate you can also set up it it is available in my video so you can check out this video as well if you want to uh set up domain and SSL so I think yeah this for today I hope you like this video okay and don't forget to subscribe and share this video okay I will see you in my next video have a good day bye
Info
Channel: CodeWithMuh
Views: 701
Rating: undefined out of 5
Keywords: eploy Dockerize Django App on AWS ECS Fargate using GitHub Actions CI/, terraform, aws ecs faragte, deploy application on aws ecs, aws ecr, Amazon Elastic Container Registry, github actions, aws github actions ci/cd pipeline, devops, cloud, aws, Deploy Dockerize Django App on AWS ECS, terraform on aws, deploy django project, github selfhosted runer, git, docker, aws ec2
Id: o22qYKDCGbU
Channel Id: undefined
Length: 36min 52sec (2212 seconds)
Published: Sat May 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.