How to Deploy a Docker App to AWS using Elastic Container Service (ECS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys in this video i'm going to show you how to deploy a docker based application to aws's elastic container service um so i figured before i get into how to actually do this i just wanted to give you a quick little overview of what exactly we're going to be doing i do want to point out in advance though that you do need a couple prerequisites for this tutorial the first one is you need the aws cli installed and the second is that you need the cli configured with a user that has some very specific ecr and ecs permissions and i'm going to leave the policy template in the bottom of this video in the description so that you can create that user yourself and if you don't know how to set the cli up check out my other video i'll put that one in the description section below on a tutorial on how to do that precisely um so with that out of the way let's just go over what we're going to be doing uh so we're going to have a docker file a very basic docker based application it's going to be a flask application that basically just hosts a endpoint that gives you random pictures of cats and i'll show you exactly what i mean by that in a moment uh once we do that we're going to build that docker file up we're going to go and create a repository in ecr and then we're going to upload that docker image to ecr and after that we're going to go ahead and set up ecs now there's three main components there's a cluster a task and a service i don't want to go into too much about what these three things are so let me just give you a very quick little summary a cluster is just basically like a logical grouping of hardware resources and a task is a template that contains metadata about how you should deploy your docker container onto a machine and this is where you set up things like memory cpu port mappings environment variables and all that jazz and a service is more for advanced use cases that require for instance auto scaling that may require a certain number of containers are always up at a certain time they want load balancing they want to do deployment so blue green deployments uh stuff like that so they're a more comprehensive solution than tasks so that's a little bit about what these three things are in this video we're going to be using the cluster and the task so i think that's about it so let me go over now to my ide and visual studio code and show you the application that we're going to be deploying all right folks so here we are in visual studio code let's just take a quick little look at this application just so you get familiar with what's going on it's a very simple flask app we have a docker file in here that just sets you know the python version the working directory dependency install and it's exposing port 5000 then we just have our app pi file which has a bunch of urls to different pictures and it just serves up a random one when you hit the just base endpoint and all this other stuff we don't really need to care about so we're gonna go down to the terminal over here and we're just gonna say docker build dash t and we're just going to say test we're going to build that image out so let's actually just run that now we say docker run d we're going to say publish we're going to expose the port 8888 on this machine and we're going to point that to port 5000 on the container and we're using test of course we got our success code back there uh so let's just check oh localhost 8888 and this is basically what we got and if we refresh this a few times we can see the cat pictures are randomly changing so that's what we got going on with this basic application and this is what i want to deploy to the cloud so instead of it being localhost this is going to be an ec2 machine at the end of this video so let me close that down and let's just move on to the next step now now the next thing we need to do is essentially log into ecr and this is really where the user with the correct permissions comes into play so i have this command copied and ready to go so you can just take a look at what's going on here so we're saying ecr get login password uh we're specifying the region and we're just saying docker login username is aws this is my account id 755 yada yada yada and this is again the same region that you specified up here and i'm going to put these commands down in the description section in a github gist so that you can kind of copy this and follow along at your own leisure so i'm going to click on enter now now if you have any kind of error you're going to get that error now but i already have the correct permission set up um so obviously you see for me login succeeded so at this point we can actually go and create our ecr repository because we're logged in to aws ecr so let's just go and do that now so i'm here on my aws console and we're going to go to ecr elastic container registry service and sure let's click on get started and let's just call this thing test because that's why i named my container anyways leaving everything default here i'm going to go ahead and click on create repository now let's go back to our terminal and actually upload the image that we just built onto aws ecr so again i have this command already copied and ready to go so i'm just saying i'm going to use the test repository and i'm going to mark it as latest again this is my account id the 755 yada yada and again the region here is usc 1 for me and then we're just repeating the um just the name of the repository and also it being latest now press enter there and so that's just going to tag your image but you actually need to upload it as well using the push command and so again i have that copied and ready to go so you just say docker push and the same thing press enter and if you have a slow connection like i do this may take a little bit but i'll fast forward it right now alright so that finally finished so we can just go ahead and close the ide now we actually don't need this anymore everything else we do is going to be in the console so we're back on ecr here let me just click on refresh and we we can check out what changed so if we click on this guy now we can see that we actually have the latest image tag and should say like the size yeah so it's a couple hundred megabytes um so we're just gonna copy this uri here for our um image and i'm just gonna put that aside on my notepad over here because we may need that in another step okay so that's it for ecr so now we're going to go to ecs so i'm going to click on this guy over here on the left alternatively you can go to the services bar just type in ecs and click on it and this is essentially what the home screen of the ecs area looks like and here i was kind of experimenting a little earlier so what we first need to do as we kind of mentioned in the early roadmap video here is we first need to create a cluster so let's go ahead and do that now so clicking on create cluster what do we got here we're going to select the second option ec2 linux plus networking because we actually want to create ec2 machines as part of this cluster now going down to next step over here at this point we can define a bunch of different options let's just say this cluster is called the cat app cluster you can call this whatever you want it really doesn't matter from here since we're going to be using ec2s it's asking for what type of pricing or provisioning model so we're just going to say on demand since it's easier for me we're going to be using the t2 micro or t3 micro actually i think it is are the free tier eligible so that we don't have to pay anything we're going to leave all this other stuff default we don't really need to set anything here now for networking we do need to make some adjustments so we're going to use the default vpc that's available with any aws account so you have a vpc associated with yours you may not know that you have it but it's there i assure you so we're going to click on the first subnet here and then we're going to make sure you select this option auto assign public ip set that to enabled that's actually important for later and the security group again we're going to use the default security group that is associated with this aws account now for the container instance iam role you can click on create new role if you have one already that's fine as well at that point we really don't need to do anything else we're just going to click on create down at the bottom and let the cluster create itself so this may take a moment or so i'll fast forward this if it takes a little bit too long but essentially what's happening right now is that it's creating the cluster itself it's creating the iam policies that are needed for it it's also bringing up a t3a micro instance so if we go if i duplicate this and i go to the ec2 section of aws ec2 we should be able to see that an instance is coming online in a moment here and there it is just as i went to it it got brought online and you see that it belongs to cat app cluster and it's currently in the initializing state so that's how these two things are connected your cluster is going to create hardware or create resources for you so that you can deploy containers onto it so i'm just going to leave this open for now because it's going to come in handy a little bit later so here we can see this is finally done so now we can just go to view cluster and just take a look at what's going on here so if we just click around here now obviously we don't have a service set up we're not going to be doing that in this tutorial we don't have any tasks set up because we we haven't created that part yet but we do have an ec2 instance so that should show over here and there it is it's the ec2 instance so let's go to the task section now up on the top left here task definitions and create a new task definition for this project so we're going to do this based on ec2 fargate is like an abstraction of ec2 so you don't even have to worry about hardware if you're using fargate uh but for us i wanna use ec2 so we're gonna click on that and we're gonna go to next step and we're gonna create a task definition name so um let's call this ecscat app demo task demo task task roll none that's fine network mode we're going to leave that in default that's fine as well um for the amount of memory we can just leave this as default um like 128 mb is fine i think it's got like a special notation you need to give here uh yeah so like 128 maybe capitals it may be case sensitive uh let's just say one oh what's going on here you know what let me just put in one this is going to be short lived anyway so it doesn't matter now let's say 1vcpu over here and this is the point in which we actually add the container uh so we specify rules regarding that container so go ahead and click on add container now we need to specify the container name so ours is called the cad app container right and this is why i copied the arn of our ecr repository from the previous steps so let me just pull that out really quick on the side here so now if i just copy that in we're pretty much good to go at this point we do need to do some port mappings magic because uh if you recall the container is on port 5000 so we need to add the mapping of the port that we want to expose through the docker container here so we're going to put in port 8888 that's going to be the local port on the ec2 machine and the container port is going to be 5000 tcp is fine health check we don't need any advanced nonsense so we're just going to say add down here and where are we okay so cats app container it looks like it got set up we don't need an elastic interface we don't need constraints we don't need basically anything else here but you can take a look at some of these options if you're interested in them i'm going to go ahead and click on create now all right so that task definition got created successfully now we need to go back to the cluster and actually deploy a task so we're going to go back here and on the tasks bar we're going to say run a new task and we're going to say ec2 and let's say this is the one that i just created ecs cad app demo task we're going to say revision 1 which is the latest we only want one task here you can run more than one on one machine if you want that's totally up to you um but yeah we're just going to use one for this example uh we're going to use azbalan spread that doesn't matter for this case let's go ahead and click on run task and at this point the application that is loaded in ecr um as the docker image is going to be deployed onto this machine and you can see here that's exactly what's going on we see that it's currently in the pending state and it's going to eventually go into the running state so let's keep an eye on this uh that's weird why did it uh just disappear all right so we can see that it's currently in the running state i actually made a small mistake in the previous step when i defined the task definition i put in the wrong url or i guess there was a typo for the ecr container so i ended up getting a container that you can see here couldn't start correctly so that's kind of just shows you what can go wrong here so at this point it should be good to go to go and check out what the application does when we hit it on port 8888 we should get that cat app right so let's go and get the public-facing ip address of this ec2 machine that's hosting this application so here's the public dns we're going to take this and we're going to copy it and we're just going to paste this in on port 888 now this does not work right away and you may ask yourself why and the reason is because our vpc currently has configuration on it that's preventing any traffic from coming in so we need to go ahead and fix that so that we can actually expose port 8888 so go back to the instances section now if we go down to security groups security groups now there's a security group that's associated with this machine so and so this security group basically has rules associated with it such that it's not allowing any incoming connections from the public internet so we need to go and fix that and expose port 8888 so let's go ahead on to the left here and click on security groups and so this was from a previous little experiment but our default one that we're going to be using here actually was this one when was this created it doesn't say 25 157. no this was from a previous one so if we go ahead and click on this um and we look at down here where it says edit inbound rules what we need to do is add an additional rule to expose port 888 so let's say yeah custom tcp that's fine we're just going to say port 888 and we want it's available to the public internet and that looks good so that can be called from anywhere so now if i go ahead and click on save rows let's actually do that for um ipv6 as well so 888 and then this one i believe it is and click on save rules so now we just basically opened up our vpc security group so that it can accept connections if we go back here and click on refresh this should work and there you go everything is working correctly so i hope you enjoyed this video and learned a lot about ecs and how to deploy applications to it make sure to check out the other aws videos on my channel and as always if you enjoyed this video please don't forget to like and subscribe so that you don't miss out on my next one thanks so much everyone and i'll see you next time
Info
Channel: Be A Better Dev
Views: 61,080
Rating: 4.9266057 out of 5
Keywords: aws, aws simplified, awssimplified, programming, coding, cloud computing, software design, software engineer, software developer, software design principles, software architecture, docker ecs setup, docker ecs tutorial, aws ecs tutorial, aws ecs ec2 tutorial, amazon ecs docker, docker, aws cloud, aws tutorial, aws training, python docker ecs, python docker tutorial, aws ecs guide, aws ecr ecs, how to deploy a docker app to ecs, docker aws tutorial, aws docker tutorial
Id: zs3tyVgiBQQ
Channel Id: undefined
Length: 15min 10sec (910 seconds)
Published: Mon Sep 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.