How to create Amazon EC2 instance using Terraform | Launch multiple ec2 instance with terraform code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we are going to launch an ec2 instance in our aws account using terraform so terraform is absolutely amazing and i will show you why i love it so before terraform we used to create our aws ec2 instance using the console and we used to go through all the steps that is choosing ami instant type configuring instance adding storage adding tags configure security groups and all the steps which was very time consuming i've explained all of them in this but now in this video we're going to see how we can launch an aws instance using terraform you will absolutely love this video before going ahead there are few things that you need to have to go ahead the first is going to be you should have a code editor i am using visual studio code the second is you should have a terraform installed in your machine to check this you can run this command terraform version so i've already installed terraform in my machine if you want to install you can go check it out this video where i explain how you can install data form the third is you need to set up your secret keys and access keys i have also explained that in this video and you can go check it out using this command as well so it was configured you need to put your access key here your secret access key here your region name and your default output so we are all set up now and we are good to go to launch our first instance using terraform so i'm going to clear this so to work in terraform the first thing you need is going to be provider so i'm going to create a file and i'm going to name it provider provider.tf when working with terraform your extension should always be dot tf so provider.tf and to explain your provider i'm going to create my instance in aws right so the provider is aws if i'm creating resources in google cloud the provider is google cloud and there are a lot of providers in terraform aws azure gcp kubernetes a lot of providers there are extensive list of nine one nine seven five providers you name it you get it you can get help null random anything you want so there are a lot of providers we are going to use aws because we are going to create instance in aws so i'm going to click on this i'm going to click on the documentation so here is the aws provider so if you're using terraform with version 0.13 and later you can use this or if you're using less you can use this as well but we're going to scroll down and we're going to use this one i'm going to paste this here and it says the provider is aws region is us best too and the access key is you have to enter your access key or secret access key so this is another way to enter your access and secret access key but i do not recommend it neither does a terraform terraform says that the hard coded credentials are not recommended in the entire form configuration so you should not do this rather you can go ahead and use the aws configure command and set up your secret access keys and access keys there right so i'm going to cut out this block i'm going to remove this perfect so now i have my provider set up and how is terraform going to know that this is the provider so we have to run this command first and whenever you work on terraform the first command that you need to run is going to be terraform init so now terraform is going to initialize the back end it is going to get the information about the provider it is finding the latest version of hashicorp aws provider and it is installing so if you see here you'll see a new folder terraform which has the details about the providers and it is encoded in binary and you also have a terraform log so now we have set up our aws provider right so to check if your code or if your syntax is valid or not the another command is going to be terraform validate says that the configuration is valid right so we have our provider setup let's create our ec2 instance so i'm going to create a new file and i'm going to name it so you have to name all the configuration files in dot tf extension only so to create an aws instance let's go to the documentation so this is going to be documentation for aws instance so you can search all the resources here in aws and you get it everything that you want easy to fleet easy to host tags traffic mirror anything you want right so we're going to use this as an example so it has data resource that we are not going to use because it is going to complicate things and let's copy this block here all right and i'm going to paste it here so it says resource aws instance and the name of the resource the ami is data.aws ami we don't do we don't have any data block so we're going to cut this i'll show you how you can insert your ami id the instance type is going to be t3 micro and the tag says hello world so first i'll show that i don't have any instance named as hello world in my aws u.s west 2 region so i'm here in my us west 2 oregon region as you can see i don't have any ec2 instance so i'm not going to trick you so to get the ami id you have to first click on launch instance and when you launch your instance every instance every ami has an ami id right here so i'm going to copy it and you can copy any mi you want if you want to launch an ubuntu using terraform you can search for ubuntu press enter and you get the ami id here right but i'm going to switch for amazon linux and this is amazon linux i'm going to copy this and paste it here in my ec2.ts ami section right so i have i have my ami i have my instance type so this should be okay to see what is terraform going to create according to the configuration there is a command name as terraform plan which shows what resources are going to be created and it has also uh put some files to reform state so yeah it says that when the resource is going to be added and if you scroll up and if you can see the details the instance type is t3 micro the name is going to be hello world right so we know that this is what is going to be created and so now we have the plan so to get this plan running you have to use this command that is terraform apply terraform apply and click here so again you'll get the plan on what is going to be created according to the configuration file so you can review if you want and if you're okay type yes so it will start creating the aws instance so if you can see aws instance is creating and let me just go back here so if you can see yeah it is initializing it is creating 10 seconds has passed and your instance is created in just 15 seconds right isn't that amazing if you go through that traditional way of launching an instance using console it would have taken a lot of time so if you see here it is initializing it is and we have the instance that is hello world it is a t3 micro instance so that's how you create your aws instance with terraform and let's just try more things let's say you want five instances at once if you're creating it with the console you will go ahead and launch your instance five times right but with terraform it is like magic you just have to give the argument that is count is equal to five so you don't have to delete the existing one you just need to run the terraform apply again or first you can check out terraform plan to see what exactly is going to be created so if i run reform plan i should see four resources to be created because i already have one right so yeah four resources are going to be added so if i run terraform apply and if you see there is only one instances right now here it is refreshing the state it is checking what is there in the console and it says four is going to be added if you are okay type yes so i'm typing yes and it will start creating the four instances so one two three four it is creating 10 seconds passed right so it has successfully created and if i refresh this i can see five instances of t3 micro in my console using terraform isn't that amazing right so if you want to make any changes you can just type it here in your configuration files let's say if i want all the uh five instances to be too small so i don't need to go ahead and destroy this or i don't need to delete this first i just need to apply and terraform is going to check what is the changes made and according to the changes it will see and do the things in the console right so it says 5 5 has to be changed twice type yes and it is going to start changing it it is starting modifying if any new resources is created it is going to be in green color plus symbol and if anything is going to be changed it is going to be in yellow color till day sign and if it if it is going to be destroyed it is going to be in red color minus sign but let's just see it is now t2 micro and it has changed t2 small for the first and the second instance so it has changed for the three instances now so using terraform you can pretty much do anything that you can do with console you can launch instances you can create vpcs you can do security groups and we have not used security groups in this code here so if you don't use any security group the default one is going to be attached so if i see here the security up group attached to my instances is default so in the next video i will teach you how you can attach your security groups with your ports and protocols whichever you want but if you don't attach it the default one is going to be attached to the instances so it's still modifying and it has modified for almost everyone except this so it's in pending state now so this is how you can launch your instance and it is not only restricted to instance you can launch your rds databases you can launch your s3 packets or you can do anything you want in the upcoming videos we'll see how we can create easy tools we can see how we can create vpcs uh your s3 packets rds instances and a lot more if you want me to make any more videos you can let me know in the comment sections so if you see here we have successfully five instances that have been changed from t three micro to t2 small if you are done with this if you want to delete all the resources at once you don't have to go here and click on terminate for each of them you have a command that is going to be terraform destroy right it will check the state and it will see what are the resources that are present here and if you are okay with it you can go ahead and type yes and if you don't want you to be prompted again and again what you can do is you can just write terraform of destroy hyphen auto approve and this can work with terraform apply as well you can use therefore apply auto approve and it is not going to ask you for confirmation it will directly go ahead and destroy or apply your changes so this is how you create or destroy your instances or any resources in your terraform for more videos about cloud and devops please subscribe to this channel if you understood what is taught in this video please like to this video subscribe to this channel and i'm going to create a playlist about the rafa where i teach everything about the reform like variables outputs meant arguments and things everything as you can see the firm has successfully destroyed so isn't terraform amazing you can create or destroy instances in few seconds using your configuration files please subscribe to this channel and have a good day
Info
Channel: Cloud Champ
Views: 13,759
Rating: undefined out of 5
Keywords: terraform tutorial for beginners, terraform ec2 instance example, terraform aws tutorial, ec2 terraform module, aws ec2 terraform example, terraform code to create ec2 instance, launch aws ec2 using terraform, terraform commands, terraform ec2, ec2 terraform, terraform aws ec2 instance, terraform tool, how to use visual studio code for terraform, creating ec2 instance using terraform, terraform aws code, terraform instance creation, launch aws instance using terraform, aws ec2
Id: E0j40hLwjhI
Channel Id: undefined
Length: 11min 16sec (676 seconds)
Published: Tue Apr 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.