DevOps Tutorial - Laravel Deployment Automation CI\CD using AWS RDS EC2 S3 CodeDeploy BitBucket

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in this tutorial we're going to learn how to set up continuous integration and delivery for any laravel project using aws and bitbucket i will be showing you how to automate your build testing and deploy process here's how it's going to work once any code is merged with your git master branch we will use bitbucket pipelines so we could build our project and run all our tests or lint scripts once our build and tests pass our production server will be automatically updated to contain the latest revision of master for our laravel project to accomplish this we will be using aws ec2 instance to host our production server now it's optional if you want to add staging which is basically a duplicate for your production server now we will also use aws rds instance to run mysql8 server for our laravel project we will also use aws s3 bucket to store our revisions we will also use aws code deploy to run deployments and aws server manager to store and populate our emv file finally we will use bitbucket to store our code repository and run our pipeline and we will use bash to create our scripts so right now i have hosted an empty laravel project on an aws ec2 instance this is the ip address right here so let's go ahead and add a new change to our code i'm going to basically add a new title tag to this main page so on the code editor let's go ahead and add a new change to our code so i'm going to add a new h1 tag i'm going to say hello world and let's save this change and open our terminal now we want to basically merge our work or commit our changes to our master branch because all the deployment will start once any code is merged to the master branch so right now i am on my master branch i'm going to add my work and i'm going to push those changes now i have stored my code inside this bucket repository and to show you guys i'm going to go to pipelines and you can see right now this is our test push right here and it's currently in progress now after waiting for a few minutes the build took around five minutes let's go ahead and see what happened here and you can see our first step we were building and testing and i have few scripts here that was running we built our server we built our project we ran our tests then we deployed it to production now if you want to add staging it's very easy to do staging is basically your a production server so it's literally a copy of this part right here now let's go back to our website and let's refresh so now you can see here our chains got deployed without us having to do anything other than merge our code with master before we get started i would like to say please make sure that you're fully aware behind all the costs for everything that we're going to be using in this tutorial for bitbucket i think you get around 50 minutes free every month for your pipeline build time so let me show you guys quickly bitbucket let's go pricing and in here you should be able to see that on the free account you basically get 50 minutes free every month for your build minutes now let me show you what happened here so this is some sample work that when i was working on this you could see it took me two minutes here so you could get an idea how much build time uh an empty laravel app would be so i wouldn't say 50 minutes is a lot but if you're uh if you don't want to spend for let's say uh so this is it's just three dollars a year but it's actually three times five so it's 15. so the standard plan would be 15 and you get around 2500 minutes and this is what i'm using and this is more than enough for small projects now for the aws make sure that you use their pricing calculator because pricing on aws is different per region and per service so if you go ahead and type aws pricing calculator and you should be able to create an estimate here other than this you should also be able to go to your cost explorer in here you should be able to see your daily costs so this is my uh this is the account i use for all my tutorials and you can see here how much i'm paying for uh per month to create a new bitbucket repository the first thing you're gonna do is go to your bitbucket homepage from here you're gonna click on the plus symbol on the left and create new repository let's call this project laravel devops and i'm gonna place this inside my tutorials or you could create a new folder and let's go ahead and create repository i'm going to copy the url and place it inside my notepad so now we're going to create a new laravel project and we're going to push that to our new repository now i'm going to be using composer to create a new laravel project and i'm gonna call it laravel dash devops once that's done let's go ahead and push our changes so i'm gonna go inside my new project directory now let's initialize a new get directory let's add all our work and let's commit our changes so i'm going to say first push and let's add our remote origin so get remote add origin and the origin would be our url for our bitbucket account let's make sure that worked and now let's go ahead and push our changes so the u flag will allow me to track the master branch because right now see i am on local master branch but it's not tracking my remote master branch so the u flag will track the master the force flag because we want to replace everything this is our first push and we're doing this on origin master now let's check what happened to our bitbucket repository so let's refresh bitbucket pipelines is an integrated continuous integration and development service built into bitbucket it allows you to automatically build test and even deploy your code based on a configuration file so to get started on the left here you want to click on pipelines from here you want to create your first pipeline and let's go ahead and select the starter template and so you can see here bitbucketpipelines.tml this file will be placed inside your root directory folder for your project so we will cover all this in a bit let's go ahead and commit this work now let's go ahead and check what happened to our source and you can see that the file is inside here and once you have this file now the pipeline will activate so see right now the steps build test length security scan so you can see these are the steps written here this is the first one build and test they're just echoing text here step two blend step three security scan now before we get started i would like to note that uh since we have the bitbucket pipeline xaml file placed in our main directory every work or commit that you do on your master branch will basically trigger the pipeline since we created this file using the bitbucket interface let's go ahead and pull our work and let's open our code editor from here let's go ahead and open our pipeline xaml file i'm gonna erase these comments quickly and i'm gonna remove the security scan step again you can add as many steps as you like and i'm going to comment this part out for now because i don't want to keep triggering these steps now remember that bitbucket basically uses docker images to create for you a server where you could build test and do your deployment or whatever other steps that you want for this tutorial we will use the php fpm image so let's go ahead and erase this and you want to write php add a colon and you want to specify the php version that you want to use then dash fpm let's go ahead and save this and now let's go ahead and create a new folder in our main directory and i'm gonna call it devops you can call it whatever you like and now we wanna create a new file inside our devops folder and let's call it build dash server dot sh now remember that we're using bash so let's go ahead and add our bin bash command oops so this file will basically run all our commands to build our server and just to be clear this is not building our production server this is only building our i would say testing server for the bucket pipeline this is what we gonna build here now just like building any other server the first step should be update so we're going to write apt dash qy update and the y flag will basically prevent us from getting so you guys know when you sometimes try to install let's say a package or something on the command line you get like a prompt that says do you want to continue enter y or n so the the y flag would prevent this prompt from showing up and those kind of prompts will break your scripts the q flag is short for quiet which makes the output display less information suitable for logging and leaving out the progress indicators we're using it so we don't clutter our log files our second step would be to install any package that we need so let's say for example we want to install curl get zip unzip so whatever you want to install go ahead and add it here now if you want to install php extensions basically you're going to use the docker php extension install command and you want to specify all the extensions you want to install for example mysql c type bc math zip and the list goes on now you don't have to specify your php version here because that's going to be already specified for you based on the php image that you have specified inside your bitbucket pipelines yaml so this docker php extension install will install all the extensions here based on php 8.0 or whatever version you have specified here now we're going to install composer using the curl command to do that you run curl and you want to add the silent flag so you would basically display less output and you want to also add the show error flag and for this we're going to use the composer original installer so it's going to be getcomposer.org installer and this is a php file so let's run it using php and we want to specify two parameters the install directory which will be inside user local bin this will make a composer accessible globally and also the file name and let's call that composer now there are other useful flags that you can use if you double click on your link you should be able to see it download here and let's go ahead and edit this file so you can see this is a php file and when you run it using php you have few flags that you can set and you can see them all in here now our last step will be to install npm and you can easily do that with apt dash qy install npm now let's go ahead and commit and push our changes so i'm gonna do get add commit and let's say server build script and let's go ahead and push our changes oops let's try that again now let's go to our bit bucket and check what happened to our pipeline so currently it's in progress now our build is complete and we have a failed status so let's see what happened here so let's click on this entry and you can see our first step build and test we failed on the on running the devops build server script and the reason is permission denied we can't access this file so this is actually a very easy fix so when you're running bash scripts always add when you're using bitbucket pipelines always add bash and let's just add this just for convention purposes now let's go ahead and push our changes and let's see what happened to our pipeline now it looks like our build passed so let's go ahead and see what happened so for our build script let's expand that and you can see here all your logs this is all the scripts that we ran now we have another step here lint that's basically just doing an echo now both these steps are running in parallel so if we go back to our code you'll see here we're using the parallel flag so what this means is that they run the same time but since they run the same time the lint step basically doesn't have access to let's say our built a server and once we start adding other scripts like our application so this would be good to do like uh other things that don't really relate to your server or code being built and lint just makes sense you don't need a server for that you can just run your link scripts to check if your code is valid now before we can build our laravel project we need an sql server or mysql server or actually any other service so if you're using let's say for example postgres mysql redis or any other service then we need to create it in our configuration file so basically we write definitions and services and for this service we're using mysql again this could be anything else and for the mysql image we're gonna use uh mysql 8.0 you can change the version as you like and let's add our environment variables so mysql database would be homestead and let's use a random root password and let's go ahead and add the mysql user which is also homestead and mysql password which will use secret again you can modify these values as you like but you shouldn't really care too much about this because this is only running for the duration of our pipeline we want to run basically our tests our software we want to make sure our migrations are not breaking our project so to be able to use now this service inside our steps we will need to add it here so let's go ahead under the step add services and we're going to add our mysql service now remember the same thing if you're using redis or any other service this is the same way that's how you add it now let's go ahead and create ourselves a new file so we want to add devops so let's call it build project so let me create this file quickly inside devops build project now on my last one of my last videos i went into depth on how to build your laravel project so if you guys are brand new to building laravel projects i recommend you watch that video first it's called how to deploy any laravel application on aws so to install our application we need to install our dependencies so composer install and i'm going to use the no interaction flag to prevent any prompt from breaking our script and i'm gonna also use npm install now we're going to create a new emv file remember we can't store the cmv file in our repository so we have to create a dummy emv file let's call it pipelines and we don't care about all this we need it to we need to set our app pmv and you can set it to local or production and your app key just go ahead and enter some gibberish and for your db connection we're using mysql as we defined it inside our bitbucket yaml file so let's go ahead and add our host and it's we're using localhost so 127.0.0.1 and our database name is homestead and our username was also homestead and finally our password was secret you want to make sure that these are exactly the same as you would have them inside your actual emv file now let's go back to our build project script and now we want to basically create a link between our emv file and pipeline file so we do that by using ln dash f dash s and our emv file name and our actual emv file so now we created a link between them now once we have that done let's go ahead and run our migrations so remember this is also a good test to make sure that exchange isn't really breaking our database and let's go ahead and generate our app key and so if you guys are using uh let's say uh passport then you want to go ahead and add the php artisan passport install or any other command that you would need you don't have to know them all right now once you start running your application on the pipeline it will fail and you should get some useful errors on what you're missing so let's go ahead and save all this and actually one more thing let's run npm run production so let's save this let's commit it and push it now let's go ahead and check what happened to our pipeline let's go back to pipelines and looks like our pipeline passed let's quickly check what happened here so for our build script we ran these two scripts the build project let's see so composer installed we ran our migrations and we ran mpm on production so this is in one way a small test let's say someone submits a migration that breaks or same thing someone does something and breaks the npm script i wouldn't really use this as a major way to test your application but it's just one way to test there is a lot of different linters you can use for your front end and back-end code if you plan on using any then you wanna just run your uh lint right here for this demo i'm going to comment this part out and since we commented this out we only have one step inside the parallel so this will not work we will need to erase this and when we erase this we have to realign our step to here so let's go ahead and do that and if you're using a visual code you can do control shift and alt and press the down arrow and you can do them all together here we go i think that's uh not one more so that's good now let's go ahead and create our test script so i'm going to add a new uh script let's uh call it run tests and let's go ahead and create it now the first thing we want to do is we want to start our server so we want to do php artisan server and sleep five so we wanna basically start our php server for our level and sleep five is just to give it some time so once we execute our next commands it would be ready now to run our php unit test you would simply do use the vendor folder inside bin run your php units script if you have any other testing that you're doing you want to add all your scripts in here now once you have all your build scripts down you want to commit and save this change to your master branch and actually i'm going to remove these two from here so deployment to staging is literally exactly the copy paste of deployment so it's going to be exactly the same i'm going to remove all this and clean up the file here let's save and go ahead and push your change now we pushed our change right here and our pipeline succeeded so let's see what happened in our script and you can see that we ran only two tests and they were successful if the tests were to fail then this pipeline will fail and you will see the output in here in aws im stands for identity and access management it lets you access and manage your aws services and resources securely in order for us to automate our deployment process we will need to give our ec2 instance access to code deploy so that we can pull our latest code revision and deploy it on our server whenever new code is merged on our master branch and our pipelines builds successfully we will also need to give our ec2 instance access to s3 bucket we will use s3 bucket to store our code revisions so im basically lets you create and manage your aws users and groups and it also allows you to set permissions to allow or deny user access to your resources so i am user groups are basically a collection of users user groups let you specify permissions for multiple users now let's go ahead and go to i am on the top search for i am and in here on the left you have user groups let's go ahead and create ourselves a new user group let's call this laravel devops you can name this whatever name you like and you need to give s3 full access so s3 full let's search for that and let's add that and we also need to give aws code deploy full access or let's remove the s3 filter actually let's just search for code deploy and you can see it should be right here and let's go ahead and add these two now once our pipeline builds in bitbucket and let's say it succeeds we need to give bucket access to our code deploy and to our s3 bucket we will cover s3 in a bit so let's go ahead and go to users on the left and add a new user for bitbucket let's call it laravel devops user and let's check the access key here next and you want to make sure that you have your group that we just created selected here and you wanna click next and uh let's go ahead and create this user now you wanna download this csv which will basically contain your access key and secret key now i'm going to add those to my notepad so my user was laravel devops user the access key let's copy this and the password let's copy this as well and yeah that's right now we're going to use service roles so we could create a new role for our ec2 instance and allow our ec2 instance access for code deploy and to s3 bucket now let's go ahead and click on roles on the left and let's create a new role you want to select aws services and ec2 and go ahead and click on next and you want to add two policies one for code deploy so let's search code deploy so you would add the code deploy full access and you want to also add one for the s3 so let's add that and click next and let's call this user ec or laravel devops role so an s3 bucket is basically a cloud storage we're gonna use it to store uh revisions of our code every time we deploy it so this way we have backups and we have the actual code files that we want to deploy so let's go ahead and search s3 on the top and let's create a new bucket and let's call this bucket laravel devops and i'm gonna add this name to my notepad and it might be useful right now to also make a a note on your region so in here we're creating this in the us east one region and we want to make sure we have all public access blocked and let's [Music] let's create this bucket once you have that done let's go ahead and copy our region so you want to go to access points so you want to go to your properties and you want to copy your region here so let's copy this and add it to our notepad now let's go ahead and create an ec2 instance for our production server you can also create another one if you want for your staging server so the steps are exactly the same from here we're going to launch a new instance and we're going to use a ubuntu server and i'm going to keep it micro let's go to configure instance now it's really important in here to give i am role so remember that account that we created the sorry the level uh devops role so you want to have that assigned now we want to add also so you can here select your desired storage so we want to also add a tag for code deploy to use it could be anything we just need a key and let's call this one code deploy so this key will be used to identify this instance and uh let's go ahead and review actually let's create some security groups so we need the ssh i'm going to select my ip we need the http access and https access and that should be uh oops that should be all let's review on launch and i'm gonna create a new gear pick uh keep here i'm gonna call it demo or laravel devops and let's go ahead and launch our instance now let's go ahead and create our database so we're gonna search for rds and we want to click on create database and we wanna select uh mysql or you could select your desired database i'm keeping mine at eight and i'm gonna call this laravel devops you can call this whatever you like and i'm gonna select the smallest size available so that's burstable and micro i'm gonna also reduce the storage so i'm gonna select solid state 20 gig that's the minimum i'm going to remove the storage auto scaling and i'm going to remove the multi ac deployment if this is an actual app that you're deploying i recommend you keep this turned on for my demo i don't need this it will cost me around 1250 to give me a standby instance that i'm gonna be probably destroying in a few minutes so let's uh [Music] let's create this database for the password let's auto generate that so let's go ahead and create now i'm gonna click on view credential details and i'm gonna grab my master password and add it to my notepad so my rds instance the password is here the user is admin and we will need to find out the end point so we could connect to it so we want to go to connectivity and we want to copy this endpoint here and let's add that to our notepad before we connect to our ec2 instance let's go ahead and grab our public ip address so let's go to ec2 and two instances and let's click on our instance let's copy the public ip address here and actually i'm going to give this also a label name so i'm going to call it uh laravel devops and i'm going to add my uh ip address in my notepad now let's go back to the file that we downloaded our pem file for our ec2 instance let's open git bash here and let's connect to it so we're going to use ssh dash i to specify the pem file and the username was ubuntu at our ip address now we want to basically install a code deploy agent what that does is basically allows our ec2 instance to communicate with our code deploy application so let's go ahead and install code deploy so first we want to run sudo apt update so we could update our server then you want to run sudo apt install and we want to install ruby dash full by the way guys all these commands are going to be posted down on the description now you want to install uh wget so sudo apt install wget and you wanna make sure you are inside your home ubuntu folder now let's uh use wget to uh connect to our instance so here's how we're going to install it wget https and we want to write our bucket name i will tell you guys how to grab that dot s3 dot our region identifier so our region was us east one and dot amazon aws dot com slash latest slash install now to find the bucket name oops let's go back to our browser and we want to go to uh docs.aws.amazon.com slash code deploy from here you want to search for resource kit and select the first option scroll down here now based on your region this is your bucket name in my case i am used one so i will copy this let's go back to our notepad and let's add it inside here now let's go ahead and copy this and let's go back to our terminal and let's add it here so let's do ls so we ins we basically downloaded this file install now to run this file we need to give it permission so we gotta do chmod plus x slash install so now we can run the file so you wanna write sudo and let's select the file then you wanna do auto and you wanna make sure that we're logging these outputs so let's run this now to check if code deploy was installed let's check the status so we do sudo service code deploy agent status and now we can see that it's active and it's running if yours is not running you do sudo service code deploy agent start now remember that service roles are used to grant permissions to an aws service so it can access aws resource we need to get our code deploy application to connect with our s3 so we need to create a new service role so let's go ahead and search for i am and on the left we want to go to roles and let's create a new role and down here we want to select code deploy so it should be down here if we search for code deploy so right here let's select that option and you want to select the first one right here your use case code deploy and let's go ahead and click on next permission and let's go ahead and click next and next let's name this one code laravel dev ops code deploy and let's go ahead and create this role to create our code deploy application let's search for code deploy on the top now from here you want to go to applications and you want to create a new application let's call our application laravel devops and for the platform you want to select ec2 on premise create application a deployment group is the aws code deploy entity for grouping ec2 instances or aws lambda functions in a code deploy deployment for ec2 deployments it is a set of instances associated with an application that you target for deployment now we will need to create a deployment group so let's go ahead and click on create a deployment group is the aws code deploy entity for grouping ec2 instances or aws lambda functions in a code deploy deployment for ec2 deployments it is a set of instances associated with an application that you target for deployment now we will need to create a deployment group so let's go ahead and click on create for the deployment group name let us call it laravel devops and let's name it production for our to represent our production server if you have staging you want to create another deployment group but with a staging name or something else to allow you to distinguish between them for the service role you want to select the one that we just created and for the deployment type you want to make sure you have the in place selected for the environment configuration we want to select amazon ec2 instance now to associate our code deploy application with our ec2 instance if you guys remember we created a tag if we click here on the key that tag was called code deploy actually now that i think about it it would have been better if we called it production because if you want to create for example a staging so you would basically create another ec2 instance and you would give it a different tag name then you would be able to create a different group here and select that tag now we want to keep all these options so for the load balancing i'm going to actually disable this option i recommend you actually have a load balancer but this is a big topic in its own i will cover this in a future video so here's what load balancers do let's say right now you have you have to update your server obviously there's going to be a few seconds where your server will be inactive now a code a load balancer allows you to have let's say for example 10 instances running and basically your traffic would be redirected to the instances with the best health so basically it would keep your up time to 100 but to get the most out of load balancers you need to have like let's say at least two instances running for your project so let's go ahead and create deployment in order for us to deploy our latest code revisions to s3 we will need to tell code deploy that our revisions is ready to communicate with aws using bitbucket we will use the aws cli which is a command line interface that provides us with tools to help us interact with aws using the command line now to install the aws cli we're gonna go to google and search for aws cli install linux and you should be able to find the first link in here and in here we want to scroll down and we want to basically select these commands now let's go back to our code editor and you want to open your bucket pipelines file and let's go ahead and uncomment the staging sorry the deployment part for our production server now since we don't have staging right now i'm gonna comment this out trigger manual only works when you have a step behind this one and the step would be basically another deployment now for the name we're gonna keep it as is for the script let's go ahead and create a new one so we want to run bash devops and let's call our new script build for production so what we're gonna do right now is we wanna build our project then send it to s3 store it there now let's go ahead and create this file so i'm gonna copy this build for production now just to know if you decide to build your project like including your composer dependencies and node dependencies remember that we're storing this in the cloud so for every deployment every revision you're storing a copy of your revision and if you're including your vendors folder and your node modules you would basically uh use a lot of space so it would be a trade-off do you want to basically build your project and store it in s3 then basically once it's deployed it's already built or do you want to actually build it on your server so that's completely up to you now for a basic laravel project you don't really need to install node modules on your production server unless if you had to so for our example let's go ahead and install npm and let's go ahead and run mpm install to install our dependencies and do npm run production now we need to install the aws cli so the aws cli basically gives us a command line tools that we can use to run aws services so let's go to google and actually let me erase this let's go and search for aws cli install linux and you should have it on your first result and let's go all the way down and let's copy this let's go back to our code and let's paste it in here so notice that this one is actually installing a zip file so once we run the installer we need to actually remove the file so we do rm rf slash aws to remove this folder after the install we also want to do our m f aws cli v2.zip and i'm going to remove the sudo because we're already running these commands as root now let's go ahead and save our work and just one more thing i actually forgot to add the build server script so when you run these steps here they don't carry over here so we're gonna have to copy the build server script and run that first then we build our production now before we can actually create our deploy scripts we need to set up some environment variables for our bitbucket repository so we could access them inside our deploy script so let's open notepad now we want to know our aws secret access key and that would be the long string basically and we want to know our aws access key id and that would be the shorter string and we want to know our application name so let's go ahead and grab that from [Music] our code deploy so let's go to code deploy applications and you should find your application name right here copy this and let's add it we want to also we want to know our aws default region and ours was us east one if you forgot or if you don't know how to grab your region basically if you go back to your amazon homepage you should find it right here or even right here and we also want to know our deployment group name so we have only a production server if you have staging you would just create another group name here duplicate this for staging and uh let's click on our application and our group name was laravel devops production and finally we want to know our s3 bucket the bucket name and you can we had that right here now let's create these in bitbucket so here's what i'm going to do i'm going to try to so let's go to our repository let's go to repository settings scroll down all the way it should be right here repository variables so in here we want to copy basically these values so the access key we want to make sure it is secured let's add our app key id and let's add our application name and we want to remove the secured flag oops let's copy this and the aws default region the deployment group name and finally the s3 bucket name now we have everything that we need to create our deploy script so let's go ahead and add a new file i'm gonna call it deploy production dot sh let's save this and let's create a new file now the first thing we want to know is our hash for our git repository so we want to know our latest commit hash we easily know that by using hash to create a variable get rev parse short head that's how we grab the latest hash we want to create a new bundle and we want to store it inside our s3 so let's call this bundle hash dot tar.gz that's we're going to be creating basically a zip file and we want to also know our s3 endpoint so let's create a variable for that and if you guys remember we had the s3 bucket name right here so let's go ahead and copy this and let's go back to our code editor and let's add it in here so the end point would be s3 colon two slashes your bucket name slash bundles this is basically where we're storing our app this is the url basically and actually for the name i'm gonna add the bundle dash the hash so that would be a better name and let me surround this with quotation marks now the first thing we want to do is we want to remove all the old bundles so let's do it this way it's already easy now we want to create basically a tar file so we do it we use star and we want to exclude all our get files we don't want to commit any or store any git files we want to also exclude our storage locks files and we want to also exclude our vendor files or you don't have to exclude the vendor files you can uh send it to your server already built but the problem is you're gonna use a lot of storage on your s3 for every single commit so if storage is not an issue for you i would actually recommend that you create it here so let's add this and what's left is a artifact so let's do a exclude the amv file now let's go ahead and create our bundle so we're gonna use the z now let's go ahead and create our bundle so to do that it's simple let's use the zcf flag add bundle dash t to specify the configuration for our bundle and let's call we're gonna have to create this file bundle.com and we want to basically specify all the columns that we wanna or actually folders that or files that we wanna store so let's quickly create that so inside our level project we will create a new file called uh bundle.conf and we need to add it to our root folder so let's move it now what you want to do here is basically specify every single file that or folder that you want to include in your bundle now basically what i did here is i just included every single file here and yeah so you want to tweak this around it's up to you note i am including my node modules just to demonstrate that you don't really have to build things on your production server but uh yeah so i'll keep this here and now let's save our work and let's go back to our build script so right now we want to actually send this star file to our aws s3 bucket so here's how we do it we're gonna use the cli and we're gonna write aws and s3 and we're gonna copy our bundle and it's for the s3 endpoint and basically that's pretty much it now let's go ahead and do an echo so we could see this in our logs and let's say your code deploy s three endpoints will be and let's add our s3 endpoint so this is where this is the link to where our revision will be stored or our bundle now let's go ahead and send that so we want to create our deployment we do aws deploy create deployment and we want to specify our application name and we had that coming from our emv file now let's go ahead and create our bundle so we're gonna use the z now let's go ahead and create our bundle so to do that it's simple let's use the zcf flag add bundle dash t to specify the configuration for our bundle and let's call we're gonna have to create this file bundle.com and we want to basically uh specify all the columns that we wanna or actually folders that or files that we wanna store so let's quickly create that so inside our laravel project we will create a new file called bundle.com and we need to add it to our root folder so let's move it now what you want to do here is basically specify every single file that or folder that you want to include in your bundle now basically what i did here is i just included every single file here and yeah so you want to tweak this around it's up to you note i am including my node modules just to demonstrate that you don't really have to build things on your production server but yeah so i'll keep this here and now let's save our work and let's go back to our build script so right now we want to actually send this star file to our aws s3 bucket so here's how we do it we're going to use the cli and we're going to write aws and s3 and we gonna copy our bundle and it's for the s3 endpoint and basically that's pretty much it now let's go ahead and do an echo so we could see this in our logs and let's say your code deploy s three endpoints will be and let's add our s3 endpoint so this is where this is the link to where our revision will be stored or our bundle now let's go ahead and send that so we want to create our deployment we do aws deploy create deployment and we want to specify our application name and we had that coming from our emv file now we also want to specify our deployment config name and actually i forgot to add that to our variables so let's go ahead and quickly do that so let's go back to bitbucket so we want to go to our bitbucket repository we want to go to repository settings and let's go to variables and we want to add a new variable for the value we're going to write code deploy actually let's remove the secured so code deploy default.one at a time and let's go ahead and add that now let's go back to our code editor and from here we want to actually add more flags so [Music] we want to add the deployment group name and let's go back to our notepad and copy this group and we wanna also uh overwrite if the file exists and finally let's set the s3 location and the bucket was s3 bucket or did we call it bucket let's copy this and the bundle type is t z that's our file extension and the key is bundles slash bundle let's go ahead and save all this and let's commit our work now i went ahead and i committed my work but my pipeline failed so if you see here i have an error the following requirements arguments are required application name now i noticed i made a small mistake on my name here so this is supposed to be dash not underscore also another mistake i made here was in the s3 endpoint i was supposed to call the s3 variable not the actual value and my final mistake was i didn't add a slash here so let's go ahead and save and push our changes now after i committed my work and pushed my changes to master branch our pipeline ran and it succeeded so let's check what happened on aws so let's go to code deploy let's go to deployment and see here this is my latest deployment it actually failed now let's click on this and see why let's click on view events now keep in mind these are all the events you have so they're basically your hooks once we we actually deploy the event on our server we have those events that we can use to write our code to do things when this event happens for example install so we filled in the download bundle event and it says here the specified key does not exist now this is an easy error to fix here's how you would fix this error you have your s3 revision right here this is the location if you click on it and click on it here again you should be able to see this file stored if you get this error then it's a 90 percent of the time has to do with your name and most likely because of this folder so if you go back to your code editor and here is the issue so for this s3 endpoint basically i didn't add this slash here so let's go ahead and save our work so the build and the deployment pipelines have passed now let's check what happened on our aws side so let's go to code deploy and let's also open s3 so let's check our bucket and let's go to bundles and see here this is our bundle that we sent from our bitbucket pipeline now let's go ahead and see what happened in the code deploy and our pipeline failed and let's see why so let's go to view events and hours failed on the before install which is perfect exactly what we wanted to do now keep these in mind these events before install install after install application start and validate service now let's check if our files were downloaded into our server so you wanna do ls and opt opt code deploy agent and from here let's go inside our deployment root folder and in here basically every time you push your code your files will be placed in a string that looks like this and it would be really hard to tell which where your files are but i'm just showing you this to demonstrate a point so again your files could be in one of these folders this is completely random i'm gonna select this one and lucky for me this one is the right one because there is this deployment archive folder so let's go inside here now you can see this is your laravel project files the app spec file for amazon ec2 deployment specifies your task definitions container names and container ports it's also where you do your mapping so let's go ahead and create ourselves a new file and let's call it app spec dot yaml oops let me change the x to c and let's erase all this so the first line you want to say your version so right now we are 0.0 os is linux now the files basically we want to map our source which is our main directory to our destination so that would be where we want to place our website on the server so we're going to do that inside var w triple w and let's call our project folder laravel devops now we have hooks that we can use to execute scripts so let's go back to our amazon code deploy and in here if you guys see this in the bottom before install install after install application start and validate service these are all different types of hooks you can use to uh attach your code scripts so i'll give you an example we're gonna add we're gonna use the after install to attach our scripts to build our server run our migrations and do some other housekeeping work so to use any hook basically you want to write the name of it here and you want to specify now the scripts you want to run so the location let's say we want to create a new script inside our devops folder let's create a new folder called hooks and let's create a new file called after install dot sh and we wanna run this file run as ubuntu because we don't wanna build or do anything on our project as root because that will not work so let's save this and let's go ahead and create this file so inside devops we create a new folder let's call it hooks and i'm going to add my file now in here let's go ahead and declare two new variables so export so we wanna declare web directory this is where our project is placed and the reason why we're using export is so we don't have to redeclare this variable on any other script that includes that's coming from this file so let's also do another one and we want to know our user the one that we're gonna run our commands for so it's gonna be ubuntu so our first step we wanna go inside our web directory folder now we want to change the user permissions so if you guys watched on my last video on how to deploy any level application then you would this would be a little bit obvious for you so we want to set we want to change the user to ubuntu and remember we're inside this folder so we're changing the access user from root to ubuntu and we want to also do the same thing for the storage folder but we're going to use the triple w data user now we're gonna also [Music] change our permission so we're gonna give u plus x and we're gonna also do the same thing g plus w on the storage folder now we wanna install our composer dependencies so to run it to run any script as a ubuntu user a different user than the one we're currently as we do sudo dash u for user and our user name so now we're running this script as ubuntu and we want to basically do composer install no dev no progress and prefer the ist now we wanna also run some other scripts so let's do artisan key generate and let's also do php artisan migrate no interaction and i would be very careful about placing a force flag here these are all some basic commands that we can run in your scenario let's say for example you wanna install npm then you wanna simply run the command here you wanna also maybe let's say install passport if you're using that but yeah so try running this few times you should get some errors and uh keep modifying the scripts until it works for your scenario now let's go ahead and commit and push our changes to our master branch we can see here that our pipeline has passed so let's go ahead and check code deploy let's refresh the page and we can see that our build field so let's click on it and let's see why so let's go to view events and after install let's see the script now the errors we're getting here is that composer not found php not found we haven't configured our server yet so let's go ahead and do that now in this step we're going to configure our production server we already went through this in great detail in one of our last videos week it was called uh how to deploy any laravel app using aws to save time i'm gonna just go through this part quick please watch the other video if you struggled on this part so we're gonna add a new script folder and i'm gonna add a file and i'm going to say build production server dot sh now once that's done let's quickly configure our apache so let's first check what happened to our website so we said we mapped it to var www and you can see that it's inside here remember this is coming from the deploy script that we built from our pipelines so let's go now to uh modify apache so atc apache and in here let's uh go to sites available and i am using my ip address so i'm going to do ip a or actually was it ipa there's no dash and my ip address is right here my public ip so i'm going to copy this and i'm going to modify the default website configuration file so sudo nano zeros okay and let's go ahead and all what i want to do is just change the document root quickly so let's just do that so it was inside laravel devops and it was inside our public folder that's the document root let's go ahead and save all this and let's see if that let's actually restart apache first the fashion restart now let's check what happened to our website so let's go and open a new web page and let's see our ip address now we're getting this site cannot be reached error and this is actually a good sign since it's a timeout we know we know that there is some connection but remember we didn't configure our emv file we didn't install our dependencies so let's go ahead and do that now now if we take a look inside our var w w and our laravel project folder remember we did this mapping in the app spec file so we can see that our emv file is not here so we will need to somehow load our emv file in here without having to save it into our repository this is where we would use the aws secrets manager now before we get there let's quickly prepare our emv file for production so let's open our code editor and let's find our emv file so the first thing we want to do we want to change the emv to production the debug to false and my app url is actually my ip address for the instance so i'm gonna copy this i'm gonna go back to my code terminal and paste it here now for the database i'm going to use our rds instance so let's pull that information out quickly and this was the endpoint which is the host the username is admin and let's add our password and for our database let's name it laravel devops we still have to create the database let's uh do that on our server so let's go back to our server and let's use my sql so it's gonna go like mysql dash h to specify the endpoint then dash u to specify the user admin and i'm gonna just copy the password then lowercase p to specify the password now if you don't have my sql installed let's quickly install that so we do sudo apt install mysql client core 8.0 now let's try running that again so let's paste the password and i think we didn't give our ec2 access to our rds instance so let me just terminate this and let me do ipa and let's grab our ip address so we want to create a new security group for our rds instance to allow access for our database so let's go to ec2 from here we're going to go to security groups and let's create a new security group and let's call it production database access and for the inbound rule we want to select mysql and we want to add the custom ip so let's paste our ip address let's create the security group and let's go ahead and assign it to our rds instance so we want to click on databases and we want to modify this database now you're going to scroll down all the way till you get to the connectivity section remove the default security group and let's add the one we just created let's save this and let's apply this immediately now let's go back to our server then try again so i need to copy the password again now we're connected let's go ahead and create our database and let's call it laravel devops and the end of the long terminator let's exit now we're going to use the aws systems manager to store our emv file so on the top search for systems manager and let's go ahead and create a new secret so we want to scroll down and go to parameter store and create parameter and let's call this laravel devops emv now for the text we want to make sure this is a secure string and let's go ahead and add our emv file so that was inside our code editor and actually let's keep this as a string now let's go ahead and click create parameter and if you click on it you should be able to see your emv file here now we need to give our ec2 instance access to the system manager to read our emv file so let's go to ec2 and let's go ahead and find our instance so instances and let's click on our instance and in here you want to go to your i am role and we want to basically attach a new [Music] policy so in here we wanna do uh add permission now for the service make sure you select the systems manager the action we only need the get parameters so let me search that out so here we need to get parameter and get parameters that's it so the resources let's uh actually let's just specify all resources and let's review our policy and let's call it laravel devops emv let's create this policy now we should be able to access our service manager inside our ec2 instance now let's go back to our code editor and we want to basically write a script where we would extend or sorry where we would pull our data from aws so inside devops inside scripts let's create a new script and let's call it generate emv and let's erase all this now we need two parameters here actually so we're gonna call this file inside our after install sh now we already have these two variables exported so we can use them there so here is how we're gonna call this file so let's actually do it before the migration so devops scripts generate emv.sh now let's go ahead and go to this file and what we want to do is declare two new parameters or variables so the first one would be the name of my emv file or parameter now the region let's go back to our aws and on the top here let's copy this now we're going to use the aws cli to extract the emv file so we're going to use aws ssm get parameter and we have to specify the width decryption because our file is decrypted name and let's add the parameter this is the file name now let's add our region finally let's uh i forgot to add these here now let's query parameter dot value this will basically return our emv file value instead of returning an object that aws would return so we're just querying the return object to give us the value so it's an array parameter then there's value and our emv file now let's uh output text inside our web directory and we want to create our emv file so remember this variable is coming from the after install it's exported here actually this is wrong so i meant to say prefer the ist so let's fix that up now let's go ahead and commit and push our work to our master branch now again the deployment we were running failed so let's go ahead and quickly check why actually let's go click on events and we're failing on the after install and in here we're getting connection reviews to our sql so i can tell from this error that our emv file is not correctly loaded so let's go ahead and check that out so let's go back to our terminal and let's do ls-la and our emv file is right here let's go ahead and modify it and see we don't have our emv file loaded so this could be multiple things it could be a permission but let's go back to our code editor so the first thing i want to do is i want to clear laravel's configuration cache so first let's set the correct user permission for our emv file then let's run sudo u using our web user to clear our config cache now the second thing i want to do is i want to test this quickly out in in our instance so i'm going to just copy the values here for now and our web directory was uh rww laravel devops let's go ahead and copy all this and let's uh execute it in our terminal now i might have misspelled my name here so let me go ahead and go to my systems manager and we want to go to parameter store and let's make sure we have the right name c here we have underscore so let's go ahead and quickly fix this so i'm going to copy my parameter name and let's add it here and for now we're just adding it here to run it on the terminal and i think i spelled parameter wrong so let me go ahead and fix this and let's just not forget that this is supposed to be lowercase so let's now go ahead and copy this and let's give it a try inside our terminal so let's clear all this and let's run this and let's view our emv file and see now we loaded our emv file let's go back to our script and let's change those values quickly so we want to replace this with the parameter and this with our region and that and this part it's coming from our hooks after install the web director so let's add that here now let's go ahead and push our work now after running our build we managed to get it to work from the bitbucket side so let's check our aws let's go to code deploy and you can see here our push passed so we managed to deploy this let's check our s3 instance or cloud storage so we have it right here laravel devops in bundles and you can see this is all our bundles so far remember since i am not uh bringing in my composer file it's only 13.2 megabytes but once you start adding your vendor files this is going to go into gigabytes easily now finally let's go check what happened to our ec2 instance and let's go ahead and open this instance actually that's not going to work because of https so we got to do it with http so see we're getting the default level page so let's go ahead and make a change so i'm gonna close all uh so i'm going to close all this and let me go to my welcome.blade and let's go ahead and add a change somewhere here so i'm going to add [Music] h1 tag and let's say final let's go ahead and push our changes and now you can see that the build passed and the code deploy it's uh let's see it passed so now we should see our change and here you go it's working thank you for watching if you guys found this video useful then feel free to give it a thumbs up also we're releasing a new video every week on sunday so don't forget to subscribe to stay up to date with our latest training videos also if you have any questions feel free to post it down on the comments below you
Info
Channel: AHT Cloud
Views: 1,461
Rating: undefined out of 5
Keywords:
Id: YQsHMbbcIBo
Channel Id: undefined
Length: 99min 55sec (5995 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.