Deploying a Laravel App via Elastic Beanstalk | Amazon Web Services BASICS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to this video my name is Maximilian and in a previous video I already got you started with AWS in this video I want to show you how you can deploy a level application including a database cue AWS using AWS elastic beanstalk let's start this is the application I want to deploy a very simple one but it does use a database behind the scenes in this application we can sign up and create blog posts like this is my first post the very first one when we did this we see an overview over all blog posts I already created some and that is the application not super amazing but using a database which makes it interesting when we talk about deployment now you can find this application on get up a link can be found in the video description and there you can simply clone or download this repository now I'll do this too so I will download it and I'll work with this one now as I said I want to use elastic beanstalk to deploy this application why if we switch to AWS in the end what we'll need is a server and we can spin up a virtual machine instance using their ec2 service this allows us to spin up virtual servers so basically like physical ones which we own but just a part on a physical server with the configuration we want we could do this and then we could install a web server on ec2 and deploy our code onto the instance we will also have to take care that we set it up in a secure way and do some configuration and we could do all that but there is an easier way if our goal is to deploy a web application and that easier way is an average service named elastic Beanstalk behind-the-scenes elastic Beanstalk will use easy to it will start such a server for us but it was you also all the other configuration we want to do and it makes it easy for us to ship our code onto the instance to manage different versions different environments like a testing or production environment and all these things well let's let's get started with elastic Beanstalk for that click simple you on get started and give your application a name I'll name it lateral simple block next you need to choose a platform and since we're using letter low this obviously should be PHP since levels and PHP framework we don't want to start the sample application instead we want to upload our own code and now it would be tempting to pick the zip file you downloaded from github right that won't work though the zip file which you upload here should contain all the source files and not a folder which contains all the source files therefore simply go to the folder you just download it and extract it this will give you a folder and now in this folder select all the files and compress them zip them again with your favorite zipping tool however before you do so make sure you do one thing you will see that dot Ian Vito sample file level managers a couple of global settings in its dot E&V file and by default this dot env fault is not shared when hosting or when shipping your code through a code repository like github which is why here you only have a dummy file and not an actual one you should copy that file and rename it to dot Ian we without dot example and now let's open that follows your favorite text editor I'm using sublime here and now you can edit it and one thing we do have to edit as we have to assign an app key here all the other settings can stay as they are for now I'll change this from local to brought though but this is up to you you can set any value here the URL can also stay as it is for now so we need that API key here the app key to be precise and to get one we can use the artisan command to generate one however for that we will need PHP installed in our machine so make sure that you have PHP installed you can then simply navigate into your folder and run PHP artisan and then a command but for this to work we actually need to do one our thing first we need to install all the dependencies if we have a look at our folder we see that it got no Wender folder here so all the dependencies including the lateral framework itself are missing that is normal you always distribute your code or ship it to get up with our dependencies like these since you can easily install them using composer in this case make sure you have composer installed on your machine link can be found in the video description and then in this project folder you downloaded and extracted you can run composer install and this will now install all the dependencies this project needs including level that will take a couple of seconds once it is done we can use the artisan command to generate a key so it did finish for me now you can run a new command still in that folder PHP artisan key : generate and this will generate this application key and automatically store it in this dot e + V file as you can see if you open it again here it is whether the project is prepared and now we can select all these files inside this folder so only the files in this folder and compress them this will generate a new zip file on windows of course use a program like sip or WinZip or WinRAR and whip that compressed you may take your compressed file rename it to whatever you like I'll name it Larry Wilson blog v1 and go cue elastic Beanstalk with it because here we need to upload our code and now we get a zip file with the code so let's choose the code here choose that zip file we just created which holds all our code including the dependencies including the vendor folder and let's click upload now as you can see in the bottom left is now ships our code to Amazon then elastic Beanstalk will spin up the ec2 instance we need and we'll ship our code onto the instance and also configure that instance to hold a webserver and so on before it does although we need to go through the remaining steps of the configuration we could click create application or we have a view look at the other options we can set up here we can for example choose which kind of fries that we want to use I'll go up the low-cost one which stays within this free tier as long as you don't have multiple of these apps but of course you could choose to a more production-ready one and you can change the environment settings I'll name the environment here differently I'll name it prod you can choose any name you like and you can manage different environment on the elastic Beanstalk for example for a testing environment a protection environment a staging environment new feature environment whatever you need now with this all set up the default settings should be fine and we can click on create app now this will as such assets been up that ec2 instance ship or occurred onto it set everything up there and I'll be back once that's finished the environment successfully launched for me and with that we get a link where you can visit our application so let's click it and there we see forbidden now do you have an idea where this error might come from it's tricky to spot but do you have an idea well you find out where this error comes from let's have a look at our level application structure this is the folder we uploaded and there as you probably are aware if you know a level the main entry point where the request is handled first is inside the public folder here we get said index.php file which is responsible for getting any incoming requests and then funneling it through the level framework you could say now we uploaded this whole folder to Beanstalk and therefore Beanstalk shifted to our web server on ec2 on this virtual server the issue is the incoming request therefore it doesn't hit the public folder it hits this folder the whole folder and there we simply don't handle it this is in the end the reason for this error we can easily change this though let's go to the configuration of our environment of the Prada environment here and there under software configuration we can configure anything related to our server which is running here so if you click on this Settings icon we can set a document root and this is exactly what we need here here we can decide from which folder from which subfolder the server should in the end serve our fuzz or where the request should basically be handled and we can set this to slash public so that now we target the public folder and there it will then automatically you reach the index.php file with this tiny change we will fix this error now what else can you configure here a couple of service specific settings about compression for example if you want to display errors and until when you want to or when you want to terminate incoming requests and some settings about logging now we don't need that but this public thing here is really interesting with that let's click apply here and this will update our environment now this operation takes a couple of seconds we can still reach our application during that timeframe most of the time at least but then at some point it will restart the web server and after that if we visit the application again this looks much better unfortunately the next issue isn't far away if we click on register and try to register here we get an error we get in sequel error that the connection was refused and this makes sense we're trying to connect your database in the level code but we got no database here by default no database is created on that virtual server wishes spun out by elastic Beanstalk behind the scenes now we could install a server on this ec2 instance by the way you can always have a look at this instance by simply going to the ec2 service and they're under instances you see that there's one running instance this is the one which was spun up by elastic Beanstalk so we can see that here and as I said we could install a database on my sequel database for example on that instance but the issue we would then have is that we have to fully manage that database it then runs on our own server we have to backed data up we have to make sure that we are protected against failures if the server for some reason goes down if we ever want to switch the hardware or upgrade to a bigger server we'll have to do everything about that switch on our own so we have to fully manage the database and that is a huge disadvantage for big enterprises that's of course doable and pretty common but for you there is a much simpler way of quickly adding a database to your stack let's go to configuration again and as you can see we are in the web tier or here are a couple of options related to the so to our website you could say now if we scroll down you see there always a data here and here we don't see any options as of now but we can attach a database to our elastic beanstalk environment here we can create a new RDS database and RDS stands for relational database service it's another service provided by AWS which allows us to spin up database instances so database servers with a configuration of our choice again regarding the power and then with database on these servers now this is a fully managed service so here we don't have to take care about backing the data up or updating software that's all done by AWS and they're always a free tier option available here so that we even don't pay a dime for the first year as long as again we stay in some boundaries as set on the pricing page by AWS so let's click create a new RDS instance and we're taking to this wizard here we could create this instance based on an existing snapshot which would basically copy old data into this to be created database I don't want that I want to start with a blank database we can choose the DB engine and I'll go with my sequel here now a sequel is included in the free tier and the version of the engine as you can see we get a couple of different versions five point six point two nine three four three five or six two six point two nine the instance classes are really big one here I'll scale this sound to t2 micro to stay in the fridge here and then we have to set up a root username and password with which we can access this database server I'll choose root here and also a password now here I just set up to automatically create a snapshot if the database server is ever going to get shut down so that the data is safe the fourth shuts down and that we don't want to copy that cross-multiply bill ability zones which is fine for this example with that let's hit apply and now it is the starting and mute database instance using that RDS service and on that database instance so on this database server it will also automatically create a new database this will take a couple of minutes but once this is starting up we can already think about a new issue we have to solve in our environment in our application if we have a look at the dot and fall which again manages our settings here we see that the database connection is set up by default it uses the local host database username these are all settings for our local development environment for whom sted specifically in this case obviously these values are not correct if we use the database host it on some server created or managed by AWS now instead we will have to enter the host so the address of this database server then the name of the exact database on that server and our credentials now I could already enter the credentials we just set up in a couple of seconds ago actually but it would be nice if there would be a more dynamic way of getting access to these values in the end elastic beanstalk is managing everything and it turns out it also gives you access to the database settings on the server superglobal how does that work we can go to the config folder in a lateral project and there we have this database PHP file in here we manage the configuration for the database and if we open this again with any text editor of your choice you see that here we can configure which default connections used you see that's my sequel and then for that connection things like the host the port the database username and password and these are exactly these things we need to define all you need to pass now outside of this return statement at the beginning of the file we can now define a couple of Global's the first one could be named RDS hostname and the name here is totally up to you you can name this globe whatever you like the value is not up to you though here we should access our server superglobal and on that superglobal we have the RDS hostname very low property we can access this property is populated by elastic Beanstalk in the end it passes us this value with every incoming request so that we have a chance of dynamically retrieving our database value and accessing the database and whenever we change something about the database we therefore don't you to adjust our lateral code so that is the hostname I need more than that for example we also want to adjust the user name here or get the user name and we can get this on this server superglobal and there on the user name property RDS username then we want to extract the password and we want to get this too and now where is this request is of course not sent from your client this is something LS it Beanstalk passes your application once the request is basically or has reached the backend already so this is not exposed to any users and finally the name of the database this is a database created by being stalked we have no idea what its naming it and here we can simply retrieve it without having to worry about the name with these clothes setup we can go down and for example here replace the host with the RDS host name global which will host the host the database here can be released with the DB name and the username obviously with the username whilst we can set the password well equal to RDS password whoops RDS password here with this this database PHP file is prepared and it's now able to well retrieve that data or that information dynamically and therefore connect to the database that's not all we all need to change something else remember that we could only choose for or between database my sequel versions 5.6 something we couldn't choose 5.7 and level 5.4 the version I'm using here actually requires my sequel 5 point seven or higher or a certain command at the point of time we run migrations will fail we can already set this up too and DISA Stan in the app folder under providers under app service provider there if you open that file you should first use schema here at the top to import that schema pass aid and then in the boot function simply execute schema and there the default string length method method and set it to 191 this will overwrite the default level otherwise assumes which is not possible when using or which will lead to an error when using an older my sequel database version as we're doing as we're forced to do in our Beanstalk app with this command however we will prevent this from happening it leads us to a new issue though or a new thing we have to think about migrations if we were to deploy our code to our server right now we would probably be able to connect to the database but what do you have to do the first time you set up your level application on a server be that your development environment or the production environment you have to run your migrations because of course in this project too I set up a couple of migrations here in the database migrations folder one for the user's table and one for the posts table and we have to run these migrations right at the start when you deploy this app for the first time to make sure that these tables are created in the database otherwise nothing will work now for that we can run the PHP artisan migrate command but how do we run that command on a server which is not on our local machine there are different ways of doing it you could connect to this ec2 instance which it was created by Beanstalk via SSH you could set up an SSH connection and then manually execute this command in the lateral folder on the server that is doable on Windows you will need extra gab program or application to run SSH though a different alternative or generally an alternative is to take advantage of behavior or elastic Beanstalk offers to you you can create a new folder here in your project and name this dot EB extensions now the name is important it should have a leading dot and it should be named EB extensions after the dot inside that folder you now can create files which have to end with dot config which will be run by elastic Beanstalk every time you deploy a new version and therefore here I can simply create a new file when we first enter container underscore commands and this is not something you can choose this as a command or a setup a configuration basically detected by elastic Beanstalk then indent with a tab we're using the yamo format here and add a name of the command this is now again totally usable by you the commands you list here will be executed in alphabetical order though and more information about this file can be found in the link in the video description all name is 0-1 in it DB : again indent and then here command : and then the command between double quotation marks which you want to execute and this will be PHP artisan migrate this will now migrate all your fault make sure to implement this fix I showed a couple of seconds ago before you run this so this fix regarding the default string length and what's that you can now save this file in your root project folder a name it for example in it config now course make sure to in the end place it in the study be extensions folder here and with that disk file look at executed by elastic beanstalk and the command in there will be executed to thus migrating our files now of course it will execute us whenever you deploy a new version too so for newer versions you should probably not include that because migrate would fail then since the database tables already would exist and that is why connecting Y is SH and running the command by doing this or by well having that connection might be the preferable way for such one-time-only tasks like this one but I want to show this capability r2 and of course there are a couple of ways of automating this whole elastic beam stuff proces even more for example with the elastic Beanstalk CLI back to our example your though we can switch back to the elastic Beanstalk console and it just finished here and with that we can go back and create a new bundle we want to deploy so let's first delete the old one and then select everyone here everything here and compress it again this again will take a couple of seconds and this new sip for now has to be deployed again I'll first rename it to letter ville simple blog video but the name is totally up to you and back in the elastic Beanstalk console you can now click on upload and deploy to upload a new application version here I'll choose this new file which holds the changes regarding the dynamic database connection this initialization script and this fixed for lateral 5.4 with older my sequel databases and we can now hit deploy this will now upload the code again and then exchange to code the old code with this new one whilst doing that it will not restart the ec2 instance or anything like that so we can still access our application while it's doing that but after a couple of second the new code will be live and can be used by us it already finished let's click this file here and let's now try this registration again I'll again enter my email address here and my password and click register and I get a connection refused error well it's kind of deceiving if we have a look at the fraud environment we see this ok at this check mark and everything looks great but actually if we scroll down we see that it failed to deploy the application so it simply didn't update the code it rolled back to our old application code now this error can be really strange at first it looks like we messed something up in our agamas and tells us invalid llamo but actually this is related to how mac and you can see a hint here stores hidden folders even though we can't see it here there is like extra subfolder in there you can simply fix this error or this strange behavior by going into the terminal into that folder so where it is zip file can be found in executing set - d wearable symbol block the zip file and then underscore underscore mac OS x /e x slash start and this will remove this annoying folder in there with this out of the way we can try uploading this again so let's choose this zip file again now we have to define our own version label since the old one is already taken since you already tried it with the same filename from which is infer set and now let's see if that works so let's wait for this to finish uploading and now hopefully it again it will replace our application code this time hopefully successfully and hopefully it now is successful running this initialization command setting up our database and adding the tables in there now that looks much better let's visit our web page again and let's try this registration process here again now we're forwarded now it seems to successfully user there's already hit the database and at center post and see if that all the works and it does now we get the application working on elastic Beanstalk with a dynamic connection to the database as you saw there were some gotchas we had to take care about as I mentioned there are of course more flexible or better ways of deploying everything through the elastic Beanstalk CLI you can automate a lot of the stuff you saw here but this is how you can ship your applications and there is nothing wrong about this approach so if you got a lateral application which you want to host this is how you could do it and for the first year as long as you don't use any other services at least this will be for free that is how that works in another with you I want to take a closer look at elastic beanstalk and what we can do any configuration what all just stop on the lectures about hopefully see you there bye
Info
Channel: Academind
Views: 105,109
Rating: undefined out of 5
Keywords: elastic beanstalk, laravel, database, rds, tutorial
Id: ISVaMijczKc
Channel Id: undefined
Length: 28min 7sec (1687 seconds)
Published: Thu Jun 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.