Day #9: How to create an EC2 instance using Terraform | Creating EC2 Instance using Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel and today we are going to learn how to create an Amazon ec2 instance using terraform so without wasting any time let's begin so the prerequisites for this particular lecture are that you first need to have terraform as well as AWS CLI already installed on your machine okay so if you are working on Windows then in that case I have written a Blog wherein I have explained all the detailed steps for downloading and installing terraform as well as AWS CLI you can go and check that out I have mentioned the link in the description so do check it out soon once we are done with the installation as well as downloading downloading and installation of this particular terraform as well as AWS CLI we are now ready to go and work with our users so what you need to do is we first need to create a user and then after that create access keys for that particular user so that we can work with them for our authentication purpose okay so first of all let's go to our AWS Management console and over here I'll be searching for iam and within this I'll be selecting the users okay and over here I'll be adding the users right okay then after that I'll be going to next I'll be attaching the policies over here directly so since we are working with Amazon ec2 I will be selecting the full access for Amazon ec2 okay and then I'll be going in clicking on next okay and create user okay so now you can see that our user has been successfully created now after this what I'll be doing is I'll just open this and go to the security credentials and in here I'll be creating the access key now since I am going to work with command line interface only I'll be making use of this particular option right here and actually checking this out and next turning the description is access key for new user okay and create access key so now it will show me the access key as well as the secret access key I will simply go and download this particular CSV file okay and now we are ready to go and work with our AWS okay so first of all what I'll do is I'll just create a new folder specifically for terraform okay and just open it I'll try to open a CMD over here and then with the help of this I'll be opening the vs code so now you can see that my vs code has opened in this particular directory now what I'll do is I'll just open the new terminal okay so now we'll be configuring our AWS okay so for that configuration purpose we'll be using this particular command that is AWS configure okay now it will prompt me to enter the access key as well as the secret access keys so I'll just go to my downloads wherein I have downloaded this particular file okay and just try to copy this [Music] okay okay I think it didn't got copied properly okay now I'll be copying the secret access key as well okay now the region is that I want to work with Is Us East one and the output format I won't be entering anything as of now so now we have successfully configured our AWS CLI okay now after this we are going to create our main.tf okay that is the configuration file with a TF extension okay so now we'll write our configuration in this particular file okay so how do we do that so first of all before actually writing it I would like to give you a brief overview like we are going to create resources on AWS right so the provider over here is AWS this is what I want to tell to terraform like I want to work with this particular provider then how would I do that I would simply mention provider over here provider block and mention the provider as AWS okay so in this way we can Define our provider I'll be mentioning the region over here as Us East one only right so now I'm using simple configuration as of now I don't want to complicate it much so that's why I'm keeping it more simple and concise okay now after this what is important is we want to create an ec2 instance right within the provider AWS I want to create an ec2 instance which is the resource right so I will be mentioning a resource block simple and the type of resource I want is an instance right AWS instance is what I want to create okay simple then after that I will be mentioning the identifier for this particular resource okay so if I mention it as my instance okay that's totally fine you can Define It Anyways okay you can Define any name over here okay it is just for our convenience purpose okay so this AWS underscore instance is defined by terraform okay whereas this my instance is something which you can Define according to your wish okay now within the resource what is it that I want I will be mentioning Ami now what is this Ami let me show you So within the ec2 instance if we try to launch an instance okay you can see that there are different images out here with which we can create our ec2 instance and each image has got a different Ami ID right so we are going to mention this particular Ami ID onto our configuration Okay so I will be making use of Amazon Linux so I have selected this particular Ami ID so this is what we mentioned in our Ami okay simple now let's again go to this particular launching instance now you can see that the instance type over here is what we want to mention right it is required before we actually create an instance so basically there are different parameters like the computing power as well as the memory so these differ from these differ for different types okay so over here since we are working with T2 dot micro and why are we going to work with T2 dock micro is basically it is free right and in your organization it will depend upon your requirement okay so according to that you can work with it so I'll be mentioning the instance type over here as well since I am working with ec2 instance right so I need to mention it has T2 dot micro okay now once we are done with this I will be adding a tag section to name my instance so in order to name the instance I will be making use of this particular tag section and the name of my instance will be my ec2 instance okay so this is the name of my instance now you may get confused like is this the name of the instance or this one so basically whatever we mention under the tags goes to the name of your instance in actual and whatever I mentioned over here is basically for the terraform to identify your instance okay so that is the thing so hope this is clear by now I'll just save this like indentations also play a vital role in and in the readability of your configuration file right so for that it is required that our file is properly written so there is a feature available with terraform that is terraform format command with which we can actually increase the readability and also rearrange our whole configuration file you can see that it is properly aligned over here right so with this it happens easily right so you can write the script and then just run terraform format command it will automatically align the things for you okay so that's it for this now I will try to initialize this working directory with the help of terraform init command it will basically install all the required plugins for AWS it will initialize the back end back end in the sense where you store your terraform.tf state okay over here by default it is local only okay so now you can see that it has initialized this directory and it has also created this dot terraform as well as dot terraform Dot Lock dot HCL okay so now once we are done with initialization we are ready 2 plan but before planning also I would like to validate whatever I have written like whether the syntax is correct or not I need to verify it with the help of terraform validate command it's showing me success that means the well configuration is valid okay now I'll try to preview the changes with the help of terraform plan command and now you can see I'll just try to take it a bit up and now you can see that terraform will perform the following actions it will be creating a resource that is AWS instance okay with this particular identification and it has got this Ai and few things few parameters will be known after apply by because they will be created dynamically okay once we actually go and create it they will be automatically get applied to it okay so now we are done with terraform plan command now we can do terraform apply now we are ready to actually implement the changes on our cloud provider so now let's enter [Music] okay I'll be confirming it once again and you can see that with terraform apply command we get this terraform.dfstate file over here right so now you can see that it has completed the apply process okay so now let's go to our ec2 Management console and over here you can see that I have got let me filter it out I have got my ec2 instance created over here right and it is in a running state so in this way we can create an ec2 instance with the help of terraform now once you are done with your work and you want to terminate this particular instance then in that case you can simply go over here and run terraform destroy command Okay so with this it will destroy whatever it has been applied okay so it will delete my instance okay in the same manner so now you can see that it shows me that the destroy is completed and the resources has been resource has been destroyed so now you can see over here if I refresh this there is no my ec2 instance over here since it got terminated with the help of terraform destroy command so in this way we can create an ec2 instance with the help of terraform and also at the same time we destroyed it as well right so hope you found this video helpful please like share and subscribe to my channel if you found the videos helpful also don't forget to press that Bell icon for the latest updates so thank you so much and have a nice day
Info
Channel: Code with Gauri
Views: 3,775
Rating: undefined out of 5
Keywords: TrainWithShubham, Praveen Singampalli, Terraform, EC2
Id: WCOM5odW0VY
Channel Id: undefined
Length: 16min 46sec (1006 seconds)
Published: Wed Mar 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.