Introduction to AWS EC2 - How to deploy a Rails App to EC2 for the first time

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you want to deploy a web application to Amazon ec2 but man it's just so confusing to figure out how to do it maybe you've used Heroku in the past maybe you're really good at Hiroko but for some reason when you get to this menu you click on ec2 you have this dashboard load it's just really confusing to figure out what button you're supposed to press and how you're supposed to configure your instance how is this menu supposed to lead you to a computer in the cloud that you can customize and deploy a web application on well I'm going to tell you exactly how on this always be code in screencast [Music] from my console I'm going to click on ec2 and then I'm gonna click on launch instance there are seven steps I need to go through before my instance is live step 1 choose an Amazon machine image already gives us our first big scary menu what the am i is asking you for is what you want the base operating system of your instance to be these are all different distributions of Linux and Windows that all have different packages and libraries installed on them we're just going to use Amazon Linux the top one this comes prepackaged with Python and Ruby and that's we're gonna need both of those for this example and click select you can always install new libraries on top of your default operating system and we're going to end up doing that now choosing an instance type this is the next option this this is talking about the size of machine that you want to deploy now each type of machine has different amounts of CPUs and memory and RAM and storage the T 2 dot micro is the default one this is about six dollars a month to run in production but if you have a fairly new account you have free tier eligibility which means you don't need to pay for it at all the reasons you would want to use a larger instance are things like running the JVM and doing highly you know memory intensive processing I use the C 3 dot large when I do JVM related stuff but for this example T 2 dot micro is fine you can go to AWS Amazon console SEC 2 slash pricing if you want more detailed and granular pricing so for example that C 3 dot large I use is 10 cents per hour per machine around 53 bucks a month which is way cheaper than this machine of this size would be on Heroku so I can tell you that now go next configure instance details you can ignore this menu add storage ignore it tag instance ignore it now number 6 configure security group this ends up being very important now by default your machine is deployed on Amazon's cloud somewhere in order for you to gain access to it you need to open up permissions on on their on their system through a security group that allow you on your host machine which in this case is my laptop - you know access that device so we need to create a new security group here I'm just gonna call this ABC example security group and these are different access rules that this security group will have by default you have an SSH port open on port 22 22 is the default SSH port and this is the IP addresses that that gives access to if I do my IP this will just be my local IP address or no one else could access it we're just gonna leave this as anywhere so you can add additional rules to this but we're just gonna use SSH for now if you don't know what SSH is SSH stands for secure shell and it's a way for you to access the terminal prompt from of the the cloud machine from your hosting chain so you can use your own terminal command line on your host machine and access the cloud machine through that so now let's click review and launch this gives us one more time to look at the different options we provisioned with and click and launch now something else you're going to need to do is you're going to need a key pair now the key pair is what you use to access the machine so this is like the private access key that you need to keep super-secret safe and secure we're gonna create a new key pair called ABC example key pair and I'm gonna download this to my desktop we're gonna need that to SSH into the machine but we can do that later so let's click Launch instances it'll say that our instance is now launching so if we go back to our console and click ec2 and go to the running instances we can see that this machine that we just provisioned is spawning up and it'll be up in a sec so now my instance is finished booting up let's SSH into it I'm gonna start by copying the public IP address that Amazon created once the image finish launch it if I go to my desktop I can see that I have that dot PEM file from the key pair menu that we're gonna need that so let me go to my terminal and let's try to SSH into it by running the command SSH - I path to the PEM file which in this case is on my desktop and then ec2 - user @ and then paste in the IP address from the dashboard so what this is saying is SSH into this machine as this user so Linux will use this signature user @ machine by default in the Amazon Linux distribution that we chose when we provision the machine ec2 - user is the root user so let's try and run this hit enter a couple times and it fails and it fails for this unprotected private key file permissions weird thing the reason this happens is because that PEM file doesn't have the right permissions on it for us to use it to route SSH into this device we can easily fix that by Kumada get and we can do Kamado undred and then the path to the file and you're always gonna need to come out this file when you want to SSH into your machine and you will always forget to do it it's one of those really annoying things so we come out it and all let's try it again and we're in now we're in we're ssh into a cloud machine now we're pretty bare-bones on our cloud machine here some might say that's a good thing and they're probably right but it means we have our work cut out for us we want to make anything of substance run here should be able to do a quick LS and just see what files we have doesn't look like there's much here we do have Ruby though so that's pretty nice we also have Python that's pretty nice also but I want to try to get a simple web page in a simple web server running and then be able to find a way to access that from my host machine so let's make a directory called example simple web CG into it and let's quickly make a index.htm now Hey look VIN works there's something /head and this is just gonna be the simplest HTML page you've ever seen in your life cuz it's just gonna have an h1 tag I'm just gonna say hello from an ec2 instance if I could type sweet and in order to run this I'm gonna run Python - M simple HTTP server I'm gonna run this on port 3001 so this is actually serving and if we try to access it the way you would think to access it by putting in the IP address and then port 3001 let's see that this doesn't really do anything it just kind of idles there forever normally when you try to access your cloud instance on you see - and it's just idling forever that means that you have an issue with your security permissions and lo and behold we never set HTTP access on this port so there's no way for us to access it time to go back to our development console and look what's going on so let's go to security groups pick the security group we use for this which was this ABC example SG and look at the inbound traffic settings we only have an SSH port on 22 available we need to edit this so if I go to edit it you know let's add a rule and let's allow for custom TCP rule TCP on port 3001 and notice that this is still idling by the way it's pretty interesting 2001 allow it from anywhere and hit save this will actually change the security permissions and we should be able to in real time see this go through maybe not real time but I did have to hit enter but once I hit enter it does work and now we have hello from an ec2 instance and that's running from this here we can even see the default logging from the simple HTTP server I was just running a static web page let's take the difficulty up a notch and try to get a rails app running first and foremost we're gonna need an actual rails app it's easier to just make this locally so I'm gonna go back to my local machine and run rails new example app CD into the directory open an atom and just make a couple modifications just to have a homepage route 2 pages homepage at controllers pages controller dot RB class ages their application controller make a home page method and let's just render text this is text coming from a rails app save it and let's just test this locally by running rails s - P 2002 going to low goal post 2002 and we see we have this is text coming from nails app rails app so let's take this app and let's try to get this onto our cloud machine and get it running so one hack for how you can get this on to your cloud machine is to use github just deploy it to get oven and clone it down on the other machine so let's try that out right now github we will go to a new repository make one called example app leave it public great repository I'm gonna use the HTTP URL just so I don't need to deal with us so sage keys on the cloud machine so get remote at github here and I'm sorry I need to get in it first get commit commit initial commit and then get remote add github and then get pushed github master master go to our example app and there it is so now let's try to clone this down on the ec2 machine go back here kill this and I run get clone well actually let me CD back to the home directory and run git clone this actually won't work because guess what git isn't installed we're gonna need to install it ourselves Amazon Linux uses yum as the default package manager so in order to install git well that needs are on sudo yum install get all and yeah you're gonna need to get used to installing packages if you're gonna do any kind of manual DevOps Heroku provisions all this stuff for you but you know what we're not on Heroku right now so once it's installed you should be able to see that we have git and we should be able to get clone from the remote machine get URL and it looks like it worked if we do a LS - yeah we should see that we have the example app right here so we can CD into it and yeah yeah but I have a rails app but it's not gonna actually run until I set up the dev environment I need to install a bunch of different scripts in order to do that and this will all be online but I'm just gonna fast-forward through it now so now that I have my rails dev environment set up I can run the server using rails ass on port 3000 straight and you need to add this option - be 0.0.0.0 don't ask me why you need to do that but when you have this on ec2 you do so now with that set up we should be able to go to the IP address slash port 3003 remember when idles that means your security permissions aren't set up correctly so if I go back to my security group and I can add a custom rule to allow HTTP access on port 3000 three four men II we're save that and now this should go through if i refresh it this is text coming from rails app so we have a rails app on ec2 rendering this text but that was a lot of work I had to go through to configure that dev environment so that would work the next time I spent up a ec2 instance I don't want to have to reinstall those libraries I'd love if I could just spin it up with the rouse environment already configured and there's a great way to do that called an Amazon ami if you remember when we first configured our instance we had to configure it from an ami an ami is just the Beast image of the hard drive that you want to start your instance off at but you can take a snapshot of any instance that you've already configured and installed certain libraries on and then use that as the base ami to spin up another instance so in this example I'm gonna go to my instances and I'm gonna go to image create image but this is going to do is it's gonna clone the hard drive of this cloud machine and store it on my AWS console so that whenever I spin up another ec2 instance I can spin it up from that image so let's go ahead and make an image named called rails dead and let's create the image now it's gonna need to shut down this running instance for a second while it creates the image but it should be done in a minute once the hard drive is done cloning I can go to am eyes and my rails dev environment here and I can launch it and I'm gonna launch a t2 that might grow from that yeah that that ami that I just chose to launch from so instance type t2 micro configure as storage tag security group we'll use an existing security group this ABC SG which exposes the exact same ports we will review and launch and we'll use a existing key pair that's the same key pair that we already have and let's launch it view instances so now we're spinning up a second instance that has a separate IP address but it should have our exact dev environment set up so I should be able to just once this is spun up I should be able to just SSH in run the server etc etc etc normally you don't route each UV traffic to an easy-to instance directly rather you put it behind a load balancer the load balancer can help route traffic to one or more running instances so you can have high availability I'll show you how to set it up right now if you just go to load balancers go to create load balancer and then create it inside this PPC thing and I don't know just hit plus plus plus plus and you see that this has the same kind of logic that we had on ec2 and since that whenever an HTTP request comes in on port 80 which is the default HTTP port on the load balancer an ec2 instance so you don't need to define the port at the end of it I'm gonna route traffic on the back end to HTTP port 80 but instead I'm gonna try to route it to each GP port 3003 where we were running our rails app so whenever you query whenever you make an HTTP request to the load balancer route the traffic to an ec2 instances any ec2 instance that's behind you with port 3003 it assigned security groups didn't like something that we have to give it a name so we'll call this example load balancer stiva security groups and then let's do the default UPC security group and configure health track so the way a health track works on the load balancer is that we're going to give it a port to ping and if that pain returns an HTTP status of 200 then it'll add a check and then this is the threshold for that so this is saying I want to make a ping request to any ec2 instance behind the load balancer every 30 seconds once there's 10 healthy requests then I'm gonna consider that easy to instance healthy and I can route HTTP traffic to it if it's unhealthy I'm never gonna route anything to it so that's the way that you can you know do upgrades to your machines where you can have a couple machines responding to HTTP requests and then you can take a machine down have it be unhealthy the load balancer will stop routing traffic to it you can change some stuff get a new version of your app up and do these high availability deployments where you're not having any downtime I'm gonna just make this very simple I'm gonna do a check every 10 seconds and if it does let's say three in a row then we'll consider it healthy just for demonstration purposes so this is the endpoint it's gonna ping I'm gonna I'm gonna actually do port 3001 where we are running that Python module and we'll just have that index that HTML file be the thing that responds to the 200 status request to the ping requests and then we'll add any ec2 instances so I have the two ec2 instances one that I created and one that I cloned from the image let's put them both behind that load balancer and then create it so now I have a load balancer it has a public DNS name right here so I can go to that and obviously nothing's going to happen right now because none of those instances have handled their ping requests properly I need to ssh into them and start up the server which I'll do right now so let's go back to our ec2 instances let's find the IP address of the new one so this isn't the one that we were using before we're gonna do a new one now and we'll do SSH - I use the PEM file from the desktop ec2 - user at the IP address we're in CD in - whatever that example simple web and run our Python server - M simple HTTP server on port 3001 so now we should actually every 10 seconds start seeing a ping from the load balancer because the load balancer is going to be constantly routing traffic to these different ec2 instances trying to see if they're healthy so that I can start routing web traffic to it yeah and as you see we just got a request from the load balancer so we're gonna start seeing these requests come in 10 second intervals and you see that this is gonna keep happening once it hit once it gets 3 once it gets its third one to respond the load balancer is gonna think it's healthy and it's gonna bring this instance online so it just did so we can go back now to our load balancer and let's look at the instances underneath it and one of them right here is in service so now if we send web traffic to that instance it will consider that in service so that is a super brief overview of ec2 just to get you started obviously in a production app that gets more and more complicated manually configuring and managing all these instances is gonna get really complicated so you might want to look into tools like chef or puppet or ansible that have like really good tooling for configuration management for making some of this stuff easier also that ami that I made at the end of the episode there's an entire community of a Mis of pre-built custom flash images that you can spawn ec2 instances from so if you want a really fully baked to Rails that environment you can go find a community ami for it and spin up an ec2 instance from it also you might want to look in a docker docker is a competing product to virtual machines it's competing products ec2 docker has its own container service called ECS I've done a screencast on it so you can check it out anyway I am at always be coding on Twitter check it out please hit up the handle let me know if you have requests for videos or if you have any feedback got some really good stuff coming this week we are working on an ethereal screencast to do a full baked introduction into aetherium development and writing smart contracts and I've got some neural net stuff in the work so machine learning neural nets hand coded from scratch it's gonna be really good stuff peace
Info
Channel: Decypher Media
Views: 30,878
Rating: 4.9492064 out of 5
Keywords: aws ec2, ec2, amazon ec2, rails app deployment, ec2 rails, aws rails, introduction to ec2, introduction to aws, elb, aws elb, ec2 beginner, ec2 intro, ec2 overview
Id: jFBbcleSPoY
Channel Id: undefined
Length: 21min 26sec (1286 seconds)
Published: Tue Jul 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.