Creating EC2 Instance using Terraform and Delete the Instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome back to my channel in this video we are going to learn about how to create an ec2 instance using terraform let's get into the video in the first step as we are going to create ec2 instance on aws we have to look at the documentation what and the terraform says for creating the ec2 instance so i'm on the terraform website registry.terraform.o so under under there click browser browse providers click aws and click on the documentation you'll get two different types of options i mean based on the resource that you are going to use you can make use of the specific section uh of a terraform code as we all know terraform is a infrastructure as a code we can create the whole infrastructure with the help of terraform code so over here before we go into the easy to commands let's see what is uh available in the aws provider for uh terraform so what they're saying terraform 0.13 and later if we are trying to create any resources we will have to use this specific section of command but in case of terraform 0 0 12 and earlier we don't need to specify this section anymore um so this is for uh more redefined command perspective but for the sake of this video we don't need to worry about it because um it it will still work if you're not using this particular section okay so um there are two main things we need to understand so whenever we create an resource it could be aws related resource or azure google it doesn't matter so first thing we need to make sure that how we are authenticating our terraform platform to the provider so uh authentication is the main key part of in order for you to log into the specific provider in this case aws so you are able to authenticate by using different input information set like static credentials environment variables shared credentials code build easy to instance metadata service there are different types are available in aws but today we are going to use static credentials under static credentials what we are specifying provider in this case aws and the region where you are going to create the ec2 instance along with access key and this secret key information okay so how do i get my access key in the secret key so in order to create access key and secure key you need to um log into your aws console under iam you will be able to create an user so i'm clicking the iem so as you know iem is in a global feature just go ahead and click on the users and add users here i'm going to use terraform as a user for example so we are going to use programmatic access uh based on the access key and the six access key id and secret access key select this and click next permission we need to give the permission so i'm going to give administrative administrator access for this particular user go ahead and click next um tags is not mandatory over here um just going ahead and creating the user so once you create the user you will get access key id and secret key okay so you are now set with the user that you wanted to authenticate with the aws go back to terraform website so these two details are ready for you now you can simply copy this going back to my atom workspace trying to create a new file my ec2 dot tf click enter so this is your workspace just paste the command that you have copied else you can just type the information whatever you have seen in the terraform website so provider aws and i'm going to create a ec2 instance on vesta2 and then we need to copy the um the access key and the secret key similar way copy the secret key yeah i think there is a forward slash okay so we are set with the authentication details it doesn't mean and that you will be able to create a used instance only which with the help of these details so next go back to terraform so here we are going to create the ec2 instance so under ac 2 you will get different options um we are what we are going to create creating ec2 instance so click on the aws instance over here it talks about the example usage it has a different variables for us to use in order to create the easy instance but we it we don't need all of the information in order for uh for us to create the easy instance so the main uh thing that we need to consider here resource so what type of resource we are going to use which is aws instance in our case and the name of the instance which you wanted to assign and amin id so which is something you will get it in the um aws website itself you supposed to know this information before you create the ama for you instance type what type of instance the instance type is based on the capacity of the instance so let's for let's take example so i'm going to click on the launch instance so in our case we are using west to re area region you see us west 2 okay i'm going to select usbest 2 so once you select the specific reason because ec2 is based on the region specific i'm going to select amazon linux to ama so this is the ama code that you want to do and no and create okay so we know the pma code and just copy this whole command and go back to atom i'm just naming this my ec2 instance an ama code we're going to copy it here and for the sake of sake of example collect click select t2 micro t2 migrate a micro is in a feed free tire so i am going to use t2 micro for my example okay then the tags is is an optional value for us so i'm not going to use the tag value here yep so now the command is ready so in the first section we are creating the authentication credential information static authentication credentials and then the resource type resource type name and then ama ids code instance type capacity once your atom file is ready with the provided details and the resource details go back to your command prompt under type um where i mean the project folder where you have the terraform exe file available type terraform.exe space init which would eventually try to download the plugins related to aws as you are trying to use the aws provider very first time next you need to type terraform.exe space plan so this will eventually check the command that you have inputted on your atom this will say what are the changes it is going to make in the aws platform so it says it is going to create the aws instance with the ama id of this one and then what else instance type as t2 micro so rest everything is known after apply which means we since we haven't chosen those specific configuration those configuration will be applied will be seen after you apply it so that's it after this type terraform dot exe that apply this will take on uh on its own time to execute this command set whatever you have inputted in the atom here it says do you want to perform these actions yes i want to perform this actions if you want to perform type s if you don't want to perform type no so here i am going to type s so after this it will take time to create the instance so now it is started creating the instance okay so now uh the instance has been created it says apply complete resources on added so now let me go back to my aws console let me type ac to and if i go to uh instance running i can see one instance is running so this is what we have created now and the instance is successfully created okay now that the instance is fully running um and since i have created this instance for the training purposes i'm going to delete this by using the terraform command itself in order to delete the specific instance or specific resource in aws you have to use a command called terraform destroy when you type just type terraform destroy it will delete all the resources what we have you have created but in our case we have to delete one specific instance or one specific resource henceforth you have to use hyphen target and then in the resource type which is aws underscore instance dot the instance name so remember when we have created the resource we have created the resource with the name called mine ec2 so just type with this command under click enter so this will ensure to delete the instance in the aws so it will take time for to delete the instance so after the command fully executed it will say comma i mean destroyed one resource so if you refresh your instance it will says fully destroyed so no no more instances are running so that's it friends i hope this video is made useful for you if you have any questions so please post it in the comment section if you like this video please click like button or if you have any new questions posted in the video that i can cover in the future okay friends will catch up in the next video thank you so much [Music]
Info
Channel: SecGuru
Views: 231
Rating: undefined out of 5
Keywords: Terraform to create EC2 instance, Provision EC2 on AWS, Terraform Automation, creating ec2 instance using terraform, cyber sec guru, cyber secguru, secguru, how to create ec2 instance in aws, create ec2 instance using terraform, ec2 instance creation in aws, terraform ec2 instance example, Creating EC2 instance using terraform, create instance using terraform, terraform basics, terraform tutorial, secguru channel, how to use terraform, create ec2 instance in aws using terraform
Id: twNYhcmBwOM
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Sun Sep 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.