AWS CLI for Beginners: The Complete Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone I wanted to make a video going over the basics of the AWS CLI and if you're not familiar with the AWS CLI it's just a command line interface for creating managing and destroying your AWS resources and so anything you can do through the console you can also do it through the CLI and it's actually a lot quicker doing it through the CLI because you don't have to click through multiple pages in multiple windows you can just do it all through a single command so I always recommend people who want to get you know more familiar with the cloud as well as AWS definitely familiarize yourself with the AWS feel like is it's a really powerful tool and it's going to help save you a lot of time when it comes to managing your AWS resources installing the AWS CLI is very easy all we have to do is just google AWS CLI and the first link should take us to the AWS page and so yeah if you want to navigate directly to there just go to aws.amazon.com slash CLI and you'll see that there's an installer for Windows Mac and Linux so I'm gonna walk you through all three of them for the most part it's it's really easy and the steps are pretty much the same regardless of operating system but there are a few nuances that I want to point out so I'll walk you through each one step by step we'll start off by doing the windows first so I have a 64-bit machine so I'm gonna just select that and then you can do run or save I'm just going to hit run and eventually the Installer should pop up and so at this point we can just keep hitting next we will have to accept the terms in license agreement and if you get this pop-up just go ahead yes all right so we can hit finish and now we want to verify that the CLI was installed properly so go to the search bar down here and just search for CMD and that's going to be our command prompt so this is the Windows command line and here all you have to do is just type in AWS - - version and at this point you should see an output that looks fairly similar to this so this is just confirming what version of AWS CLI that I'm running somewhere I'm running version 2.0 at 22 you may see a later version if you're watching this video in the future but as long as you see this output that means AWS CLI was installed properly if you see any other output it probably means something went wrong so just probably repeat the steps and/or close out your current terminal and then open up a new one sometimes that helps out as well but that's all we have to do to get it installed on windows let me show you guys how to do this on a Mac now so to install this on a Mac let's make sure we navigate to that same page such as Google AWS CLI and once we get to this page we're gonna select the Mac OS s the Mac OS s the Mac OS PKG installer and we'll hit allow and you should see something download right there so just click on the box we want to double click on the dot pkg file and so we can just keep hitting continue and you will be prompted for your password you alright and once that's done open up your terminal and here we're just gonna type in AWS - - version and so this is this should ideally it'll do two things it'll confirm if it was installed properly and I'll let us know what version of the AWS CLI that we're using and so just like in the windows section right it's gonna print out that I'm using version 2.0 dot 22 and so when you see this output that means your install went through and everything's good to go you see anything else that probably means AWS CLI did not install properly so go ahead and just repeat the steps or you know just do some googling to see you know if you get some kind of error just google those errors and most likely someone else has already hit it and so there you go guys so that's all you have to do for a Mac setup in the next video I'm gonna show you guys how to set this up on a Linux machine to install a WC Li on a Linux machine all we have to do is just search for AWS CLI and that should take us to the page with the Downloads files and so we'll click on the first link if you ever want to navigate directly there it's gonna be aws.amazon.com slash CLI and on the right side right here we can see that there's a Linux section so we can just download the Installer here and you'll see that the steps for Linux is slightly different from the Mac and Windows and so here you know you have to select which which type of machine you're using so I'm using a Linux x86 64 bit and so at this point we can just copy this right here and then paste it into our terminal and you'll see that I got a couple of errors and that's because when we tried to do this curl come in and says permission denied and that's just because we need to run this with sudo so what we want to do instead is just copy each line individually and then type in sudo and then paste that line in and we're just going to do this for all three steps making sure to do a pseudo beforehand if you get an error and then finally if we can do the last one that already had pseudo in there all right so now if we do an AWS - - version like it tells us - that's going to confirm if it was installed properly and so if you see an output that looks like this below that means AWS CLI has been installed and it's gonna let you know what version you're running which I am running version 2.0 dot 22 okay so we've got AWS CLI installed at this point we can actually start working with our AWS resources well we're almost ready to do that we still have to set up our credentials and I'll walk you through how to do that in the next video now that we have AWS CLI installed the next step we have to perform is downloading our access keys from the AWS console and so these keys are important because whenever we run a command from the AWS CLI the CLI needs to know you know what user are we trying to perform these commands with or as and so we have to give it the access keys of a specific user so that it can actually use those credentials to call the backend API on AWS and so to actually get our credentials I want you to go to the AWS management console so you can go to aws.amazon.com slash console and just go to my account and then AWS management console and then just login you and once you're logged in I just go to the top right up here where you see your username and hit the drop-down and go to my security credentials and in this page we want to select the access key section and then there's this nice blue button right here that's gonna allow us to create new access keys so we'll hit that and this is very important guys so we can only see our access keys once and if you hit this show access key this is gonna be our access keys and it's important to understand that you know once we exit out of this right here once I hit X I will not be able to see my secret access key so it's important that you you know write these down save them someplace Amazon gives you an option of hitting this download key file so if I hit download and then just save it then saves it as a CSV file so I can just open it up in a text editor and these are gonna be my credentials so make sure you store them someplace safe and however you know if you do end up you know losing your credentials it's not a big deal because once we create these keys we'll get an option to delete them so you can just delete it and then just create a new access key so it's not a big deal but now that we have our access keys what we want to do is we want to go to our terminal and I'm doing this on a Windows but just do this on a Mac or Linux terminal if you are you know on those operating systems and the commands that I provide for the most part gonna be the same regardless of operating system so if there are any differences I'll make sure to point them out and so what we want to do here is we want to type in AWS and then configure and so here it's going to give us a few prompts to enter in our different access key so it's asking for the access key ID so I'm just going to copy this and paste it in as asking for the AWS secret access key so we'll take that and copy that and then it's gonna ask for a default region and so if you don't know what a region is basically Amazon's got a couple of data centers you know well not just a couple of a decent amount of data centers scattered throughout the world and each location for these data centers is going to be a separate region and if you let me see where I can find this let me go back to my account are let me go into services in an ec2 and so you can see right up here it says Northern Virginia so this is the default region that I get associated with and Amazon just picks whatever region or data center is geographically closest to you so I'm on the east coast of the United States and so Northern Virginia data center or what it's refers to as EU s East one region is going to be my default region however I can actually select any region to work with and I can actually deploy resources into any one of these regions but this prompt is just asking what do you want the default region to be if we ever run a command and we don't specify what region we want to you know deploy a resource to so I'm just gonna do US East one because that's where I deploy most more things but uh you know if you live someplace else you may want to pick whatever region is closest to you and then default output format so when we run commands it's going to spit out some output and there's several different options of how you want those outputs printed out you can get in a JSON format you can get in yamo format you can get in text and you can actually see all the options here in the documentation so if we go to documentation and I think it's under user guide let's see that installing configuring AWS think it's here let's see you here we go so output format so if you can click on this configuring the AWS CLI you'll see the different output formats that we have so we've got JSON yamo text and table I like my defaults to be JSON but feel free to pick whichever one you feel most comfortable with I'm just gonna do JSON now at this point we're done with our configuration and we technically should be able to run the AWS CLI commands and it should be able to automatically login into our account and perform those actions but you know when we run AWS configure I want to actually kind of explain to you what it did and so if you're on a Windows I want you to navigate to your home directory so generally your prompt is already gonna take you to your home directory however if you don't know how to get there and it took you someplace else it's always C Drive slash users and then the name of your user and we can actually navigate to it using the File Explorer and so if I hits this PC C Drive users and then my username you'll see that it created this dot AWS folder right here and if we click on that there's going to be two different files so there's gonna be a config file and a credentials file and so when we ran AWS configure it went ahead and created those two files for us and we can just take a look at them real quick so let's go to the configure one I'm just gonna open this up in notepad and if you take a look at the configs you'll see there's just a couple of lines so there's this top line that says default which I'll explain what that is in a second and then we've got the region so you remember we specify what our default region is so that's going to be us east one and then our output format is going to be JSON and our credentials you know I'm sure you can already guess what's going to be in here but we can see that once again it has this default which I'm going to explain in a second or two and then it's got our access keys that we provided so Amazon is going to look into that dot AWS folder to get all this information so that when you run commands it knows what configuration settings to use as well as what access credentials now just to quickly show you Mac users and Linux users how to get to that dot AWS folder because it's a little bit different but pretty much the same though on these machines so if you're on a Mac or Linux the steps are exactly the same you want to go to your your home directory and on a Linux or a Mac machine the easiest way to get to your home directory is just do a CD and then do the little tilde okay that's gonna take you to your home directory and if you want to know the exact path it's gonna be slash users and then the name of your user so it's pretty much the same as a Windows machine just know C Drive and if we do it LS you'll see that there is no dot AWS folder but that's just because that's a hidden folder so if you do LS - la you should then see oh well I didn't actually run AWS configure so let me run that real quick so AWS configure gonna type in some garbage okay so now if we do an LS - la we should see a dot AWS folder get created so we can move into there and in there we should see the same two files and if we take a look at those files you'll see the same thing we've got the default and then we've got a region and our output and our credentials gonna be the same thing okay and so this is the same for a Linux machine okay so let me now go back to my Windows machine actually cuz that's what I feel most comfortable with and let me explain to you what this default is so this is referred to as the default profile and a profile in AWS CLI world is really just a collection of settings and credentials that you can apply to an AWS CLI command and so when we run commands on the aw CLI they're going to take these configurations and these credentials and use them when we run that command however we can set up other profiles if we wanted to so we don't just have to use the default profile the default profile it's just a special profile because it's gonna be the default one if we don't specify a specific profile in our command however if we wanted to make a special profile you know I can just call this Sanjeev then give him his own you know access key you know dot dot dot and then you know my own AWS secret and I can even specify other configurations that I might prefer so if I wanted a interesting thing though actually so here you just specify the name of the profile but when you're under the config folder you have to do profile and then the name of the profile so Steve so you want to make sure it matches so you don't need the word profile here but you need the word profile here and then here I could say my region let's say I want my region to be us West one us West to and an output maybe I want yeah Mille and so now I have a default profile and a profile name Sanjeev that I can use and so anytime I run an AWS command I can always pass in the - - profile flag and then specify oh I want to use the Sanjeev profile however if I do not include a profile flag or a profile option in my command it's just going to take the default configuration and the default credentials in the last video I showed you guys how to set up your access key so that AWS CLI can use them however some people may not like storing their credentials you know on their machine like that it could be considered a security vulnerability and so you know there are other options that you can use to set up your authentication for AWS CLI and one of those is using environment variables and so this is something I'm sure you guys are already familiar with but we can actually define these environment variables to hold all of our access keys and then AWS CLI will check with them directly to get those values and the nice part about storing it in an environment variable is that there's no document really that's storing it on our machine like there's no file that has those values the only way to do that is to actually just run the command to print out all your environment variables so it's a little bit more secure than storing it you know in that credentials file in your AWS folder and I'm just gonna quickly walk you through how to do that but I definitely recommend you check out the documentation cuz you know even after I explain this I'm sure at some point you're gonna forget it and it's much easier to just go to the documents and quickly read it and copy and paste these commands sort of you know coming back to this video and so under the the ADA boys command-line interface user guide this is the direct link you just want to click on under configure the AWS CLI you want to select environment variables and they've got solid documentation so you can see the different commands that you have to do to define all of your credentials as well as your default region and so this is pretty much just copy and paste so you just copy all of this well this is for Mac so I'll show you how to do this on Windows real quick but you just copies this value right here and then you go to your command line can i paste copy it and then you put in whatever your access keys are that you got from your AWS console right so that's all you to do I'm not gonna actually do that right now because I already have it saved in the file and I want it to overwrite that now you just do the same steps for all three of these and I don't know how to actually verify if a if an environment variable is set on Windows machine I wonder if you could just to set X set yeah so if you type in the word it said it's gonna show you all of your environment variables so once you run these commands I just run a set and then you can just verify if it actually worked on a Linux machine we can just go there so let me go to my R let me go to my Mac machine so you know Mac and Linux once again are exactly the same so to set the environment variables let me go to the documentation on this guy and we can we can set it on this machines this is a virtual machine and so here you know we can literally just copy this and you obviously want to put in your own credentials don't copy the one from the AWS documentation but once you hit enter if you do a print env and then do a pipe grep AWS you see that new environment variable that you saved and then once you do the next one you should also see that one as well you and so now if we do a print Envy pipe grip AWS we can see that these access keys and so now AWS CLI can just use these values to connect so it's a little bit safer than storing in a file however if you reload you'll see that these environment variables will get deleted and you'll have to redo it but kind of like logging in every time you reboot not a big deal and there's actually ways to get that to persist by saving these environment variables in your - in your dot bash RC file however you know at that point you know you're saving your credentials in a file and so it's not quite as secure and if you're gonna save in file you might as well just save it in your dot AWS credentials file alright guys so now that we've got everything set up and we can connect to AWS I want to start getting you guys familiarized with the AWS CLI command structure and the basic structure of a AWS CLI command is going to be listed on this page so if you're not here on this page I recommend you pull it up you just go to the user guide I am and go under using the AWS CLI and then command structure and you'll see that every command starts with AWS and then we have a command a sub command and then as many options and parameters that we want okay and just to kind of give you an example of what that would look like if you go under let's see under this section right here under Amazon ec2 key pairs you'll see an example of a command so you've got AWS you've got the main command you've got the sub command and then you see all of these options and flags that are being passed in so that's the basic structure of a command it's not very complicated however it can be hard to kind of remember you know what are the different commands and sub commands that we have at our disposal what are the different options we have so if you go up to this getting help section right here you'll see the CLI has a lot of tools to kind of make our lives a little bit easier and one of the main ones is using the help option so we can always do in AWS help it's kind of almost like a man page for the AWS CLI and so we can just kind of space through all of the options that we have at our disposal we can see all of the different services that the CLI provides and helps us manage and you can even go under a specific command like ec2 which if you don't know it's used for managing ec2 resources and we can do a help here and we can see all of the different sub commands that we have so if we want to associate an address we can do that and associate a private IP address we want to attach a volume attach a VPN gateway so we could see all of the options that we have under the ec2 command but I think that's enough that what I want to do now is I want to walk you through deploying a resource and we're gonna do it first through the AWS console which I'm sure you guys are like oh we already know how to do that but I want to show you how the steps that you perform on the AWS console and how they map to the AWS CLI so I think that's important so let's go to the AWS command R R to our console and so login to your console go to the ec2 dashboard I'm the hit running instances so I've technically got one running instance but what I want to do is I want to just launch a new instance and here you'll see that we have all of the different a.m. eyes and so I'm just going to select the first main one which is the Amazon Linux to AMI and you'll see that the ami has a unique ID and this is important once we get into the AWS CLI but this is basically telling Amazon you know what image we want to use to deploy our ec2 instance so we'll select that we then select the instance size so we're gonna stick with free tier which is a t2 micro but you know we obviously have all of these sizes that we can use with the ones down here being a lot more expensive and a lot faster and a higher performing it so at this point you know we can technically go into configure instance details to you know modify other settings and configuration parameters for this ec2 instance but for now I'm just going to review and launch and here we can hit launch and it's going to ask us to choose a existing key pair and you may not have one already so these I've actually manually created and so what I want to do is uh before we actually deploy this just keep in mind that we actually do need to have a key pair and I'm not sure if it gives you a default one it may not so let me quickly walk you through how to set up a key pair and so we're just going to cancel out of this for now and under the ec2 dashboard just scroll on down to key pairs under Network and security and you'll see I have these two key pairs but if you just made a new account most likely you won't have any so we'll just hit create key pair and so now we can create our key pair and I'm just going to call this test one and just to signify that we made this through the console I'm gonna type in - console but the name doesn't matter and then we can choose between a PPK file and a PEM file so you know if you're on a a Linux or Mac machine you'll probably want the PEM here on a Windows machine you want the PPK however if you do download a PEM for Windows there's obviously ways to convert it it's not a big deal so we can create this key pair and so we've got this key pair and you'll see that it downloaded the PPK file or the PEM file if you selected that so we can use this to log into ec2 instances once we deploy them but I just wanted to walk you through setting that up so now we can go back to the ec2 dashboard and deploy our ec2 instance again so we'll do the same steps I'll select the Amazon Linux to AMI t-too micro review and launch and when we do launch we can now select that new key pair so test one console you have to acknowledge that and then we'll launch the instance so Amazon's launching that we can go to view instances to take us back to the ec2 dashboard and you can see the one that's pending is the one that we just deployed and so that's going to deploy now and you know hopefully you guys kind of understand what we do uh you know what we need and what information we need to deploy a c2 instance at the bare minimum right so we obviously need an AMI we need a instant sighs so the nmi with the linux AMI and the instant size was the t2 micro and then we need a key pair okay and so these are all things that we need if we're going to deploy it from the AWS CLI and so that's why I wanted to walk you through how to do it through the console but now let's go ahead and actually run our first AWS CLI command and you know we'll start off by creating a key pair within the AWS CLI so let's go to the command-line interface let's go to the user guide it actually walks us through how to do that so if we scroll do down to using the AWS CLI with AWS go under Amazon ec2 an ec2 key pair it's gonna show us how to create a key pair so you know we start off with a AWS we type in ec2 and then the sub command is create key pair and then we have to give it an a parameter of key name or an option of key aim and then the other two are just there so that you know we can actually save it to our computer because we don't just want to create a key pair we want to actually save that PEM file onto our machine and so we're going to just copy this exactly and this should work on a Windows or a Mac machine the only time it won't work is if you're using the Windows PowerShell which is a little different than the command-line tool I don't actually use the PowerShell but it is available and so if you're using the PowerShell you want to make sure you copy the second line but we can take this and I can just paste it in and make sure you don't copy the dollar sign you don't want that and feel free to change any of the details so if you want to save this to a different file so we'll call this um my awesome key pair and then the key name we want to store on AWS it generally you want to you want them to match so that you know which one maps to which one but technically you can call it whatever you want but let's just call it the same thing so we'll call this my awesome key pair and then we can hit enter and so now if I do a dir which is going to show me all of the files on my machine or you know what let's just go to the file explorer and so here under user sanjeev i saved it and so you can see my awesome ki pair so this created a key pair on AWS and it downloaded the PEM file onto this machine and if we go back to our console we can verify if it actually did that so we'll go to key pairs and we'll probably have to hit refresh and so now you can see my I misspelled it but you can see that the AWS CLI created this key pair for us alright guys so that was your first AWS command and you know you'll see that working with the CLI is very simple it's it's pretty much this just that you know AWS your commit and your sub come in and then a bunch of different options that you got to pass in and what flags an opportunity to pass in well you got a-you got to go to the documentation right so there's a reference guide let me see whether this is the reference guide here so if you go to you know AWS CLI Amazon AWS comm v2 documentation and then all of that I don't feel like sitting all of it and then you can then search for ec2 and then under ec2 now you'll see all of these sub commands that we want create keep air and then you'll see all of the flags and options that we have for this sub command but going back to the user guide there's our a few other things that we can do from the CLI so instead of having to go to the console to verify that we successfully created a key pair we can actually do that through the CLI and so we can do AWS ec2 describe key pairs and so here you can see that they're passing in the key name if we don't pass in a key name it's going to list all of the key pairs so that's what I'm gonna do go to AWS easy to describe - key - pairs and so for me it printed out the three or four different key pairs that I have so I've got a main key I've got my awesome key pair I've got Sanjeev and then test one console all right and you know once again with the CLI anything we can do in the console we can do in the CLI and actually I don't know if this is the case for AWS but I know for a sure there's actually some things that you can only do through the CLI that you can't do through the console I don't know why they ended up doing it like that but you know maybe Amazon is the same way I'm not sure so you know that's why I always recommend learning the CLI cuz a lot of times it can be significantly more powerful and you'll see it's a lot quicker than just having to click buttons through the console but let's go ahead and delete a key pair so I'm gonna pick our you know my awesome key pair to delete so we'll do AWS ec2 delete - key - pair and then we have to pass in the key - name parameter and then we'll just copy this name right here okay so now we can go back to the console and verify but you know we don't need to do that because we know how to verify through the CLI so we can just do AWS easy to describe key pairs and there you go guys it's gone now all right so we've created a key pair on your machine now hopefully if you deleted it go ahead and bake it again because we're gonna deploy an ec2 instance don't wanna make sure you have a key pair before you do that and so in the user guide the next step is going into ec2 instances and this is going to show us how to ec2 instance by using the AWS CLI so you can see that there's the AWS the command as ec2 once again but now we have a run instance sub command and if you look at the flags that you have to pass in or the parameters you'll see that they kind of make sense because it's pretty much exactly what we were doing from the console so we have to pass an image ID which is the ami ID and if you already forgot what that is or where to get that just go back to instances and then if you want to verify what the ami is of a specific incidents that you're running you can just click on it and you'll see the Amazon the ami ID right here and you can click on that and you can just copy this ID however if you want to kind of look through the marketplace and see the different a.m. eyes you can search here so if you want I don't know a Windows AMI this is gonna be the mi ID for a windows 2019 server but we'll just we'll grab this am i right here so we want to deploy the same Amazon Linux to AMI you to the the user guide and let's just follow along so here it's passing an account of one that's optional I think it defaults to one but if you wanted to deploy five really quickly you would just do - - count five then we have to specify the instance type which is t2 micro so that's a requirement and then you pass in the key name that you have created so that you can actually log into the device and then here you see they're passing in a security group ID and so you know every use every instance has to be associated with a security group I don't know if I actually have any security groups and I don't know if there's a default one but before I kind of proceed further let me make sure that there isn't a default if there is a default security group or not you and so it looks like there is a default security group that comes with our V PC so I don't think we should have to pass in a security group and it should automatically just default to that so that should be fine let's go back to the user guide and let's actually try to create an ec2 instance and so we'll do AWS ec2 run - instances and then we want - - image - ID and I can't remember if I copied the ami so let's go back and I'm just gonna copy this am i and don't copy mine because yours may actually be a different one because every region for some reason they flip the ami IDs from region to region I don't know why they do that so make sure you actually follow these steps and copy it from your own machine if you're on a different region all right and so other flags we need the instance type of course so - - instance - type you and this is gonna be 82 micro and then we want to pass in a key name so - - - name and so just make sure you grab one of the keys that you currently have so I'm just gonna copy this Sanjeev right here and if you don't remember you know just do the Amazon ec2 AWS ec2 described key pairs command and that's going to print out all of them so I'll paste that in there and you know like I said we can use the default security group so you don't technically have to pass it in but if you did have a specific security group you want to associate with that ec2 instance make sure you pass that in and so let's hit enter and run and it looks like I messed something up so let's see okay so it looks like I copied the wrong ami so for some reason when I do the 64-bit arm when it gives me an error but when I do the 64-bit x86 it doesn't so I'll probably have to do a little research on why that didn't work but anyways just go ahead and just copy the 64-bit one and then so now when you run that command we can see that it prints out a whole bunch of output and so all of this output is information about the instance it just created so we can see the image ID we can see the instance ID that Amazon gave it so it's got a few other details regarding the device like the instance type the key name launch time we see the availability zone that went to the US East 1e and we've got you know the VP CID so it's going to give us all the information about that device and if we go back to our console and if I cancel and exit out of this we could see the new instance that it just deployed right here and so you can see it's initializing it right now but you know we have successfully deployed a ec2 instance using the AWS CLI and you can see yet just how quick and powerful the AWS CLI is now we've I've showed you how to create a key pair how do you delete a key pair how to create an instance let me show you how to modify an existing resource so what we'll do is we'll take this instance that we just deployed using the AWS CLI and we'll give it a new tag okay so we'll give it a name tag so that we can see a nice pretty name so that we know you know what this instance is and what it's being used for and once again to do that I recommend you go to the documentation and there's actually a section on this so if we just keep scrolling down a bit adding a tag to your instance so what we want to do is we do the AWS ec2 command but now we use the sub command of create tags and then we have to pass in a flag of our parameter of - - resource and then give the resource ID and we can get that you know from the output that we just got and then we pass in all the tags we want to give it and so we'll do that step-by-step so to get the instance ID right that's just right up here however if you already lost this output or if for some reason you know you've you close out your command line you don't have the information and you don't want to log back in to the console we can actually run a command to see all of our running instances and just like you just like with the key pairs and just like with the key pairs remember we had the the describe instances sub-command we can do the same thing for ec2 so we can do AWS ec2 and then describe - instances and it's going to print out information regarding all of our instances and you'll probably only see one if you just deployed one and let's just say we want the - - instance IDs cuz I don't want all of that information well it looks like it's going to print all the information but anyways let's just grab the instance ID of one of our machines it doesn't really matter which one I'm just going to copy that and let's create a tag now so here we go to AWS ec2 create - tags and then - - resource then paste in the instance ID and then now let's give it actually this window is probably a little annoying for you guys so let me just kind of minimize that and so now we can kind of work on it side by side so once we have the instanceid we can then go ahead and create our tags so we'll do AWS ec2 create - tags then - - resource and then paste the instanceid then when you do - - tags and then give it a whole bunch of tags and so the first thing we have to do is provide the key for the tag so we'll do key equals and then I'm gonna call this tag a name or the key is going to be name and then we have to assign it a value and the value is going to equal how about my first instance you so let's hit enter and now let's go back to the console and let's take a look to see if it got updated so if I hit refresh look at that you can see we gave this instance a tag of my first instance so this is a special tag the name tag which will allow us to kind of see the name right here but if we click on this instance and go to tags you can see all of the tags that got assigned so we just had the name and then my first instance so that's how you can add tags and modify a pre-existing resource using the AWS CLI alright guys so to kind of wrap up this section with deploying and modifying and working with ec2 instances altogether I want to show you guys how to kind of clean up the mess that we've made in AWS and show you how to delete our instances as well as delete our key pair just so that we can clean up everything in our AWS environment so that hopefully we don't get you know any unnecessary bills and you know this is obviously a skill you need to have whether you do through the console or through the CLI so let's figure out how to actually delete an ec2 instance and so what we're gonna do is we're gonna delete the ec2 instance that we gave that name tag and the way to do that is well let's check the documentation and so if we go to the command line reference so if you go to you dock stylist at amazon.com CLI slash intercom you want to go to this tli reference and it says you know right here that we're viewing version 1 of the aw CLI and we're actually using version 2 so we want to click that and go there and then here we wanted to scroll down and look for ec2 and we want to look for something that looks like a delete or a terminate and I already know off the top of my head it's a terminate so we want terminate instances and so this is a pretty easy command just like everything we've worked with you just do AWS ec2 terminate - instances and then - instance - IDs and then you can pass in as many instances as you want so if you want to delete you know multiple ec2 instances you can do it all with one command and so let's do that and we'll do AWS ec2 I was a terminate - instances and then - - instance - I D actually was it I its IDs with an S and so now I've got two running instances so I'm going to turn I'm going to terminate both of them and so we'll copy this instance ID right here and you can look you can click this copy to click board I'm gonna paste that in there and then let me grab this one as well I'm gonna paste that in there as well and let's run that and so it gave us some output letting us know what's going to shut it down and then I guess terminated but if we hit refresh here we can see that it's in a shutting down state so it'll get shut down and then once it shut down it'll then terminate both of these ec2 instances and so that's how you delete an ec2 instance and let's also clear out our key pairs so we've got a couple of key pairs and so let's delete these through the CLI and once again probably gonna have to use the documentation so let's go to the reference page and we want let's a key pair if there's anything create key pair Delete key pair this is probably the one we want and so we just do delete key pair - key name and then the name of the key pair you and I'm just going to delete test 1 - console all right and no output was a cent so I'm not really sure if it went through or not but we can always hit refresh or even better let's do a let's print out all of our key pairs so if we hit the up arrow key we can see all the commands that we ran and there's a there should be the command that we already ran describe - key - pairs but that should show us all of our key pairs and so you can see here we just have main key and Sanjeev so we deleted test1 console and we can verify that in the console as well and so now we just have these two key pairs alright guys so now we just deleted a couple of easy two instances as well as our key pair and so you know hopefully so far you guys are learning a lot about the AWC line and getting more comfortable with it in the next section I'm not really sure what we're gonna cover probably kind of start covering like s3 or something like that because ec2 and s3 are usually the two main services that people always like to use with Amazon so I definitely want to cover that in this crash course in this section we're gonna start using Amazon's s3 storage which is their cloud storage offering and so you know just like we did with the ec2 instances we're gonna start off by using the console I and then just see what steps are required to you know create a bucket to copy files over to a bucket and things like that and then we'll delete all of that and then we'll figure out how to do it through the console so that once again we can kind of map how those two are essentially doing the same thing just in a different way and so if you haven't already done so click on services and navigate to s3 and so if you don't feel like searching for it you can always just do s3 and should be the first result and so here what we need to do is create a bucket we'll create a bucket and we got to give it a unique name I'm gonna give it a we'll call this pizza and wings Pizza - wings it's gotta be a unique name so if anyone else has the name that you're trying to use it it's gonna error out and so select your region so I just use my default region hit next I usually just skip all that hit next and then block all public access that's fine for now and then we'll create our bucket and so now we've got our s3 bucket and we can go in there and now we can you know upload files create folders and things like that so let's upload a file for real quick and so a lot of file and I don't really have any files on this machine so I'll just upload my PEM file don't really see why you'd ever want to but just for demonstration purposes we'll upload that next you're gonna get a few options I used to just keep hitting next and leave everything as default and so we've got our file now in our bucket and we can even create folders so we'll say test folder and if we want we can you know move this think we can move this and then we can move this can we move this into a folder I don't remember how to do that through the CLI I can't drag it now it's got to be through here it says I don't have a folder maybe I have to hit refresh now you here we go yeah I just had to refresh so now we have the option of selecting the test folder I'll hit choose and then now we can move it into that test folder so we've now moved it into there and yeah that's kind of all I really wanted to show you I mean we have the option of selecting this and then you know we can download it so we can download as and then just download it as file other options are you know we can delete a file and we can go back to the parent directory we can delete our folder and then finally let's go back to s3 and let's just delete our bucket and to confirm a delete you have to just type in the name of your bucket so that's kind of um you know what we can do through the console playing around with buckets obviously there's a lot of options that you can do you can do versioning you can do security and things like that on your bucket but the core concepts of s 3 is just managing your file so let's figure out how we can do all of that through the command line and so if you don't already have the documentation go ahead and pull that up also if you do AWS CLI and documentation user guide and they've got some great examples if you go under using AWS CLI with AWS services and then select s3 and then high-level s3 commands and so we've deleted everything hopefully and you know let's start off by just listing out all of the buckets we have so right now you shouldn't have any on this you know you've already been playing around with AWS but to print out the list of all of your buckets you can do in AWS s3 LS and that's going to list out all of your buckets and so you can see here we don't have any buckets at the moment and so let's go ahead and create a bucket and a way to get to create the bucket is by using the mb sub trainer which I think stands for make bucket go to AWS s3 mb and then s3 colon slash slash then the name of the bucket so we'll do pizza - wings again right so it made that bucket and we can do an AWS s3 LS to confirm that and so now we've got our bucket print out the contents of a bucket we can do an AWS s3 LS and then s3 colon slash slash pizza - wings and it prints out nothing because there's no actual files or folders inside that bucket because we just made it but you know taking a look at the documentation you can obviously print out the contents of you know the entire bucket or a specific folder within your bucket so that you know you can really see whatever you want to see we'll come back to deleting the bucket later let's play around with our with our bucket now so one of the things you want to do is copy files from your local machine over to your s3 bucket and so we use the CP command to do that so you do AWS s3 CP then the source of the file and then the destination of the file and then let's ignore grants for now let's keep it simple so let's see what files I have if I do a dir o or just look at the file explorer I can see all of the different files I have right now I'm in my Sanjeev Balder and so I've got these two key pairs so let's upload those again I can do AWS s3 CP then the full path to get to those files and so I'm already in that folder so I can just do you know my key pair PEM and I want to copy it to my s3 bucket and we'll copy it - pizza - wings and if I hit enter at this point it's just going to send it to the root directory of my bucket however if I also wanted to create them folders I can create you know let's say my PEM folder okay and so now it should have created that folder and moved that file which was on my local machine on to that folder so first of all let's verify in the console and there should be a refresh button so here we've got the bucket it created go in there we can see the PEM folder and if we go on there we can see my keeper and you know once again we can obviously do this through the CLI go to AWS s3 LS f3 : / / you can defeat the - wings so you can see that folder but if you wanted to go in that folder we can do that as well we can see now my key pair that's cool and all but I think where Esther he gets really powerful is using the sync functionality so you can sync a folder really quickly with one command to your AWS oh and even the other way around so you can actually sync your AWS to a folder on your local machine so let me show you how to do that and let's say we want to basically sync whatever is in the downloads folder on my machine to AWS okay well to do that we do AWS s3 sync and then we provide the source and the target so we want the source to be our our download folder and the target is going to be s3 : what was it yeah s3 colon slash slash and then pizza - wings and so now it would just sync it to the root directory but I want to create a separate folder so I'll say we'll call it downloads - back up and so you can see here it's inked all of the files and folders that were inside my downloads directory and placed them into the downloads - backup folder within my bucket and so there was four different files and if I go to my management console go back to pizza and wings it refresh you can see the Downloads and backup folder got generated and you can see the four files that was in my downloads directory and let's just make sure those were the files that were actually in there and so yeah you can see the four files and it looks like there's a hidden file that it also copied over great now let's create a new folder and I'll just call this a test folder and let's sink it again and see what happens so if we run the same command it looks like it didn't do anything but I think that just maybe because I have no files in there so if I create a new file in there now inside my test folder I'll just call this test file if I now do a sync it now uploaded that file and also created that folder folder and there's our test file however here's the interesting thing let's say we delete that file now and I'm gonna delete this guy and if I do a sync now you know I want you to try to figure out what's gonna happen right you're probably thinking that it should delete the file but you can see nothing changed and if I hit a refresh here nothing happened the file is still exists and if you take a look at our documentation that's actually the expected behavior so if you want to sync the folders and delete files that got deleted as well from your local machine then you actually have to pass in a flag so that it actually sinks and deletes file so by default it won't delete the files and you have to pass in the - - delete flag so let's try that now - - delete and so now it deleted the test file we can just double-check and that file is gone perfect now let's say we accidentally you know went into our downloads directory and deleted all of these right and we're like oh no what do we do well since we are backing up this folder we can do a sync but in the opposite direction so we take all of the files in AWS and copy it to our local machine and the commands exactly the same the only difference is we flip the source and the target so this is going to be the source and this is going to be the target you all right and now look at that guy's see we copied all of those files from AWS onto our local machine alright so the next command we should probably take a look at is the arm command so that's how we delete a file on s3 so we can do AWS s3 RM and then you just have to give it the path to the file that you want to delete so we'll do sync oh nothing is three colon slash slash pizza - wings and then let's just pick a file to delete so it refresh let's delete root key dot CSV in downloads - back up and then route e @c SB and I misspelled that Oh what's happening here Pizza gotta get the two Z's make sure this file goes away there you go guys so now we deleted a file we also have the option of moving files as well and so we can move a file in AWS from one directory to another or we can even move files between our machine and AWS so you know till now we've been copying files but we can also move them as well and so as an example let's let me go to my downloads directory gonna just delete everything for now all right and let's see what happens when we do a move so we'll do AWS s3 move or MB sorry and then we want to move from s3 to our machine so we'll do s3 colon slash slash pizza - wings /downloads - backup and we want to copy test 1 - console dot PPK and we want to move it to our current directory and the way to do current directory is a dot okay and so here if we hit refresh you can see the files gone so it wasn't a copy we didn't copy to a machine we physically moved it and now if I go to our file explorer we can see that the file is here and we can do this in the opposite direction so we can move files from our machine to AWS s3 and so all we have to do is AWS s3 copy be test1 and we want to move that to this path happen here MV command all right and so if I do a dir just to see the contents of this directory you can see there's no files there's no files as because I moved it to AWS hit refresh and we can now see the files up there in AWS all right and so that's most of the things I wanted to cover I guess the last thing is let's go ahead and let's delete our bucket so that's the final thing to show you guys and it's good to clean things up when you're just learning so that you don't get unnecessarily charged so to delete a bucket it's just AWS s3 RB in the name of the bucket but we'll do AWS s3 RB s 3 colon slash slash pizza - wings and it's going to fail that's expected and that's because our bucket is not empty by default Amazon will not let you delete a bucket unless it is empty however we can pass in the - - force flag and that's going to make sure that the bucket gets deleted regardless of what what its contents are so we'll hit the up arrow and then just do - - force and you can see deleting the files and then removing the bucket so if we go back to our console well actually let's check on the CLI first AWS s3 LS and we've got no buckets and if we go back to us three it refresh no buckets oh you have it guys so I think that's all I'm going to cover in this crash course I plan to make more videos on AWS as well as the AWS CLI I and so if you guys want me to cover any specific topics let me know and I'll be happy to make a video just either message me or I'll post a comment I don't know if I'll actually read the comments but yeah just find some way to reach out to me let me know what you guys want me to cover and I'll help you I'll be happy to do so
Info
Channel: Sanjeev Thiyagarajan
Views: 48,676
Rating: undefined out of 5
Keywords: aws, cloud, amazon, command, line, cli, beginners, tutorial, crash, course, ec2, s3
Id: PWAnY-w1SGQ
Channel Id: undefined
Length: 61min 0sec (3660 seconds)
Published: Sun Jun 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.