Deploy Django app on AWS EC2 with Auto Scaling Group, Load Balancer, Route53, and SSL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome again to my YouTube channel in this video we are going to cover very interesting topic which is deploying Jango application on ec2 but using Auto scaling and load balancer okay so let's get started so here you can see uh we are going to use Autos Skilling group to launch our instances okay if you can look into this uh diagram okay uh first of all if uh public users will try to access application we have we will have a load balancer okay what the load balancer will do it will equally distribute the application across different instances created by the Autos scaling group you will understand it better when we will be creating this Autos scaling group okay and load balancer so it can uh you know uh distribute traffic and these things so this kind of structure and we are going to you know create on ews so let's move to next what we are going to do in this video video first of all we will create you know set up our project configuration how the project should be ready for the deployment okay then we will be going to create an audious database on aw cc2 we will connect it with our project because our project will be going to you know use a different uh server okay because if it's based on Autos scaling okay if there's a three server two server are running okay so we have to manage a database uh through a one uh platform okay it can be AWS RDS and we will managing you know migrations and migrate locally by connecting with this RDS okay so and then we will uh install a PG admin tool okay which is a tool for the database management okay I will you know uh show you how we can connect with uh RDS okay this PG admin it help us to uh learn how we can connect and see our database tables okay and data okay it's very interesting and next in the step four we will be going to launch and template on AWS ec2 for our Autos scaling group okay and then in the step six we will be going to verify the load balancer and we will try to browse our application into the browser okay and then we will going to s for the instance if uh we need to update our code in the future we need to get some get pull okay so we will learn how we can connect with our instance and we can update our a server okay and then we have to we will be learning to how to host a domain okay using a Route 53 uh AWS Services okay and then we will connect this uh domain with our load balancer so we can browse our application using uh simple domain then if I you know uh in the step nine we will be creating an SSL certificate using AWS certificate manager and we will attach to the load balancer so we can um um browse our application uh which is which will be SSL based okay so SSL will be attached to our domain okay and what next it will be a surprise at the end of the video okay so uh let's jump into my uh whe Studio code okay yeah this is by the way this is my Jango application which is going to be deployed okay and the source code and everything will be available on my GitHub I will uh give this link into the video descript description okay so if I just open my will Studio code okay you have to understand how the structure is okay it is very important first of all what you have to do okay if you have your own project okay you have to copy this jicon folder scripts folder okay and user data Dosh these folders you have to copy and place in your project okay then what you have to do in user data you can see GitHub repo URL okay you have to replace the GitHub repo of your GitHub repo okay and if it's public you have to just you know um uh replace uh you know paste it okay if your repo is a private okay then you it will look like something okay like your GitHub repo and your username of the rep and the personal access token okay because uh this script will will execute okay automatically if it's a private you can you know replace with the username name and the personal access token how you can create a personal access token onto the GitHub okay if I go to my GitHub profile and I show you okay if I click on my profile if I go to the settings and then you have to go to uh developer settings and then you have to click on uh you know personal access token okay you can click on tokens and you can generate you know uh uh token here okay okay so you have to create token you can give any name okay GitHub AWS okay and you have to give per permissions okay like uh uh GitHub you know it should read uh you know your repo it should be cloneable okay so yeah you can give some permissions okay and yeah for admin okay everything that's fine okay so you have to just give the permissions okay and then what you have to do you have to just create a token okay when the token is created you have to just copy this token okay and you have to uh you know put the token here okay uh here in the personal token place okay so then you can also you know clone your private repo okay next you can see here my project uh main directory name which is this one Jango ec2 Auto scaling okay if your project directory name is different you have to replace it okay and make sure this is uh my manage.py file is in this directory okay in my Parent Directory if your manage.py file is in a subdirectory make sure to add that uh you know folder path okay so so you can avoid any mistakes okay so then uh you don't need to change anything in this uh uh file okay it will automatically clone the rappo okay and it will execute all these you know uh scripts okay then if I just go to you know uh jnicon file okay here you can see uh in the line number eight okay this is my project folder where my manage.py file is okay if your file is in a different folder okay make sure uh you give you know a correct path okay and then you have to you know in the second line you have to just give the Parent Directory name okay whatever The Parent Directory is okay currently I have this Parent Directory I have just written okay okay it is just for creating virtual environment okay so then in the line number 13 okay you can see I have uh given the core dot because my uh wsgi application okay which exist in this folder okay so I have just given okay because my parent uh folder is defined here okay then in this folder I have the score okay and the score and yeah if your folder structure is different make sure it's correct so this is uh in the jinon service file okay and then if I open the scripts okay you can see the you don't need to change anything in these uh you know after install file and and this is for jicon setup okay it will automatically execute okay similarly you make sure your U folder name is correct where your manage.py file is why I'm telling this again and again because mostly uh developers did mistake here okay they put wrong folders okay and they uh did not you know application deployment success successful but by the way if you you know feel like uh uh you are stuck somewhere okay so you can send me a message on the LinkedIn okay or you can comment in the uh video okay or you can send me an email I will be helping out you okay so this is file you just have to change this line and this is just for dependencies you don't need to change anything okay and here you have to change you know uh the folder name okay similar again like where the manage.py file is okay then you don't need to change anything in this folder it will you know install requirement. txt activate activate virtual environment okay and then in the start app okay similarly you have to update uh the main directory folder name okay and you are good to go okay so these are the you know uh some configurations you don't need to miss out okay and in the requirement. txt make sure jicon is added okay into your folder and in this tutorial because we are going to use uh post SQL database that's why I have added this Library okay make sure you don't forget to add this okay I will just add a comment so you know you need you need to make sure these two lines are added in your requirement. txt okay these are the things you you should care about okay and you have to run the project locally first make sure it is running fine there's a no bugs okay so before you Deploy on you know um on the server okay and then you have to you know we are going to know deploy application on AWS so yeah if I just go to my you know this okay yeah this is application that is going to be deployed yeah one thing if you are not connected with my uh with me on LinkedIn you can follow me on LinkedIn okay and I will be sharing nice articles and news on the LinkedIn also you can subscribe to my channel it will help me to create good quality quality content because it's you know get some time to create content okay and you can follow me on the GitHub as well for nice repos if you want to get every week okay so now I'm just uh going to create and auds database okay I will just search auds into the AWS console okay I am just going to create an database and I will connect this database with my Jango project so just create a database and simple standard create post create SQL okay just select this postre SQL okay and the varrior name we have to just chol what is the latest one and the by default and the free tire because I'm going to for just for this tutorial purpose if you have different requirements you can change it and I'm just going to you know give some name post okay this DB identifier and master username post gra and I will also keep the database name post gra so I should not forget any of the parameter okay when connecting with the Jango project you don't need to change anything okay if your project has different requirement you can increase the storage and you can also change the storage type depends on how your project is okay and then you will see okay you don't need to change anything just you have to make sure the uh Public Access is enabled okay uh don't forget to you know uh enable the yes option okay then you have to you know create uh Security Group here I will give name uh Jango and RDS okay RDS Security Group okay uh no preferences it will automatically create okay and simple and then we have to just scroll down okay additional configuration it will run on the port 5432 okay that's fine and in the additional configuration that's fine we don't need to change okay here we have to give the database name I will give post gra uh okay name make sure you give the initial database Name by clicking on this don't forget okay and I think that's it okay we are just going to click on create our database so it will take approximately 4 to 5 minutes to create uh this uh database okay meanwhile I am just going to show you what how we will connect with our Jango project I will just open my whe Studio code and I will just go to uh settings.py file okay and here we see the database normally when we create a new Jango project we have this kind of database okay but when we are using post SQL we have to uh this kind of uh settings okay engine is same we don't need to change and in the name I have given a post name to my database and the user also I given post gra and then my password was also post gra and the port was also 5432 just only the differences I have to replace my host okay because my instance is being created in a four in 3 to four minutes okay when it will be ready I will just copy this uh host URL from my in uh RDS okay okay so if I just go to my auds okay and if I go here okay so you can refresh okay it's just still creating okay if we just click on it you can see endpoint is getting ready okay we have to wait here meanwhile one thing important which is a security group just click on security group and here we have to make sure the inbound rules okay just click on this Security Group and click on in inbound rule so because uh database uh when we uh you know will apply migrations migrate okay database uh will only allow if our IP is enabled this is my system IP so that's why it is it will allow me to connect with the database okay but as you will be going to learn how to uh you know our ec2 instances has also need to connect with the database so I'm just going to you know change it to uh 0 0 okay so you have to just change it okay and when you are done you have to just go back okay onto the RS okay and I will just refresh okay okay my instance is getting ready okay okay I have to just wait here okay so okay you can see here okay inpoint is now available I will just copy this inpoint okay and I will just go to my settings and I will update this host okay so yeah I will just make migrations okay and first of all I will just need to activate my virtual environment okay and then we have to you know make migrations and migrate I have already applied migrations okay so I will just apply python manage. py migrate okay so maybe you will need to wait 3 to 5 minutes okay because if RS is totally ready okay then you can apply migrations and it will work okay so we have to just wait for 1 to 2 minutes meanwhile uh other thing which is uh uh interesting which is a PG admin so if you just uh you know open the PG admin okay just go to the Google and download the PG admin for your uh system okay if you're using Mac or different operator in system you just need to You Know download from official uh website of you know PG admin okay so when you done the download just open the file it will you know um you know create application okay so if you using Mac OS or different machines okay you can just uh download Based on your system okay when you download it okay you have to open the application because I have already this application I'm just going to open this application here okay basically PG admin is to connect with your database see how the tables uh look like okay and uh it is very helpful uh when you need to apply some operations okay so when your application is open PG admin is open Okay click on register a new server okay so we know our server name is post gra okay that I have created on AWS okay and then I have you know password and username is also post gra okay and the port then the host name okay host name I can just copy from my you know RDS okay this is a host name okay and I will just go to uh PG admin okay and I will just try to connect okay and it will be connected okay so if our database will be ready okay and it will be connected okay so if I just show you here okay databases and click on postc and schemas okay in the schema you have to just click on tables you can see there's are no tables okay let's go to the V Studio code we are going to apply a migrate command okay it will create a tables into our ews RDS database okay as soon as these uh are applied on to our uh database and I will see these tables into the PG admin okay so we have to just wait for our migrations to be applied onto the database okay so migrations are almost applied okay so if I just go and maybe refresh okay you can see that these tables are coming here okay this is a user table if I just open the user table you will see there is a no user currently okay because I have not created any super user okay so yeah first 100 rows so there will be no uh data it is you know connecting with the database and applying the cury okay so we don't have any users so I'm just going to create a super user okay clear okay I will just run command python manage.py create super user okay so I'm just going to create a super user so I can show you how the tables will look like admin okay super user is created just go to the pg admin again and then um apply the quy so you can see this is a user which is created so PG admin is a very useful tool okay because sometime you need to export the data okay suppose if you click on import export you can export as a CSV okay and download in your system okay so that's very interesting to use PG admin okay okay next uh we are going to you know deploy know our application onto the ec2 so just go to the AWS console okay I'm just going to the AWS console here okay so here you have to you know uh go to the EC to dashboard okay here you have to create a launch template okay if I just go to the launch template it will be add here under instances so I will click on launch template okay here you have to give Jango project okay and the Varan I will give 1.0 okay and then we have have to just go to Quick Start okay we have to select ubu okay and this we have to instance type okay we can choose T2 uh micro okay then uh you know uh we can create a keep here okay if we need it to connect with our instance later in the future okay I will just connect I will just download Jango project key okay so the file will be downloaded into your downloads folder we will use this later in this video okay so next we have to create a security group I will give name Jango project Security Group okay so this is a you know we have to give some this is uh Security Group okay simple okay and the VPC is which is already connected okay and we have to assign you know add a security group role and I will just go all TCP okay and anywhere okay so it will be easy to you know go through this tutorial okay so it will allow the traffic from HTTP https okay to that uh instance okay so this is a template we are going to you know create just scroll down and click on Advanced details and just scroll down and you will see the user data here is main thing just go to the whe Studio code and copy this whole file okay okay and copy and just paste it into the user data make sure if you are using private repo just comment this line and use this line okay uh for a private repo I'm using my public repo okay so I will just what I will create a launch template basically this launch template will be used in the Autos scaling group uh because if uh Autos scaling group need to create more instances okay uh based on the traffic it will you know uh copy it will use this template always to create a new instances okay our template is created okay then we have to you know create a Target group okay so we can Target our traffic uh from load balancer to the Target group so just uh create a Target group here select instant expenses okay give name d Jango project okay and just leave it HTTP protocol okay that's fine he check click on advanced settings that's fine okay so just you have to click and create this uh Target group okay so because we have not created any scaling group just leave it okay just create a Target uh group okay so the target group is now created okay now you have to just go to the load balancer okay and you have to just click on load balancers okay and then here you have to click on create a load balancer okay because we are going to create application load balancer Okay click on create okay load balancer Jango load balancer okay and we are going to use internet facing okay that's fine then we have to you know mapping if you want to use for you know uh subnets okay multiple availability Zone you can connect I'm just going to use two availability zones okay and uh the security group that we have already created okay while creating a template so I will just select that sec group here okay and then we have to uh you know select our Target group that we have uh created okay we have to just keep the port 80 okay because it will be working on Port 80 for the HTTP protocol okay and then we have to just uh you know click and create this load balancer we don't need to change anything so our load balancer is automatically you know being being created okay that's fine now what we have to do here we have to know going to create our last thing which is Autos Skilling group in terms of deploying application just click on Autos Skilling group and create auto scaling group Jango Auto scaling okay Auto scaling group that we are going to create okay and we have to select a template here this is our template that we have already created okay and then you don't need to change anything okay because we have only the um template which is variant one okay so it is by default one next okay and then we have to choose the availability uh zones okay you can choose up to you know if you want to choose multiple okay for your uh Network okay then you have to uh just click on uh next you have to attach to an existing load balancer because we have already created okay this was a Target group and the load balancer which is connected okay just select it okay and turn on elastic load balancing health check okay then you have to just click on next okay here you can see the desired capacity when the Autos scaling group will be created it will create uh one instance okay so minimum desired capacity one and maximum I will set two okay if uh you know Target tracking scaling policy okay is reached suppose if the first instance instance has reached to 50% CPU utilization okay it will create our second instance okay so if uh CP utilization is decreased it will automatically you know terminate the second instance so this is how Autos scaling will work that's fine just create next we can add a notifications to get uh but I'm not going to create any notifications just skip it next okay and you have see all these configuration and just click on create Autos scaling group okay so Autos Skilling group is now created okay if I just go and show you yeah this is Autos scaling group and it is going to create E2 instance for us okay I can just go to the E2 dashboard and uh it will quickly you know start getting created okay you can see this instance which is pending it is going to be created by the Autos scaling group okay so we have to just wait for 1 to 2 minutes here okay yeah meanwhile I think we can do one thing here if you just go to the Target groups okay and if you click on this target group okay and just just click Target here we are going to attach a Target group okay so register a Target group okay this one and Port 8,000 because our application will be running on the port 8,000 if I show you in my project into the jicon file you can see our application will be running on the port 8,000 okay so I will just go to yes here is yeah uh included okay so this will be the target group the load balancer will you know uh try to run on this port okay so we have to just register a Target group okay if the target group at is created you have to you know D register it is not required because our application is running on Port 8,000 okay so we have to just uh you know go to our ec2 dashboard again and we can see our application is uh running okay our instance is running okay it is being initializing okay you have to minimum wait 4 to 5 minutes okay to get everything ready when everything will be ready okay you will just click on load balancer okay and click on this load balancer okay and you have to copy the DNS name okay and you will browse the application but I will recommend you to wait for 3 to 5 minutes okay then you can and browse the application so I'm just waiting for 2 minutes okay now let me try to open my application okay I have just air racket of my load balancer you can see this is how my application is deployed if you feel your application is not working okay it is not accessible any error it mean you did mistake you did mistake where you did mistake in the uh applying this folder names okay and uh this name and also uh in these files if where you need to you know Define your project directory name okay so make sure all these things are you know fine if still you feel you know problem okay you can reach me on the LinkedIn okay just make sure you sent a message okay so I know I can help out you and uh yeah so this is how our application is deployed using Autos scaling okay which is very great okay so next thing is how we can attach a domain instead of you know going to this uh uh what I will say going to this a racket so what do you have to do you have to just go to root uh 53 okay here we will be registering our domain okay so you have to you know create a hosted Zone okay in the hosted Zone because I have a domain on my GoDaddy account okay I will just open my Go Daddy account okay okay this is the domain okay I will just uh copy this domain and I will paste it okay simple and then I have to register this domain okay and this domain will be registered okay so what I have to do here I have to you know copy these uh name servers and I have to add on my GoDaddy okay so I'm just going to you know click on DNS okay I will just click on name servers okay so I will just make sure that my domain is properly connected so I will just copy one by one all these records okay I will just change the name servers uh just uh delete these ones first and the second is this name server okay we don't need to add the dot okay then the third one which is this name server okay that's fine and then we have to copy the last name server okay so we have to add it okay so we have to save it okay so it sometime you know it can take hours okay but normally it can take you know 5 minutes okay your uh domain will be connected with your Route 53 okay so when you update the name servers okay then what you have to do next you have to uh connect okay uh a load balancer with your domain okay so what I will do here if I just click on create a racket okay so because I'm using uh DNS of my load balancer okay so what I will do here I will just click on Alias okay and we have to use a traffic from our load balancer okay there will be application uh you know and classic load balancer just select it and then we have to choose the region because uh my application my load balancer is in a Virginia region okay okay I will choose that region okay then I have to choose the load balancer okay so I have to just attach this load balancer to my uh domain okay so my load balancer is connected if I try to you know access the application onto this domain maybe I can try Okay so because sometime it takes to uh propagate the domain name server to uh the application okay so we have to just wait here okay and uh it can get ready okay meanwhile I think we can install an SSL certificate okay and it will be ready uh in a shorter time okay so if I just go to yeah that's fine let me just open AWS certificate manager okay I will just write certificate manager okay certificate manager we are going to add a certificate okay I'll just go and uh because I have already you know uh connected with the uh certificate but you can click on request okay and next you will need to just write a domain name here okay and then you will just uh click on request okay what I will do I'm just going to you know delete it okay so I can just show you how it work okay so that's fine just go and this is my domain name okay and just click on create okay so the domain is being uh created okay so we have to wait or if we just click on it okay what we can do we can create uh this racket into our Route 53 we have already a domain so I will just click on create a racket into okay because uh when AWS provide certificate they provide us a c name record we have to add the CM record into our you know domain if we are not managing at Route 53 if you're managing on you know Cloud flare or GoDaddy we have to add this racket so I have added this ret onto the Route 53 because I'm using uh Route 53 okay so pending validation okay uh a take five minutes to get uh this ready okay so they allow they issue the uh certificate so if I just go back to Route 53 so I will show you this C name bracket is edit okay onto our domain okay hosted Zone click on it so you will see okay this C name bracket which is this one this one added okay so we have to wait for SSL to be ready okay and yeah it is getting ready so let's try to you know access the application again okay it's still HTTP okay it's still not accessible because it takes some time to get ready okay because it has to you know get connected okay because the name server are already added Okay so it can take some time so our certificate is also pending so I can just wait okay so there is one interesting tool like where you can check your domain uh propagation okay how it is being propagated and connected with your Route 53 okay so suppose I have a domain. shop okay so this is my domain if I check so you can see in most of the regions it is no accessible okay it is propagated and connected with the Route 53 it is still you know will need to connect with some other regions okay so uh maybe I can try know again if it is accessible or not in my country okay I think still it is not accessible so it need to be you know connected whole the you know uh regions in the world meanwhile uh what we can do here if you just go and to your load balancer because we have to create a new uh ret there so we can run application on https as well so what I will do here I will just go to my ec2 dashboard and I will just go to the load balancers okay currently we have uh if I click on my load balancers and if I just go select on listener and rules we are only running on Port uh 80 which is https what I will do I will also add an https as well okay and I will select the target group that we have already okay and then here we need to you know select the certificate okay we have already a certificate edit that's fine and we have to just add okay so this recet is also edit it will help us to run application on https as well so no I think almost every step we have done okay now we have to you know just you will need to maybe wait okay for 10 minutes for 30 minutes okay to get your uh domain be ready maybe I can try to start shop onto the https okay but uh still it is uh trying to connect okay so hopefully uh I can wait 1 minute more okay so I have almost waited for you know 10 to 15 minutes but in my region it is not working but I can show you one interesting thing I can if I have my another browser okay here I can try to you know uh use VPN uh in a US region so I can show uh it is accessible in the US region okay so let me yeah my SSL yeah I am connected okay so you can see our application is totally accessible on the domain as well so I think yeah that's for today and hopefully you like it okay and uh if you like it share this video on your LinkedIn on your social media okay also like this and comment if you if my video is helpful okay so I will be seeing you in my next video have a good day bye
Info
Channel: CodeWithMuh
Views: 1,218
Rating: undefined out of 5
Keywords: amazon web services, aws, aws cloud, what is amazon web services, Deploy Django app on AWS EC2 with Auto Scaling Group, Auto Scaling Group, Load Balancer, aws load balancer target group, AWS Load balancer, aws certificate manager, aws route 53, aws rds, postgresql database, deploy django project on server, deploy django project on aws, ec2, python, django, aws application load balancer, aws autoscaling, deploy django app on aws ec2 nginx gunicorn
Id: GVHZwqJvJiw
Channel Id: undefined
Length: 40min 6sec (2406 seconds)
Published: Mon Mar 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.