AWS Boto3 Python Crash Course with AWS EC2 Instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to aws ec2 with the bottom 3 python tutorial in this lesson i'm going to show you how you can access your how you can actually manage your ec2 instances by using a python button 3 package all right so these are the some basic operation which we will be doing with our python boto3 package like how to get the basic information of ec2 instance how you can start it stop it how you can reboot monitor unmonitor all those things we'll be doing here all right super so we are gonna use here the bottom three python package so the first of all you have to import your bottom three so we are gonna see like this one import bottom three once you import both boto3 thereafter we have to get the ec2 client so that's the bottom three dot client all right and in that we are gonna pass this ec2 so we have got the ec2 client and the first one first of all i am going to get here the details of my ec2 instance so ec2 dot describe instance when you are you write there uh describe instance you will get something like this yeah this describe instances just run it so what do you see there you see an empty result that's mean i do not have any c2 instance and you can see there clearly i do not have any running ec2 instance even i don't have any ec2 instance all right so it says that i do not have anything so the first of all i have to create one ec2 instance to get the list of easy to instance so what you can do you can i mean if you want to access it from this console you can simply launch a new instance but actually we don't want to launch a new instance from this console our task is to launch a new instance from this python programming and to do that if you if you know a little more a little about the ec2 instance so you need a security group and a key pair all right so this security group and key pair these are the necessary requirement before we create this ec2 instance so the first of all we have to create a key pair all right so let's go ahead and create a key pair all right so to create a key pair what we have to do we have to first of all import boto3 thereafter ec2 is equal to boto3 dot client sorry we have already called this ec2 so we don't need to get this client once again so it's like this one ec2 dot create underscore key pair thereafter you have to also pass here the key name uh which you want to use here for this key pair i'm gonna use it for kgp talking all right then it will return a result and in this result you will get this one ah this one is actually the key material but the problem is here i did not store it in in any of variable and if if i want to store it in a variable it will show an error because this one is already present in my key pair so to do that what i can do i can create a sorry let me see this key here sorry i'm having a little cuff today so you have here this key pair click on this keep here thereafter we are going to delete it actually we had just created it right now all right so okay i i have to write here this delete so the mistake which uh which i made here is that i did not store it into a new variable because i have to get this rest so this rest is having this key material and i i want to store this key material as a pm file so this overall key material is going to be inside our pm file because we will be needing it to get connected with our instance all right so the simplest way to store this in a pm file how you can do that it's like this file is equal to open thereafter i'm gonna say like this kgp talky.pm i'm gonna open it into a write mode thereafter file dot right and inside that i'm gonna write this one all right thereafter file dot close let's go ahead and run it now we should be able to see that this pm file is inside our current working directory just seconds ago it is created all right super so we have got our key pair now the last thing which we need before creating any ac2 instance is uh we'll be needing here the security group all right so for a security group we have to first we can also get the list of the security group okay so how would you make sure that you do not have any other security group or how do you make sure that how many security group do you have so it's like this ec2 dot describe security group if you remember earlier we had used this describe now we are going to use it for security groups so ec2 dot security group i do not have any so it should be an empty there yes so i do not have any security group that is why the most of things are empty here and there is only one security group which is a default security group so we can't delete it and we can't do anything for this particular security group all right superb so that's the default security group and you can see the security group from here as well and this one is only a default security group and if you see 6767 and 0 and that's the security group id is there all right superb now let's go ahead and create a new security group here how you can create a new security group all right so do remember these things we will be using the few of these things later on all right so to create a new security group it's like this resp is equal to ec2 dot create underscore security group thereafter you have to pass here the group name do you see there this group name all right so you have to pass sorry i should get i should copy this one control and see this group name and what is going to be this group name a group name i'm going to say here the kgp talkie all right this is going to be a group name thereafter there is description for each of these group name do we have any description here we should have all right so it says that i think we do not have here so the moreover uh we can set that here no issues all right so it's description and in this description oh sorry i do not need to put it actually here this sign so group name and there is a description oops sorry and uh in the description i'm gonna put here this one is kgptoki uh sg that's mean the security group so this is just description no no it's huge all right thereafter we want here the vpc id i'll tell you how you can get this vpc id vpc id so what is this vpc id this is virtual private network id and we do not have it yet so how we can get it so the id is here do you see there this id is here we can get it by describing our security group so this vpc id is a default id and i'm gonna put that id is here all right so after this vpc id and this group name etc we have got everything no issues so this particular security group will be created let's go ahead and execute this one and if you see this resp message it says that the 200 that's when this this should be created there let's go ahead and refresh it all right now do you see there this security group is created and this one is a new security group and the name of this one is this all right so all of these are created but one thing is missing here that is the inbound rule all right so inbound rule will allow any outside connection to access our ec2 instance to which we will associate this security group so that's the problem here and if you have access to the console you can definitely edit from here but we are assuming that we do not have access to this console and we want to set it from our now how you can do that now do you see this group id we will be needing this group id so you can see like this one gid is equal to resp and then this one is key that's the group id all right so this is a group id and we will be using this group id to set our authorization permission all right so what i'm going to do here i'm going to do ec2 dot authorize security group in grace all right that's mean these are the inbound rule connection so the first of all i have to set it for for a group id all right so ah sorry there is a mistake yeah so it's going to be a group id is equal to the group id is uh this one gid so we are going to set it for uh the group id thereafter we are going to set it for ip permission now do you see their ip permission etc we had here so these are kind of egress and we also need these permissions for in grace as well so i p permission all right so we have there this ip permission uh thereafter we are going to create it like a list and in that we will be passing here our dictionary and in that dictionary the first we will pass their ip protocol and let me see where it was yeah okay so the most of the information is not present here so the moreover it's like this i p protocol and other things we will pass there so the first of all we will pass this sorry i p uh protocol and we are going to pass this the tcp ip protocol thereafter we are going to say here the from port the port 80 i want to pass here all right thereafter this is going to be like 2 port this one is 80 thereafter i want to say that what is going to be the ip ranges on which i want to allow it so these are the ip ranges and in that we have here the dictionary and this dictionary is this one cidr all right so this one cidr ip i'm going to just copy it and paste it here so that a missy spell should not happen here thereafter if i pass this 0.00 all those things that means uh we are passing it for everything that's mean moreover i'm going to take this whole thing all right so i'm going to put that whole thing here so this one all right so this one is ip protocol for the port number 80 and the same thing i'm going to do here just wait a second i'm going to just a sec all right so this one is closing here the same thing i'm going to do for a port number 22. so the port number 22 is used for the ssh so there you have port number 22 all right thereafter you have all these uh information and then i think it should be ready let's go ahead and run it all right it's so it's a alright it says that some ip protocol error is wrong okay so this one got error no issues let's go ahead and run it once again it says that unknown parameter in ip permission ip ranges must be one of uh oh sorry this one all right so you know a lots of text stuff so it's possible to do some uh the spelling wrong here and it says that this one has updated here now if you repress it let me reload it you should see their inbound rules are updated and now and now the uh everything is everything is being accepted here for any http and ssh connection at port number 80 and 22 all right superb so you have successfully created key pair and security group now we are ready to create our ec2 instance easy to instance or the creation of ec2 instance is a little difficult and i'll be doing it just a little all right so easy to create ac2 thereafter you have it something like this let's say instance id we already have sorry the instance resource we already have ec2 so ec2 dot create all right so it's a create ah sorry uh ec2 instance cannot be created with the client so for that i i have to take the resource all right so what i'm going to do here i'm going to do like this one ec2 resource is equal to boto3 dot resource there we have ec2 now we have got this ec2 resource thereafter you need to click there create instances all right and there i'm going to read all the output which is coming into instances now inside this instances create instances we have to pass here the image id do remember this image id we have to take it from the i mean from this aws i don't know this image id all right literally so this email image id is not something we should always remember and to know the image id we have to do some trick here uh see when you are a regular user of aws then you do remember these image id which you want to launch but i don't know all right and to be honest so for that purpose see i'm not going to launch anything from this from this console i just want to take this image id from here all right free tier image id because if you don't know any of these image id you won't be able to launch any any of these machines so for that purpose i'm just taking this image id and i want to launch let's say this one and for that purpose i need this image id all right let's go ahead and copy this image id and that's it now i don't need anything else here so this image id you can also get from the internet as well if you search somewhere so there is nothing to do with the console here whether you are logged in or not all right so this image id i have got it here this image id thereafter ah it it is a free tier yes it is a free tier you should verify that whether it is a free tier image or not otherwise you will get billed all right so you have image id then i want here the mean count that's mean the minimum number of instance which i want to launch here that's one max count is one all right then we have here the instance type instance type that is going to be the t2 dot micro now once again if you don't know what are these instance type c you can understand these instance type from here but do remember i'm not launching anything from here i just want to show you that this t2.micro is only free tier eligible all right otherwise if you choose anything else from here like this one then you will get charged for that one all right so this t2.micro we will pass this information there once you get to know all those things you don't need to visit any of these things to to get these data so t2.micro thereafter the key name i'm gonna pass here the kgp talkie all right so do you know the key name all right so now i don't need any of these things i'm going back here all right so there you have ec2 dashboard and there you have the ec2 instances so i don't have any ec2 instance just to verify that all right so this the key pair if you remember earlier we had created this key pair and that's the kgp talking there after we are gonna attach this key name there and now we want to also attach their volume so this volume that's mean this is hard disk with our computer all right so the currently we do not have any volume that's mean we have to also create a volume but the creation of this volume we can do these things in the runtime from here so i'm gonna pass here the block device mapping is equal to sorry this would be the device mappings um is equal to like this we have to pass here a dictionary which should have a device name so there you have a device name all right so the device name which we are going to pass here this div and x vda do remember how i have got this one so i think i had seen somewhere though this one is address for this volume so it's like in your computer how these hard disks are referred so there is nothing to do with these things and the programming so it was somewhere i think on in the document somewhere i read that so it's like this dev and xvda this one is going to be the device name for the hard disk there after the ebs that's been the value which i'm going to the launch here so in the evs we are going to get here uh are these some settings i'm gonna do actually delete on termination and i'm gonna say here yes true and thereafter the volume size all right so the volume size i'm gonna put here the 20 gigabyte that's mean a 20 gigabyte of volume will be created here automatically and this volume will be attached to this ec2 instance all right superb now thereafter uh we have got everything here in the block device mapping thereafter the final step which we have currently that is the security groups so the security groups we have already created and this is list because we can attach a multiple security group with the ec2 instance so the the security group which we have currently that is the kgp talkie which we had created earlier and you can of course see those by visiting there so the security group name was a kgp talking now thereafter everything is okay and perfect you can run this one in a while a single instance of ec2 t2.micro will be launched and if you refresh you should be able to see there a new volume are created with the size of 20 gigabyte and these are some other information and it is just created a few second ago and still it is being created so it is not finally created thereafter if you visit in the ec2 dashboard you should be able to see there volume and keypair acceptor but still it is zero because we do not have any running instance yet it is just getting created perhaps all right so it still it is in initialization mode and these are instance id which is created and you can get that one from here instances it says that this instance id is created now do you remember previously when i started this video i did the first thing is describe these instances and we got their empty result now if you do it once again you should be able to see there a single device is running and all these information all right you see the if you compare this result with our previous result earlier we had got there just few line and the mostly the most of the lines were empty but here we have this particular image id which is launched and all other information about related to that image like the public ip address and the private ip addresses all those things we have everything all right superb so this is how you can describe an image now let me show you how you can start and stop your image all right so the image start and stop is pretty much simple only you have to identify that which image you want to stop and start you have to get the image id so you can get this image id sorry not image id it's instance id all right so the instance id we have just copied it from there and let's say i want to stop that that one so ec2 dot stop instances and in that i have to pass it like instances id sorry instance id is equal to because these are list so i can stop stop many instance at a time let's go ahead and run it now once you run it you should be able to see there let me so it here you should be able to see there this instance should stop in a few time yeah you you see there instance state is stopping that's mean it will take a time it will take a while to completely stop thereafter it says that this instance is stopped now let's say if you want to start this instance then simply you can type there start instance and instance id thereafter you will be able to see there this instance is being started that's when it is currently in pending mode but soon it will be uh it will be started all right so you can just replace it once again i think it is still in pending because of course i mean it's a windows ah okay so it's not a window so it is i think the linux machine all right so it's pretty much faster so if you had here windows machine then that might have been taken ages to start and stop so it's ubuntu and it has just start and stop within few seconds now it is in running state let's say if you want to delete this whole thing how you can do this pretty much simple ec2 dot terminate instance now if you execute this one then you will see there this particular instance will be terminated and one more thing to remember when we created this instance we were creating volume and we said that delete that volume on termination that's mean when we terminate our machine that's mean this volume will automatically terminated that's mean this volume size of 20 gigabyte will be automatically deleted but other things like the key name and the security group these things will not be crea will not be deleted now if you repress it you see this is terminated and in a few second this will not be visible in your instances because this is deleted all right but uh and one more thing you see there this volume is also deleted and it is not there the key pair and the security group will be there all right so do you see there and this key pair i'm gonna just delete this key pair because i don't need it now so i have already shown you the process how you can do all those things and i can do i mean i can delete these key pairs from the from here as well but i'm gonna just stop this video so i was doing it from here as well all right perfect superb so you have learned how you can do how you can manage your ec2 instances with 103 python package and i'm sure you have enjoyed this lesson thanks a lot for watching it please please please do like and subscribe this channel thanks a lot guys bye bye have a nice day
Info
Channel: KGP Talkie
Views: 3,854
Rating: 4.5999999 out of 5
Keywords: kgp talkie, kgp talkie videos, boto3 tutorial, boto3 aws, aws automation, boto3, python boto3 tutorial, aws python boto3, getting started with boto3, boto3 python aws lesson, boto3 tutorial aws, aws python tutorial, boto3 lambda, boto3 s3, boto3 python, boto3 aws tutorial, boto3 dynamodb, boto3 aws ec2, boto3 aws lambda, boto3 aws credentials, boto3 aws python, boto3 basics, boto3 bulk upload, boto3 bucket acl, boto3 client, boto3 code, boto3 credentials setup
Id: wzXekHrvldM
Channel Id: undefined
Length: 28min 59sec (1739 seconds)
Published: Sun Aug 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.