Transcript for:
Introduction to Terraform Basics

hello guys welcome to jayhawk and in today's session we are gonna talk about terraform and these are the topics which i have planned for today's session starting from what is terraform then what are terraform commands that is terraform init terraform plan terraform apply and the third we are going to create our first terraform file and the fourth one is how we are going to install terraform on our system after that we have a couple of demo the first demo is setting up virtual machine on actual google cloud platform using terraform and the second demo is setting up an ec2 instance on aws and the same using terraform this is going to be a long session so if you want to skip some of the topics then that is also fine i'll put the timestamp of each topic into the description section so just click on those timestamp to follow those individual sessions and if you are a beginner and you want to learn terraform then i would recommend to follow this complete session because this is going to be a very basic session where we will be starting everything from the scratch what is terraform terraform is used to provision and set up your infrastructure on a cloud platform the next question comes what is provisioning and setting up of your infrastructure suppose you have a google cloud platform account or aws account and that's the only thing you have so you need to set up your servers or the virtual machine so that you can deploy your application so terraform will help you to create those infrastructure using terraform file so you don't have to manually go into google cloud or aws console and manually create those virtual machines and install your software packages once you know terraform then you are going to create a multiple terraform file which will help you to set up your complete infrastructure from the xero then what is terraform file this is the example of a very simple terraform file where you will have two component one is provider and second one is resource and always remember the terraform file always have an extension of dot df so if you look carefully over here inside the file we have specified provider as google so if you specify provider as google then you are definitely going to interact with the google cloud platform and in case if you are going to mention here aws then you are going to interact with the aws cloud services the second component which i have mentioned over here is the resource so resource varies from google cloud platform to aws because if you are provisioning a virtual machine on a google cloud platform then we are going to call that resource as a google compute instance as you can see in this terraform file this terraform file is a sample uh terraform file for a google cloud platform but in case if you are working on a aws so instead of google compute instance you are going to mention ec2 instance this is just a little idea how to create your terraform file and how you are going to interact with the cloud service provider but the terraform file is not self-sufficient enough to interact with a cloud service provider you need to know some terraform commands then what are those commands are the first command is terraform in it which is going to prepare your local environment for setting up your cloud infrastructure second terraform plan it is going to tell you like how many resources you are going to add update or delete the third command is terraform apply this command is actually going to run your virtual instances on a google cloud platform or aws and the final command is terraform destroy so this command is something which is going to destroy or delete all the instances or the resources which you have created using previous terraform apply command don't worry we are going to cover all these commands in more detail into the next slides starting with our first command that is terraform init suppose you have a directory terraform demo on your local system or a desktop and inside that directory you have a file that is main.terraform and this is the code snippet where we have specified the provider and the resource don't worry we are gonna look the complete actual terraform file into the later session but this is just an example where we have specified the provider that is google and the resource that is google compute instance now after that once you run the first command that is terraform init command then it is going to contact the cloud service provider and it is going to download all the required dependency on to your local system so what does it mean so once you run this command then you will see your the same directory you will get a directory called terraform so in that directory it is going to download the required packages which is needed for interacting with your cloud service provider since this example is for google cloud platform so it has downloaded terraform provider google version 3.61 so here you can see this is the dot terraform file which which is just got created once you issue the terraform init command so if you summarize what is terraform init command is then it is just preparing your local environment by downloading all the required dependencies based on the provider which you specify since we have specified the provider as a google so it has downloaded all the required dependencies for google cloud platform and in case if i have specified over here aws then it is going to download all the required dependencies which is needed for communicating with the aws environment so this is how the terraform in it works all right the next command which we are going to look is terraform plan so again we are going to take the same example we have created a terraform directory and this is our main.terraform file where we have specified the provider as google and the resource as a google compute instance and after that we are going to run the terraform plan command once you run the terraform plan command then you will see following message at the end of your terminal and this message is going to tell how many resources you have added how many resources you have changed or updated and how many resources you have destroyed this command is not still not responsible for creating any resource on a google google cloud platform or aws platform it is just going to inform you like what is going to happen or what is going to change or what is going to destroy so here our main dot terraform if you look it again then we are trying to add a resource that is google compute instance that's why we have got a message one to add that means we are going to add one new resource to the google cloud platform ah to summarize the good terraform plant command then it is just going to inform you what is going to add what is going to update and what is going to destroy on a google cloud platform or aws platform it is not going to update or change anything on actual cloud platform it is just going to inform you that's why the name is plan moving to the next command the next command is terraform apply and i'm just going to tell you this is where everything will happen or the actual thing will happen so again we i am taking the same example i have created a directory terraform demo inside the terraform demo i have a main.terraform file and again there is a provider there is a resource for google cloud platform and i am going to run the command terraform apply and once you run the command if you are running this command for google since this example we have taken it for google cloud platform then it is going to uh start a instance that is a virtual machine on a google cloud platform and suppose if you have taken the same example or we have updated the same main.terraform file for aws then we are going to get an ec2 instance on aws platform don't worry we have a separate complete lab session for aws and google cloud platform where where we are going to create the complete terraform file and we are going to spin those virtual machine and ec2 instances on those cloud platform now you have got an idea like how the terraform command works and what are the sequence of running those terraform command now in this slide we are going to see our complete terraform file this is an actual code of our main.terraform file this code sample or this complete main.terraform file i have taken it for google cloud platform but this file structure or the components which i am going to explain is going to be pretty much same for aws instance also okay let's start with the first component so the first component is provider since we are talking about google cloud platforms that's why i have mentioned the provider name over here is google second if you are going to interact with a google cloud platform or aws then you need to have some kind of authentication so you need to create this uh json key that is your authentication key on a google cloud platform first and then you need to download that json key onto your local system and you need to pass that local that json key to this terraform file the third component which we are talking about here is the project id so if you go to google cloud platform then you need to select project over here so here i have already created a project called gcp terraform and this is the project id for it so we need to copy this project id and paste it over here inside our main.terraform file so that this terraform knows like in which project we are going to create those resources moving further the fourth component we are talking about here is the reason and the zone uh this settings is pretty much dependent on in which part of the world you are so i am currently in the europe zone so that's why i have selected the europe first reason but if you are in some other part of the world then select the nearest zone to you now we are done with our provider section the next section we are going to talk we are talking about google compute instance so the google compute instance is a resource that means we are going to provision a virtual machine on a google cloud platform that's why we have specified the resource name as a google compute in stars but in case if you are working on aws then you need to specify the name as aws underscores instance okay the second keyword over here it i have kept it as a default so this default is a user defined name actually and you can keep any name of your choice all right then there is a next component sorry i have pasted it i have clicked it but the next component over here is the name that is tag that is also user defined name so any resource which you are trying to create you can put a suitable name over there all right now moving to the sixth component that is machine type what is machine type so machine type is something like a cpu so what kind of a cpu or what kind of a powerful cpu you need since this is an example or demo that's why i have keep i have kept actually very small cpu that is a micro cpu but in case if you are looking for a production environment then just go for some bigger uh machine type that is e to small medium but still e2 small and medium r is just only for development and demo purpose just look out for documentation and pick some suitable cpu if you are looking for a production environment the next component which we are talking about is the operating system since we are starting or we are setting up a virtual machine it needs to have an operating system so what kind of operating system you want to install in it so this is the tag boot disk and initialize params here we are going to specify our debian cloud or debian 9 system so we want to have a debian operating system installed on our virtual machine the next component which we are talking about is the network interface and if you know if you are working on a cloud environment then you need to have a public ip which so that you can access your virtual machine so you need to define a network interface for that so this is the code snippet which you need to keep inside your main.terraform file and if you don't want to control the ip address then you just define that network as default and google cloud platform is going to assign a public ip address for your virtual machine in the previous slide we have seen the complete terraform file for a google cloud platform so now in this slide we are going to compare the two terraform file one for google cloud platform and on one for aws so here on the left hand side these are the code snippet from our ah google cloud terraform files so which is not going i'm not gonna explain because we have seen in the previous slide but on the right hand side we are pretty much interested in aws so as i told you we need to specify providers so here we are going to specify the provider as aws then again we are going to specify the region based on your location uh in the world second uh this access key and secret key this part is little different here in google we need to download that json file which com combines of your authentication but here we need to specify the access key along with the secret key so that you will get from aws console don't worry we have a demo so we i'm going to show you like where to copy the access key and the secret key the next we need to specify the resource so in google cloud example we have seen the resource name as google compute instance but here we need to specify that instance name as aws instance because we are going to provision this instance on aws environment then you need to give some suitable name so i have given the name as ec2 example next component inside the resource is ami so you need to find the ami id from your amazon console or aws console actually i'll show that id but to give you a hint what is ami ami is a machine id because uh in google cloud platform you need to specify operating system like a debian tan but here you need to choose the predefined images so amazon provides you a list of images one for in that list you can find one for ubuntu red hat santoyas or fedora so you can choose any of the machine id out of that list and put that id over here so that it can provision those machines for you the second is instance type here and that again denotes the cpu power so if you are looking for a demo purpose then you can choose t2 micro or something similar and if you are looking for production grid cpus then just look for the documentation and choose some very high processing power cpus okay moving further now we have sufficient knowledge about terraform and what are the terraform commands so the next task we have for us is like we need to install terraform on our local system so i have prepared this guide uh where i have put all the instructions and the commands which is needed for installing terraform so i'll put the link of this guide into the description section so you don't need to copy of any of the command just come back and check this guide and copy all the commands for installing terraform onto your local system so here on the right hand side you can see this is my guide and i'm going to click on install terraform on ubuntu so first utility which you need is you need to install the unzip utility on your system because we are going to download the installer of a terraform in a zip file so you need to have this unzip utility if you are working on a linux operating system so uh i'm assuming that you have installed the unzip utility because i have already installed on my system so i am not gonna perform that step again the second step is we need to go to terraform dot io click on the download cli and below there you should choose the installer based on your operating system so since i'm using ubuntu so i'm gonna use linux so i'm just gonna select 64 bit right click on here copy the link head back to your terminal on the left hand side and i'm gonna download this zip file using wget command paste the url and here you can see the terraform zip has been downloaded on my ubuntu system now we need to unzip the file so the command is unzip terraform and unzip has been completed and we can see the file terraform which has been extracted now we need to go back to our guide for the next instruction so now we have copied the link we have downloaded the zip file now we need to move this file inside user local bin so for that what we need to do is we need to use the command sudo move move the terraform file into the user local bin and for that you need to have the sudo permission otherwise you won't be able to move this file inside the user local bin directory so i'm just going to copy this command and paste it over here so here you can see my file has been moved and you can verify using user local pin command and here you can see the terraform has been moved to the user local pin directory and now we can verify the installation by running the command data form dash v and here you can see we have installed the terraform version 0.14.8 all right demo time so the first demo which we are going to perform we are going to set up a virtual machine on a google cloud platform using terraform uh if you are interested in aws then you can skip this demo part and directly jump to the aws demo uh but if you are interested in setting it up on google cloud platform then you can follow this session also okay so i think we have sufficient knowledge which is required to provision a virtual machine on a google cloud platform using terraform so we are gonna use all those concept which i have explained earlier and we are going to implement all those concept in this demo also i have prepared a guide where i have put all the instruction which is required for this demo so during the demo if you forget to note any of the commands or any of the bash script which i'm gonna run so you can come back and check this guide i'll put the link of this guide into the description section so you don't have to copy or make a note of any of the instruction which i'm going to perform during the demo so you just come back to this guide and copy and feel free to copy any of the commands to start the demo the step number one is we need to create a project on a google cloud platform so here you can see on the left hand side this is my google cloud platform account where we are going to create our first project but before that you need to sign up if you haven't done before so just go ahead and create a google cloud platform account and they offer you a one-year free trial where you can experiment on a google cloud platform so i am assuming that you have an active account on a google cloud platform okay so how to create a project that's our first task so here you can see on the left hand side this is my google cloud platform console and here uh in the left left side of this google cloud console you can see yeah there are some projects which i have already created but if you haven't created any of your project then just click on this new project and you should be able to create your new project but anyway that you can do it it's a very simple step but here i have already created a project with the name gcp terraform so that's going to be your first step uh which we need to perform in this demo moving to the step number second uh here you can see in the left hand side i have already selected my project that is gcp terraform the step number second is where you need to create a service account for your project and this service account we are later gonna use for authorization okay so in the left navigation menu click here and you need to go to i am an admin section and inside that i am an admin section you need to click for service accounts so here you just click on service account and here you can see i have created some service account earlier but you can create a new service account for this demo click here to create your first service account and here you can mention your service account name so i'm just gonna put demo service account and then just click on create and here on the right hand side you can follow the same instruction over here also okay so the step number third where you need to assign some roles so the first role which we are going to assign we are going to assign the project honor roll so you need to select the project so just drill down into this drop down menu and look for the project here and here you need to select the role as honor okay there is one more role that is compute admin role that you need to assign so click on add another rule and then you need to again drill down and you need to check for compute section a compute option over here and it should be somewhere yeah this is compute engine and here you need to select for compute admin role so that's the compute admin role which i'm gonna select and here you can check in the guide also this is the screenshot which i have taken from the same console okay after signing the compute admin role we need to assign one more role that is compute network admin so click on add another rule and again just go for compute option i think i just saw somewhere here here there here it is and we need to go for compute network admin just select that one also okay so that's three role which you're going to need for this demo and after that just click continue and then just click done once you're done creating your service account then you can see over here this is the service account which we have created that is demo service account and here if you look carefully there is a key id column so there is no key associated with this account so the next step is we are going to generate the keys for this account and that is going to be our step number four so here you can see on the screenshot uh we need to click on these three dots over here for the account which we have created so here the account which we have created is demo service so just click on this option and then you need to look for manage key option just follow along with this guy all the instructions are there so and after that you need to look for this add key drop down so that is over here and here we need to click on create new key so once you click on create new key then it will pop an option for json or p12 so we need to create a json file and if you go back to our slide which i have shown you i think it's over here so if you go back to our slide then here you can see we need to download a json file so this is the same json file i'm talking about okay sorry it is over here so this is the same json file which we are going to download so just click create and you just need to save this file somewhere on to your system so i'm just click ok and here you can see i have just downloaded the json file and this json file will be useful for you to authorize your terraform to communicate with your google cloud platform okay moving further now we have completed the step number four we have now downloaded the json file that will use later during the authorization process but the step number five is we need to create a main.tf that is our terraform file which i have already explained but we are just gonna create this file from the scratch so what we need to do first of all switch back to our terminal and this is where i have already installed the terraform and that you can verify using this command okay so we are running uh the terraform version 0.14.18 sorry 8. so i'm just gonna create one directory over here mkdir data form gcp demo and just switch to that directory i'm just gonna clear the screen and i'm just gonna first of all create a file that is main dot tf yep i'm gonna open this file into vi mode okay so this file is empty and we are just gonna first copy a provider code okay so just copy this code snippet i'm just gonna explain what this is all about so here you can see the provider is google although we have downloaded the key file but we haven't renamed that file to be like a json account. sorry gcp account.json so i'll do that part later secondly we need to mention the project id that is gcp terraform so if you go back to your google cloud console over here and if you click over here then this is your project that is gcp terraform and this is your google cloud project id that is gcp terraform 307 307199 so you need to copy this project id into your terraform file so that is going to be your project okay so we just only left with this gcp account.json that we need to specify or we need to create or we need to copy the json file into this my virtual machine which i am running over here okay and this is my region which can be anything uh it can be asia or u.s based on your location and the zone also okay so this is our first provider code which we are going to put inside our main terraform file so i'm just gonna save and quit moving to the next step we need to copy the code snippet for the google compute instance because that's the actual virtual machine which we are going to run and that is going to be the e2 micro machine okay so again we are going to update the main dot tf file which we have uh inserted just the provider code there so we are just now going to insert the google compute code snippet inside that main.terraform file so first of all just copy this uh code snippet and again open that file into the vi mode and here you can see we already have the provider code so i'm just gonna go into the edit mode and i'm just gonna paste the compute instance code also so here this is our google compute instance that's the keyword which one i keep and this is the name i have kept it as a default but you can keep any name of your choice that's absolutely fine and here this is going to be the tag or again the name so it is absolutely okay to keep any name of your choice but be careful about the machine type you need to specify the exact name what kind of a machine you want so i am keeping it as e2 micro i just want the lowest cpu as possible or the lowest configuration cpu as possible third thing the boot disk i am using debian debian 9 ubuntu machine and the fourth thing is we need to specify the network interface and i just created this configuration over here and i kept the network to default so i'll get so that i can get some public ip for my virtual machine okay so i think we are done with this uh main dot terraform file and that is sufficient enough to start your virtual machine inside your google cloud platform the only thing which we are left is with this json we haven't created this json file yet although we have downloaded the json file after creating the service account but we haven't copied that file into this same directory so that it can be used okay so first of all i'm just gonna quit here and save this configuration or the main.tf file i'll clear the screen and just to re-verify whether our content has been saved we i'm just gonna run the cat command and here you can see everything has been saved properly okay so i'll clear the screen and i'll do the ls command one more time and here you can see we only have the main.tf file over here we don't have the gcp account.json file that we need to create it okay so first of all once again do the cat i just want to copy the same name so i'll just copy it i'll clear the name screen i'm just gonna run the command touch and this so it's gonna create that file and now you can see we have the json account sorry gcp account.json file over here and if you want to view the content of it then it will be empty and here you can see this is completely empty so first of all i'm just going to copy the content of the key file which we have downloaded after creating the service account so that is i'm gonna do let's head back to browser and see where we have downloaded our json file after creating the service account so this is the file which we have downloaded so i'm just gonna open that file so this is the file and i'm just going to open in one of my editor so this is the content of my json file or the key file which we are going to use for authentication so i'm just going to copy the content of this file i'm just going to head back to my terminal and i'm just gonna open the json file which we have created that is gcp account.json and which is empty right now so i'm gonna open in edit mode and i'm gonna paste the content over here and after that just save and quit okay so now we have placed our gcp account.json find file just parallel to our main.terraform file so we have everything in place now so if we move further into the guide and there is a step number six where we are gonna perform these commands the first command is terraform init terraform plan and the third one is terraform apply so we are just gonna look all these commands one by one so the first command is terraform init command okay so just copy this command clear the screen over here and just run this command i'm just gonna explain what this theta form init command is doing so if you remember uh if you see our code snippet in the main.terraform file here is the provider that is google so whenever you mention a provider over here in the main.terraform file and when the first time you're gonna run the terraform init command it is going to download all the dependencies for the provider that is google so here this is the line in which it is going to download all the dependency which is required to communicate with our google cloud platform so in the terraform init command these are the prerequisite step which is done by the terraform so this is a mandatory step whenever you are going to work with a terraform module so you need to run the terraform init command so that you can download all the required dependency which you have mentioned in your provider or in your main.terraform file so here the provider name was google so if in case the provider name was aws or azure then it is going to download that dependency when you issue the terraform init command now you might be wondering there are lots of thing which are happening behind the scene when i issued the terraform init command so don't worry we are just gonna see what it has done in the background so if you run the command ls lart so you will see a directory which is created as a dot terraform so this is the directory which is get which gets created once you issue the terraform init command so in that directory you will find all the required dependencies uh which is downloaded when you issued the terraform init command so to view the content just run the command ls and then inside the directory terraform and there will be some more directory that is provider hashicorp and here you can see google and there will be version and that linux amd 64 machine and this is going to be your uh final i think file or the dependency that is terraform provider google 3.60 that's the version so as i told you whenever you issue this command so it is going to download all the dependencies which you mentioned into the main.terraform file so we have mentioned google so it has downloaded the whatever latest dependency of google provider which was available uh during the demo so it has downloaded that dependency into your local machine so here you can verify those dependencies okay moving further into the guide we have completed the terraform init command and here you can see this is the console log which i have pasted here in the guide so in case if you uh forget or if you want to verify your output then you can just cross check with it okay so as you can see we have completed the terraform init command the next command is terraform plan okay so that what is terraform plan command so terraform plan command tells you like how many resources either you are going to add either you are going to update or either you are going to destroy or delete so it is just going to tell you what will be the plan are you adding it are you deleting it or are you destroying it so that is the main intention behind the terraform plan command so i will clear the screen and i am just going to run the command teraform plan okay so the output will be little bit big on your console and it will be a little overwhelming but don't worry we are just gonna uh go through the complete output so this was the command which i have run that is terraform plan and as you know if you see our code snippet then we are trying to create a virtual machine a virtual compute instance so this is the compute instance which we are trying to create so here you can see uh it first highlighted what we are going to do so we are going to create a resource so that's the first thing it is going to mention you when you run the terraform plant command so you are going to create a resource so that's correct yeah we are going to create a resource and what kind of resource it is so that is a google compute instance so that's the resource which we are going to create and if you look carefully at the bottom of this so the plan is one to add that means you are trying to add one resource that's the new resource actually so that's the thing which you need to notice over here the second is zero to change so are you going to update or change any of our existing resource then it's zero because no we are not going to update or change any of your our existing resource the third is zero to destroy that means we are not going to delete or destroy any of our existing resource so our main intention is we just want to create our virtual machine inside our google cloud platform so that means we are going to add a resource that is virtual machine so this is the google sorry this is the terraform plan command one thing which you should remember this terraform plant command never create a resource it just tells you what it is going to do okay so here you can see we are just trying to add one resource and nothing else so this command will never going to provision or run any of our virtual machine on our google cloud platform before we move further and issue our next command that is going to be terraform apply i just wanted to show you my google cloud console so that i can show you that i am not running any of my virtual machines already so here you can see this is my google cloud platform and these are the resources that resources list is completely empty and you can also verify from here from the left navigation menu going to compute engine and going to vm instances and here you can see the list is completely empty and i am not running any of the virtual machine already so the next command is we are going to run the terraform apply command so that is going to be our next command where it is going to communicate with our google cloud platform and it is going to actually start a virtual machine on our google cloud platform okay so i'm just gonna copy this command and on the left hand side on the terminal first of all i'll clear the screen and after that i'm just gonna paste the command and it will ask enter a value so are you going to provision this virtual machine so i just gonna say yes okay so here you can see if you read the logs carefully it is first of all it says that it is creating and it took some time to create those machine and after that apply complete and one resource added so that means our virtual machine has started in our google cloud platform so just go back to our same google cloud vm instance screen and just refresh this page and here you can see this is the test instance which we have started and as you know we have mentioned the zone europe west 4 so that's our zone and the name is test so if you go back into our code snippet of our main.terraform file then you will find the test we have mentioned so that's the test name which we have mentioned okay so that's our virtual machine name uh which we have provisioned using a terraform and one more thing you can see the external ip which we have got so this is the external ip which is returned by google cloud platform and you can use this uh external ip to ssh into this virtual machine okay so ah this is how you provision a virtual machine inside google cloud platform using terraform so now we have seen terraform init terraform plan and terraform apply command so these are the most three basic command which you need to know to work with terraform and any other cloud service provider okay so we have done the verify steps also so that you can check it by yourself by going back to your google cloud console and that you can verify the next command is terraform destroy and that is going to be the last command for this demo and this is it says destroy so it is going to delete everything so here you can see our test instance is running and now we are just wanted we just wanted to destroy this instant so just copy this command from here go back to terminal clear the screen and paste the command so as the command says destroy it is going to remove and here you can see now there is zero to add yeah we are not adding it we are not updating it but yeah we are trying to destroy it so that's the command just type yes and it's gonna take some time and it's going to delete our virtual machine instance okay so here it has destroyed our resource and we can go back to google cloud console and just refresh this screen to see whether it has deleted or not and here you can see the test is instance or the virtual machine has been deleted all right so now we are done with our first demo that is provisioning a virtual machine on a google cloud platform using terraform and now we have pretty much seen the terraform any terraform plan terraform apply and terraform destroy commands moving to the second part of the demo where we are going to provision a ec2 instance on aws console so again i have prepared this guide for aws also and you can find this link of this guide into the description section of this video to start the demo first of all the main requirement for this is you need to set up your aws account that is going to be our first step so i would recommend you to uh create an account with aws and aws offers you a 12 months free trial or 750 hours per month limit free tire which you can use for this kind of a demo but you need to provide some credit card information so that's absolutely okay that will charge you one dollar for authorization but after that it won't charge you for anything so i'm assuming that you have an aws account and you have already signed up for that moving further after you have signed up for aws you need to log in and you need to log in as a root user so that is going to be your second step so i'm just going back to my aws account and here i need to input my email id and password so i'll be back after entering my username and password and also remember you need to login as a root user once you login into your aws console then this is the dashboard which you're gonna see okay moving along the guide the next step is where we need to generate the access keys so as i told you like whenever we are working with the aws so you need to have your access key as well as the secret access key so that terraform can authorize itself to communicate with aws so for that what you need to do is you need to go on my security credentials and that you can do it by clicking on your username over here going to my security credentials and then go for access keys over here and here you can see i have already generated my access key but if you are doing it for the first time then you need to click on this create new access key and once you click on it so it is going to show you the access key id as well as the secret key here i have hidden it because that's the some confidential information which i don't want to share but you get the idea like you need to generate this access key and the secret access key here but always remember this secret access key will be visible only once for security regions by aws and once you close this then you won't be able to use or view the secret access key actually so you need to copy both access key as well as the secret key so i am assuming that you have copied both the information and after that you can just click on close and here you can see as i have already created one on 18th of march but here i can see there is one more exercise key which has been generated on 23rd of march the day when i am recording this video okay and both are in active state so that's the thing which you need to notice over here so moving back to the guide same instructions i have mentioned over here with the instructions and the screenshot and here you can see uh you need to copy the access key id and the secret access key the step number third is where we need to create a main dot terraform file as similar to the previous demo where we have seen for google cloud platform also where we have created the main.tf file so this is on the left hand side you can see my terminal and i'm just gonna create a directory aws demo i'm just gonna switch to directory aws demo i'll clear the screen i'm just gonna create the file man.tf and i'm just gonna open in the vi mode and this file is empty so we don't have anything over here okay so the step number one over here is we need to mention our provider details in adapt for aws so the syntax for that is resource provider type and the name name is option uh not optional but it's a user defined or you can skip it also that's also fine okay so here is the actual ah example where i have mentioned like what is provider what is aws and region access key and secret key here you can see this is xx because i don't want to put my actual secret key or access key over here i'll copy this code snippet from here and i'm just gonna paste it inside my main.terraform file which i have just created and i'm just gonna replace this access key because i don't know with the actual values which i have created inside my aws console okay so now i have copied my access key and secret key as i told you i cannot show those secret key and access key because of security reason but after copying and pasting your access again security key you just save and quit it okay so after that moving further into the step number second where we need to create our aws instance and for that we need to find the amazon machine image and that you can find by using aws console so go back to your aws console click on services go to ec2 and in the left navigation you should see an option amis click on emis here in the drop down menu is drop down option uh always remember to select public images and here you need to type the option ubuntu because i'm just gonna use the ubuntu as my operating system for virtual machine click enter and here you can see this is the ami which we are looking for so once you click on this uh line number then you can just copy this ami id from here copy to i'm just gonna copy it okay and then copy this value and store it somewhere because we are gonna need it later okay moving back to the guide uh all the instructions i have mentioned for how to find the amis over here also the next instruction is we need to mention the aws instance so that's the next code snippet which we are gonna put it over here so copy this code snippet where we are going to create our aws instance and this is the name that is we are keeping easy to example so it is a user defined name you can keep it uh any name of your choice secondly we have copied the ami id so you need to copy and paste that ami id over here and third thing you need to mention the instance time since it is a demo purpose so i am keeping it as a t2.micro but you can increase this instance type based on your requirement and choose some higher configuration also and this tag name is completely up to you what kind of a tag name you're going to put so i'm just going to put terraform ec2 okay so that's the code snippet which i'm going to copy from here and paste it inside my main.tf but i'm also going to update the emi also okay so copy it and go to your terminal on the left hand side and yeah open in vi mode go into edit mode and paste the content over here so the thing which we need to replace it over here is the ami id so i'm just gonna remove the existing one and i'm just gonna paste the new one which we have copied from the aws console so i'm just gonna paste it over here so now uh save and quit okay and you can we re-verify whether you have saved correctly or not by using the cat command and here you can see every thing the provider name and the aws instance all right moving to the next step uh now we are ready with our terraform file and now we can run our terraform command so the first command which we are going to run is the terraform init command so what does the terraform init command do so first of all just copy this command and i'm gonna explain like what it actually does so just run this command first of all okay here you can see our command has run successfully so the terraform init command is responsible for downloading all the dependency which you mentioned for the provider so the provider over here is aws so if you'll see our code snippet so that's the provider we have mentioned that is aws so whenever you are going to run a terraform init command so it is going to first of all look which is your provider so that is your aws and it is going to find the latest version uh whatever available so at that point of recording this video the latest version what 3.33 and it is going to download all those dependencies uh on to your local system okay and you can verify this dependency by running the command ls lar t and you should have a dot terraform file uh folder which is created after running this terraform init command okay and as you remember we have only created the main dot terraform file okay and these files has been created after we run the terraform init command okay so and you can also view the content here also by running the ls command and here you can see so these are the content of our terraform dot terraform directory and here you can see there is a image linux amd64 which means that's the virtual machine or ec2 instance image which is our terraform has just downloaded by uh using this provider information which we have mentioned so if you go back to our guide so this is the provider information that is aws once we mention this thing then terraform init command going to download all the required dependencies for aws all right so now we have run the terraform init command the next command come is we need to run the terraform plan command so i'll clear the screen over here and i'm just gonna run first of all the command tera form plan sorry i just did the type over here i'll just clear the screen once more and i'm just run terraform plan come on all right so the output of this command is little bit big but don't worry we are just gonna go through the output and try to understand it okay so what the terraform plan command does it it just tells you that what it is going to do once you actually run the terraform apply command so terraform apply is the next step but there is a final command terraform apply which is going to actually run your or install or provision your ec2 instance on aws but we are still one step behind and we are focusing on terraform plan command so terraform plan command tells you like what is going to happen in future and the future is the next step so it is going to create one resource so that's why there is a green plus sign over here and with the option create so that means once you run the terraform apply command then it will be going to create and resource okay so if you look carefully there are three ah options over here in the plan that is one to add that means one resource will be added and that's correct we just want to spin our easy to install so we are trying to add a resource in our aws environment that's why it shows one to add we are not updating anything that's why it's zero and we are not deleting anything so that's also zero so terraform plan command tells you like what is going to happen once you run the terraform apply command so it will just sketch you the plan like how many resources are going to be added removed or updated all right so now we have seen the terraform plan command so the next command is the terraform apply command so this is the command which is actually going to start our ec2 instance by contacting the aws server and it is going to start our ec2 instance all right so this is the command terraform applies which i'm just going to copy from here and paste it on my terminal and hit enter it will ask like do you want to perform this action so just type yes and hit enter okay so our terraform apply command has finished successfully and here you can see it has added one resource and the messages apply complete so now we can go to aws console and verify whether our instance has been created or not so go back to your aws console go to services go to ec2 and here you can see instances running that is one so which we have started recently just click on it and here you can see the status is running the name is terraform ec2 and which you can verify by using our main.tf file and here you can see the name is terraform ec2 the same name which we have given over here so you can see over here also and it's running and yeah this is the uh our ec2 instance which we have just provisioned using terraform and you can verify the zone also and this is our public ip address so that you can use it to access it and yeah so this is how we provision our ec2 instance using terraform all right so the next command which i would like to tell is to destroy so now we have created this instance so how we can destroy it so the command is pretty much similar the only thing which we are going to change is the last keyword that is destroy and hit enter and it will again ask for confirmation so just say yes and hit enter okay so the destroy has been completed and it has destroyed or deleted our one resource so on the right hand side we can just refresh this console and see whether it has been deleted or not and as you can see the instance is gone so we have no longer any ec2 instance running over here okay so you can remove this filter also so you can see uh we have previously terminated two instruments but none of us none of any of the instances running right now all right so with that that was our last command that is terraform destroy and that's the demo i just wanted to show you for aws part also i hope you liked today's getting started with the terraform session and also the demo on google cloud platform as well as on aws and if you have any question related to the session then please put down into the comment section and also if you have any suggestion for similar lab session which i which you would like to have it on my channel then also please put down into the comment section then i'll try to get back to you on those sessions as soon as possible and please do follow my channel on youtube and you can find me with the name rahul vag or jay hook and where i keep posting the sessions on devops on kubernetes helm chart cube spray and terraform and also you can find me on a web with the url jhook.com where you will find all the guide which i use generally use into the demonstration so just feel free to uh browse those guides and copy those commands from there if you feel useful so uh till then thank you for your time and see you into the next session bye