 Hi, so let's start the section by talking a bit about Terraform. Terraform is basically an open source software that is generally used to create architecture or should I say infrastructure. It's widely used across most of the cloud providers these days and it has become a sort of a standard for creating infrastructure in cloud. So you can see Terraform that is used widely across most of the major cloud providers that include Azure, GCP and AWS. So what makes Terraform special is that it is absolutely simple to use. You can probably master it within a week if you have the right resources. The Terraform basically consists of an input which is basically a configuration file and that configuration file when it is deployed or in Terraform jargon when it is applied, it creates infrastructure within that particular cloud provider. So in this particular course, I'm going to give you a lot of examples, a lot of assignments and a lot of quizzes for you to absolutely understand and to get into details about Terraform. Now once you've completed this course, I am guaranteeing you that you have absolutely no problem in figuring out anything about Terraform. So I hope you have a nice time and I hope you enjoy this particular course and please do not forget to give me a rating for this course as well as I have put in a lot of effort in making most of these slides. So thank you again and I hope you have a good time. Okay, so now that you know that Terraform is used to create infrastructure in multiple cloud platforms like Azure, GCP and AWS, let's talk about a few advantages of using Terraform. The most important advantage I feel is that Terraform is a very simple tool to learn. You could probably master it within a few weeks, if not less. The second advantage of using Terraform I feel is the HCL that is the HashiCop configuration language. So this is a custom language that they use to build their code. So HCL is a very simple language to learn and it's much simpler than using JSON. Of course, you have the option of writing your Terraform script in JSON, but HCL is a much better and a more preferred language and all the examples that I would teach you would be in HCL. So look out for the HCL chapter and you will realize how easy and simple it is to use HCL. The third advantage of using Terraform is that it has providers for all the cloud platforms. So basically HashiCop works with most of the major cloud providers including GCP, AWS and Azure and these providers make it possible for Terraform to communicate with these cloud vendors to create architecture. So these cloud providers have a close collaboration with HashiCop to create the Terraform providers for their respective cloud platforms. It is arguably the best tool for creating cloud-based infrastructure and it is also agentless. So what I mean by agentless is let's say you're creating a virtual machine using Terraform. Now Terraform will not have its piece of code within those virtual machines. So Terraform doesn't work like how Puppet or Chef works where those platforms do have agents within each of those virtual machines. So Terraform is basically an agentless tool. Okay, since Terraform does not have any agents running within virtual machines created by them, it is not recommended to use Terraform as a configuration tool. A configuration tool is a tool basically which is used to insert, update software or programs or applications within virtual machines but it does combine with other tools like Ansible to do the same. So you can always consider Terraform to be a tool that is to be used specifically for creating infrastructure and it's not advisable to use Terraform as a tool for configuring software or applications within virtual machines. So that is one of the disadvantages of using Terraform. So if you have used any of the cloud vendors, you know that these cloud vendors do provide native infrastructure as a core tool. For example, if you're using Azure, Azure provides a tool called Azure Resource Manager which has templates which is also written in JSON and if you're using AWS, AWS has its tool called CloudFormation and GCP has its own CloudFormation and most of them are either written in JSON or YAML. Now what makes Terraform special is that if you learn Terraform then you have the potential of creating infrastructure in any of these tools without having to relearn anything extra. So all that you need to do is you need to just change the provider name and the resource configuration. So using those two changes you can create infrastructure in any of these cloud vendors. So it's just one language that gives you the capability of creating infrastructure in any of these cloud vendors. If you are a DevOps engineer then it's imperative that you learn a tool like Terraform which will give you a more holistic view on how to create infrastructure unlike if you're creating it using the native infrastructure as a core tool. Another very important factor about using a code like Terraform is the simplicity. So Terraform is specifically built for creating infrastructure whereas a tool like deployment manager or even cloud formation is very complex and if you go through the code of cloud formation or deployment manager you will understand the complexity and the sheer number of lines that it takes you to create architecture whereas if you look at a tool like Terraform since it's specifically built for creating infrastructure it is more well thought out and it's a much simpler tool to learn. If you look to your left it is a code to create a virtual machine in Azure and if you look to your right I have just copied a snippet of code to create a virtual machine in Azure resource management template tool and it takes a much longer time to create infrastructure using the ER rather than using a tool like Terraform. Now let's talk about a very important concept called item portents. Now if you've ever used AWS you will know that you can actually use your CLI or your REST API to create resources. So why would you want to use a tool like Terraform? Well the answer is item portents. So if you ever run your CLI more than once by accident there is a high possibility that your resources would get created again and again whereas if you're using Terraform if you run your code once and if you accidentally run it again and again it will not create any resource but it'll just skip that particular run. So let's look at a very common example of how this works. So here I have a very common Terraform configuration so all that it does is it just creates an VPC. So to run your Terraform you need to run a command called TerraformApply. I will explain to you this command in a later lecture so all that you need to know is that when I run this command this particular VPC will get created. So let's see what happens if I create or if I do a TerraformApply. Okay so it has created that resource as you can see that one resource was added. So let's try to run the same script again. So let's suppose that by mistake the script got executed again. So you can see the next time it did not add or it did not change any resource. So that is because the entire state of this particular Terraform configuration is preserved and unless you make some changes to this particular script it will not do anything. Okay so now what I'll do is I'll use the AWS command to create the same resource. So here I am creating a VPC of this particular side of block. So let's see what happens. So it created a VPC with this particular ID that is VPC0880 etc etc. So what would happen if I run the script again? So if I run the script again you can see another VPC got created. So you can see how detrimental it would be if you use this particular piece of code in your script and that particular script ran again by accident. So that's the reason why you would want to use a script like Terraform where you know the exact state of the resource and you don't have to worry if that particular code ran again and again. So I hope this was a useful lecture and I hope you understood why it's preferable to use a tool like Terraform rather than use your CLI or SDK to create resources. So I'll see you in the next lecture. Now you can install Terraform in Linux in Windows as well as in OS X. So there is a link on how you can install the link I will send in the description below. So you can just go through this particular link and get your Terraform installed based on whatever operating system you have. So let's talk about the root module. The root module is basically the folder that contains all your configuration and all your variables and outputs. Now the most important file in your root module is called as the main.tf. So let's create a main.tf file. So here I'm using my visual studio code. So the first thing I'll do is I'll create the main.tf. So the main.tf contains all the configuration that you need to create the resource. So let's create a very basic main.tf file. So here I have a very basic main.tf file. So this contains basically the Terraform provider. So the Terraform provider is something that I'll explain to you in the Terraform block. And then there is a very simple resource block. So this resource block we had seen in our previous lecture as well. So like I said previously, this particular resource block creates our resources. Now apart from the main.tf, the other very important file is basically the variable.tf file. So the variable.tf file contains information about the variable. So this variable.tf file is something that I'll explain to you in the variable block. And there's also something called as the output block. Now again, this will be taught to you in the variable block that is the upcoming lecture. So the most important files within your root module would be the main.tf that contains all the configuration and the variable.tf and the output.tf. So these two files, I'll explain to you in the upcoming lecture where I'll talk about variables. So the top, so that particular lecture is called as a variable block. So in the coming lecture, I'll explain to you how you can run this main.tf file and all the commands that are needed to run this particular configuration. So I'll see you there. Okay, so in our previous chapter, we had created a main.tf file that consists of one particular resource block, and this particular resource block should create a VPC network. So how do we run this particular configuration? So the first thing we need to do, the first command we need to do is run the terraform init command. So what that does is that it will download all the provider related information that we have in our terraform provider block. So again, like I said, this particular block is something I'll explain to you in the next lecture. But suffice to say that it will download all the provider related information that is required to run this configuration. So let's run the terraform init command. Before I run that, let me show you how the folder structure looks like. Currently there is just the main, the output and the variable file. So once I run the terraform init, it'll create a hidden directory. And within that would be all the provider related information. So let's run the terraform init command. So you can see that it has created a terraform folder dot terraform folder. And this has all the provider related information. So now that you've run the terraform init command, the next command that you'll run is called as the terraform plan command. So what the terraform plan command does is that it will tell that particular user that, okay, these are the resources that will be created. It will not create those resources, but it will tell you in advance that these are the resources that will be created. So if you look at the output of the terraform plan, and it will give you all the information about that particular resource, it will give you only the information that can be calculated before it's before it's created. So for example, it can tell you the cider block, but it cannot tell you things like the ID, because the ID value is only generated after you've deployed or created that particular resource. So once you've done your terraform plan, and you know, you know that, okay, these are the resources that will be created. The third and the most important command is basically the terraform plan command. I'm sorry, the terraform apply command. So the terraform apply command will create those resources that you have in your configuration. So here you just say yes. So you can see a particular VPC has been created and this is the ID of your particular VPC. You can always go back and check in your AWS console whether this particular VPC has been created. So okay, so I'll see you in the next chapter where we'll discuss how I was able to connect my terraform with AWS. So in this particular example, I had already done it just to showcase all the three commands, but in the next chapter, I'll show you how that particular step was executed. So I'll see you there. Okay, so now let's talk about ways to connect terraform with AWS. There are the following ways in which you can do it. You can use static credentials, you can use environmental variables, or you can use AWS configure. So that's basically a AWS CLI command that we need to use. And for all these three, you need to use IAM credentials. So that's the thing that I'll be explaining in the next chapter. You can also use instance profile, but to use instance profile, you need an EC2 machine. And apart from that, you can also use rules to connect your AWS to terraform. But that's something that's a little more complex and I wouldn't be explaining currently. So I'll see you in the next chapter. Okay, the first thing we need to do is we need to create credentials. So let's go to our IAM. Here I'll create a new user. I will click on add users. And I'll just call this as terraform user. And I just need to give this person access key and secret key access that is programmatic access. I do not need to give him access to the console. I'll click on next. Now here I need to give him access to all the resources. Now for the sake of simplicity, I'll just give him access to the administrator. So what I'll do is I'll click on the existing policies and I'll just choose the administrator access. Now if you're running it in production, make sure just to give that particular person who is going to use this just enough access so that he's able to create the resources that he needs, you shouldn't be giving him more access than required. Now just click on tags. You can just skip this review and click on create. Okay, once you created this particular user, you can either get the access key or secret key like this, you can just click on show and get it, or you can just download this particular file. So what I'll do is I'll just download this particular file. So once you've opened the Excel sheet, you'll get a table that looks something like this. Here what you need is basically the access key ID and the secret access key. And these two values we will be using to connect our AWS to Terraform. So let's do that. So the first way you can do it is to use static credentials. So what you need to do within your provider block, you need to add the access key and the secret key that you got from the Excel sheet. So you can just paste it here. And then you can just run any of the Terraform commands. Now to check whether your connection works, you can just do a Terraform apply. So you can see that the Terraform apply did not throw any error. And it was able to connect to AWS. So this is how you do it using your static credentials. So now let's use our environmental variables to do the same. So to use environmental variables, I can just remove these two. And here what I need to do is I need to set this particular environmental variable to the secret key that I had just received. And apart from that, you also need to use the AWS access key ID. Now these two keys I will provide in the resource section below. So you can always check that. And this particular key should be referring to the access key that we had caught in the Excel sheet. So if you just set these two environmental variables used, and then let's do a Terraform apply. You can see this works as well. So this is how you can use your environmental variables and static credentials to connect AWS with Terraform. I'll see you in the next lecture where we'll use AWS configure to do the same. Another easy way in which you can connect Terraform with AWS is using the AWS configure command. So let's use the AWS configure command. So here I run my AWS configure command. And here I need to enter my access key that I had caught in my previous lesson. And here I need to enter the secret access key. And here if you wish, you can enter the region and the output format as well. And that's about it. Now to make sure that you're configured using these particular credentials, another important command that you can run is called as the STS get caller identity. So if you run this command, it will tell you which particular user is being configured. So you can see that my particular CLI is currently configured using the Terraform user. So that is correct. So this is a very important command that will come in handy. And I'll paste this command in the description section below. So once you're done with your AWS configure, so let's create this particular resource. Again, let's do a Terraform apply. I had removed the previous VPC. So this should create a new one. I just say yes. So you can see that my particular resource has been created. Now to verify whether your particular command had run just through the AWS configure, you can just check your provider. So you can see that my provider is empty. So it was not because of the static credentials. You can also use this particular command to see if there are any environmental variables set. So let me run this command. So currently you can see that there was no environmental variable set. So because there was no environmental variable set, and because there were no static credentials, so we can be, we can be sure of the fact that our resource got created because of the AWS configure. So that's it for this particular lecture. I will paste all the commands in the description below. I will see you in the next chapter. See you in this particular chapter. I'll explain to you how you can use an EC2 machine to connect your Terraform with your AWS resources. So let's start. So here I have created a, so here I have created an EC2 machine. So I log into this particular EC2 machine. To do that you can click on connect and you can click on connect here. So once you've logged into your machine, you can install Terraform. Now to install Terraform, you can go to your Terraform page, install page and underneath that you can click on Linux and within Linux there's a tab called Amazon Linux. You can just click on this and all you need to do is you just need to copy these three commands. I will send a link of this particular page in the description below. So once you've done this, you can go back to your console and just do a Terraform. So you can see that my Terraform is installed. So the next thing I'll do is I will just use the same main.tf page that I had previously used to create a network and I'll just copy this particular piece of code and I'll try to run this. So let's do that. Okay. Just to make this video short, I've created a folder called Terraform and within that I have created a main.tf file. It's basically the same piece of code. So let me try to run this particular piece of code. Of course you need to do the Terraform in it. So I've already done that. So let's try to do a Terraform apply. So you get an error stating that you have no credentials. So let's try to connect instance profile to this particular EC2net. So to do that, let's go back to our main page. So this is our instance. So I'll just click on this particular instance. I'll click on action. I'll go to security. And here I need to click on modify IAM role. So here what I'll do is I'll create a new instance profile to do that. Just click on create a new IAM role. You can click on create role. Now here the important thing is that the trusted identity has to be an EC2 machine. If you do not do this, it will not work. So just click on EC2 machine. Just click on next permissions. Here I'll just choose administrator access. I'll click on this. I'll click on next. Next review. I'll just type Terraform admin. I'll click on create role. Let's search for that particular role. So this is our particular role. And you can see that an instance profile AR and got created here. So that's exactly what we need. So we go back to our main EC2 page. And here I'll just refresh this. And if you click on the drop down, you should see the Terraform admin. So I'll just click on the Terraform admin and click on save. So that's about it. Let's go back to our main page. So let's try to do a Terraform apply now. Yeah, now it works. Now it has permissions to create and it doesn't throw that error that we saw previously. I'll do a yes. And you can see that I was able to create a VPC. So this is how you can create a VPC in your EC2 machine and connect it to Terraform. So I'll see you in the next lecture. Another extremely important command in Terraform is called as the Terraform destroy command. So let's say you have done a Terraform apply and created a set of resources. Now, if you want to delete those same certain set of resources, then you can do Terraform destroy to do that. So let's do that. So here I have created one VPC and I want to destroy it. So to do that, I will just run the Terraform destroy command. Here at last case, if you are certain that you really want to destroy that resource, so you can just type yes. And you can see that one resource that is this particular VPC has been destroyed. Just remember that whenever you want to practice using Terraform, always make sure that at the end you just destroy all the resources that you have created. Else you might incur some costs. So I'll see you in the next chapter. Now let's talk about the Hashicob configuration language, also known as HCL. Now HCL consists of arguments and it consists of blocks. Now, of course, we can write our configuration in JSON as well, but I highly recommend that you write it in HCL instead because HCL is much simpler to understand and it's much simpler to learn as well. So let's talk about arguments. So arguments can consist of an identifier and an expression. So here in this particular example, the identifier is image underscore ID and the expressions can be of the following types. It can be a string, a number, a Boolean, or it can be of a complex type. That is a list, a tuple, or a map, or an object. Now these primitive types and complex types are something that I will teach you when I am teaching you about variables. Now let's talk about the block. A block is of the following syntax. It consists of a block type and a list of block labels. Now based on what kind of block type it is, there will be a corresponding set of block labels and then it is followed by a curly braces and within that are a set of arguments. Now these are the same arguments that we talked about in our previous slide. Now let's look at an example of a few block types. Now the most important block type is the Terraform block type and this particular block type has zero labels. Another block type is the resource block type and this resource block type has two labels. So here in this example, the block label is the Azure RM resource group and the next block label is example and then comes the variable block type and this variable block type has got one label called as availability zone names. Now here is a very common example of a Terraform configuration file. It consists of a provider block followed by a provider configuration block and after that comes the variable block, the resource block, the data block and the output block. Each block I will teach you in the subsequent lectures. So suffice to say that your entire configuration file is basically a set of blocks. Now this is something that you have to keep remembering that configuration file in Terraform is basically a set of blocks. So let me reiterate again what I have said. HCL consists of arguments and it consists of blocks. Now your argument consists of an identifier and an expression and together both of them form the argument and then a block and then comes the most important thing, the block. So the block consists of a block type and then a set of block labels based on what kind of block type it is and within the block are a set of arguments and each configuration file in Terraform is basically a set of blocks. So that's about it. Now this is a very important chapter and I hope you go through it a few more times and it's a very short chapter but it's very important to remember all the points that I have reiterated in this particular chapter. So as you begin your journey in Terraform there will always be the complications with regards to correct syntax. Now there is a command called Terraform fmt that makes sure that your syntax is proper. So you can always use this command especially if you are worried about the syntax. So let's look at this particular block. So this is a Terraform block and I have my curly braces in the next line which is an incorrect syntax. So let's run the Terraform fmt and let's see what happens. So it says that an argument or a block definition is required to set an argument use the equal to to introduce the argument. So what it says is that it considers this as an argument and not as a block that is because your curly braces is in the next line. So what I will do is I will just change it to the same line. So now let's run the Terraform in it again. Terraform fmt sorry again. So now you can see that there was no error. Another important factor is basically the differentiation between a single quote and a double quote. So a single quote is not allowed here. So let's just change this to a single quote and let's see what happens. So I'll run the Terraform fmt again. So it says that it's an invalid expression. So again to make it work you have to change this to double quotes. I'll run the Terraform fmt again. And this time it works and it validates. So the next chapter is going to be an assignment in which I have a few questions with regards to the validity of the syntax. So you can use the Terraform fmt command to check whether that particular syntax is valid or not. So meanwhile I'll see you in the next section. So I'll see you there. So let's talk about the variable the local and the output block. So here I have for the sake of simplicity considered my main dot tf to be a function and this particular function can be considered a function in any language. So if this particular function takes in an argument then that argument that it takes in is basically the variable block. So here for example I have created a variable a and that has a value of test. Now let's assume that you want to create a variable that scope is only limited to your main dot tf. Then you would create a local block. So for example here I've created a local block called a that has a value of 100. Now let's assume that you want to return your function to another module for example then you can use an output block. So for example here I've created an output variable and this particular variable I have defined as where dot is that basically means that it is taking the value of 100 that I have defined in my local. So based on this you could consider the input variables are like the function arguments the output values are basically the function return values and the local values are basically functions temporary local variables whose scope is just based or defined within that particular function. So now let's dive into some more theory about the variable block. So the variable block has the following arguments it has the default so you can use the default to set values for your particular variable this is an optional block then there is the type so the type defines what type of variable it is. So there are the following options that you have you have string number boolean list or map now I will give you examples about these types in a later lecture you can also set a description about that variable which again is an optional block you can also set validation for the values of that particular variable and you can also have a sensitive argument in your input variable so you can use this if you do not want to display your output for that particular variable in the output field or in the output block now inputs can be defined either in your main.tf and your variables.tf now there's a distinction between defining variables and assigning values to them so this is basically where you define your particular variables you can either define them in your main.tf or in your variables.tf so I will show you an example later on in this particular section as to using both main.tf as well as using variables.tf now once you declared your variables you can assign values to them as well so there are multiple ways in which you can assign values so the most important ones are using the slash where command when you're running the terraform apply command so you can have a slash where option where you can actually assign values to the variables that you have defined a second option is using the .tf where files so within .tf where files you can assign values to variables that you have already declared in your main.tf or in your variables.tf file you can also assign variables using the environmental variables so the precedence is also as follows the environmental variable has the least precedence and using the slash where command has the highest precedence now let's look at all the types of input variables there are three kinds of input variables there is the string the number and the boolean apart from that the other types include the complex types which can be of either collection types or the structural types so if you're using collection types then it consists of lists maps and sets so you use collections types when you want to allow multiple values of another type to be grouped together as a single value and you use structural types when you want to allow multiple values of several distinct types to be grouped together as a single value so one important distinction is whenever you're using a structural type then you also need to mention the schema for that particular type whereas for collection types you do not need to mention the schema you just can mention what type it is so these types I will explain using examples so let's go ahead and let's see the examples in the next particular chapter okay now let's talk about creating some variables and displaying it as output and we might also talk about a few local variables or define a few local variables so let's start with the most basic so we have the primitive type of string so let's create those variables so before we need to before we create them we need to declare them and then assign values to them so I have a set of files that I have included in my project or in my root module we have the main dot tf so main dot tf is a place where you can declare variables and you can do the same in your variables dot tf file as well so you can declare variables in either main dot tf or your variable dot tf and I also have a terraform dot tf where so here is where you can actually assign values to that particular variable and the output is where you can store your output that you can display while you're doing the terraform apply so let's start by creating the most basic so let's create a variable of type string so I will be declaring this particular variable in my variable dot tf file so to do that let's create a variable block so I've created my variable the name of the variable is string where and it is of type string so this is important so the type will always be there and this is the only mandatory field that is required in variable so now let's declare a particular value for this string where so to do that you can go to your terraform dot tf where's and you can declare a value for that particular string so I have declared this particular value string where has the value my fair string so now let's go to the output dot tf so in the output block you can display the output of this particular variable so this particular variable that I have declared in my variable dot tf and the values that I have initialized in my tf fares will be displayed because of this particular output block so let us run this particular terraform file so I'll do a terraform apply so it says that changes because this is the variable that I have created so it will give you this particular as the output so I will do a yes and you can see the output so this particular block which represents the output block has the value of this particular variable that is the variable that you have declared here that is my fair string so let's do a same and let's just change it to a number now so I'll declare another variable and this will be of type number I'll change this to number as well and here in the terraform dot tf so I will not assign values in the terra for tf fares I will assign it using the slash where command when I do a terraform apply so the only thing I'll do here is I just display it as output so I'll just copy this I'll just do a num and the value I will get from here and I'll just copy it here so in this particular way in this particular example this particular number I am assigning in my variable dot tf I'm sorry I am declaring in my variable dot tf and the values that I will assign I will assign when I once I run the terraform apply so let's do that so I do a terraform apply and I do a slash there and for this particular variable I will assign the value 100 I'll do a yes and you can see that this particular so if you go to the output dot tf you can see that this particular block of this particular name that is output num has the value of 100 so this 100 has come from the slash where that I gave when I ran the terraform apply and as you know that this particular slash where has got a higher precedence now let's create another boolean and this boolean will declare not in a variable file but in a main dot tf so let's do that again I'll declare the output as well so again I have to declare the output and instead of the variable dot tf I've used the main dot tf to create or to declare this variable and I have assigned my variable in the terraform dot tfs file so let's run this again okay so boolean is not the correct type so I need to change this to pool so let me run this again I'll do a yes so you can see that the boolean output has got a value of true so this is how you declare simple variables so in the next chapter I'll discuss on how we can create complex variables and assign them as outputs so I'll okay let's start by creating the complex types so in the complex types let's start with the collection types so the collection type consists of list map and set you could consider list and set to be like arrays and maps to be like json objects so let's start by creating a list so here I have a list of string and the variable name is list example so I have declared my variable and let's assign a few values to this particular list so I'll copy this variable list I will save this I will assign values to it in the terraform dot tfs so it would be of type it would be a list of strings so I can just put some random values here I have assigned list values to this particular value so again you can say you can say that it's an array and this array contains three values and each of them are strings so if you do not assign a string value it automatically gets converted to string so let me just get the output for this so I'll just copy this I'll go to my output and I'll assign an output I will create an output block I'll call it list and the value would be variable dot list example so let me just run this particular terraform file I'll do a terraform apply so you can see that single quotes are not valid so you need to use double quotes so let's go back to our terraform tfs and let's just change this to double quotes again let me run this I'll do a yes so you can see the list has been displayed in the output so let's assume that you just want to display one particular value let's say b so to do that you can go back to your output file and you can just set it to one like you would do in an array so it starts with zero and one would hopefully give you the value b so let's run this again I'll save this file and let's run this again I'll do a yes so you can see the list currently has values of just b okay so now the difference between a list and a set is that using set you cannot do this particular operation so let's just change this to type set of string and if I run the same thing again it will throw an error so list and set are basically the same the only difference is set does not have the concept of indices so you cannot search for a particular value in that particular string like we try to do using the output so this thing does not work if you're using set so that's one thing you need to remember so let's go back to our output.tf and let's remove this and let's say if you are able to run it again so let's do a terraform apply now you can see that using your set you can display all the values so the only thing that is not possible using set is the concept of indices where you want to just target a specific value so now let's talk about maps so in our previous example we talked about list and set in this particular example we'll talk about map so let's define a map so a map is basically an object so to define a map you can do it again in your main.tf or in your variable.tf so let's do it in the variable.tf so I have created a map of type simple map example and the type is map string so this particular variable will accept any object that has the value pair of the key value as a string so once you have created this once you have defined this variable let's assign a particular value to it so the kind of values that you can assign would be something like this so you have an object and the object has two keys and the values are both strings so this is a perfect example of type map string so again in the output I'm just going to display the same variable so let's run this particular example so you can see the output again is just the key value pair now if you want the now if you want your values in your key values to be anything whether it's a string number or boolean then you can change this from string to any so once you've done that you can go back to your tf and then you can just change it to anything so like for example I can just have a subnet three and I can set it to a boolean so I can set it to true and I can have one more subnet I'll keep this as subnet four and I'll set it to a number so if I set it to any that means it'll take any value so let's just run this again and I do a yes so you can see that the output is valid okay now let's change it from any to number so let's see what happens once we do that so I've changed it from any to number and if we run a terraform apply it'll say that the values are invalid so if you need to handle a situation where you do not know the type of the inputs then you can always put any so that's it for this lecture in my next lecture I'll explain to you what an object is so in our previous example we had learned about maps so these maps are key value pairs where the value can either be of string number or boolean that is they can only be of primitive types now let's assume you want to create a map of key value pairs where the value is not a primitive but a very distinct object itself so how do we do that so to meet a scenario like that we have to create an object first and then what we will do is we'll create a map of those objects so let's start by creating an object so the important thing to remember is that an object is a group of distinct types and an object should always have a schema associated with it so let's create a variable of type object so here so here I've created a variable of type object and this object can take two strings as key value pairs the cider block and the region so if I create a so if I assign a value to this particular object example let's see how it should look like so I'll copy this and I will assign a value to this so this has to be so this has to contain the cider block and the region so I'll just copy this and the cider block I'll just define a value as 10 dot 0 dot 0 dot 0 slash 20 and then it should also contain a region so I'll just copy this region and I'll set this to us east so this is just an example and I have to put a comma so now what I've done is I have assigned this particular object example the value cider block is equal to this and the region is equal to this so let's output this particular value so I'll copy this I'll go to my output dot tf and I'll paste that particular variable so once I've done that let me just run this particular terraform so you can see that this particular output has these values okay so this is how you create an object now creating an object by itself has no value what you need to do is you need to create a map of these objects so that the map can not just be linked to a primitive but it can also be linked to a structure like an object so to do that let's go back to a variable again what we'll do is we will define a map of objects so what I'll do is I'll just copy this and I'll paste it and now what I'll do is I'll create a map of objects and then I'll go back to my terraform dot tf as and I'll just so what I'll do now is I'll create a map and these maps will have key values and the value would be this particular object so let me just copy this I'll just have two subnets and what I'll do is I'll just copy these objects as well and I'll change it from test here I'll change the value as well so I'll just keep it as one okay now what I'll do is I will just change this to I'll change the value of this to a map object example I'll copy this I'll save this and I'll output that this particular variable as well okay let's apply this again yes so now you can see that I was able to create a map of not a primitive but a map of map of objects so this is how you can use your map and objects combine them together and get a useful variable created so I've created a set of assignments I hope you finish those assignments because once you've done them then I'm sure that you would have perfected everything with regards to variables so I'll see you in the next lecture now hi this is going to be a very short lecture on how or where you can declare and initialize your local variables your local variables have to be declared in your main dot tf here I have an example of initializing a local variable and setting it a value of 100 now if I want to output this particular local variable I have to go to my outputs and here what I need to do is instead of where I need to do a local so I will do a local dot a because a is the name of that variable and if I run this then the output should return me 100 so let me run this particular so you can see that the value is 100 so this is how you use your local variables and output them just remember that the scope of your local variables is only within your main dot tf so that is one thing you should remember so I'll see you in the next section now so now let's talk about the provider block terraform relies on plugins called providers to interact with remote cloud system so whenever you open your main dot tf and if you want to connect it with aws or rajah or gcp the first block that you see is the provider block so here is an example of how a provider block looks like in aws so this is a very basic template and you can get it from this particular link that I will provide in the description below and whenever you do a terraform in it that particular provider block will download all the dependencies that you need for your terraform to interact with aws so now let's talk about configuring the provider now there are two important configurations that you need to do in your provider the first is the region and there you need to specify a particular region and any resource that you create by default will be created in that particular region and apart from the region the other important configuration that you make to your provider is the authentication and here you decide how you need to authenticate your aws with terraform so this was something that was discussed in the previous chapter and you can just go through that particular chapter that also would be provided in the description below so I'll see you in the next chapter so now let's talk about the most important block in terraform that is the resource block so resources are the most basic elements in the terraform language and each resource block can describe one or more infrastructure objects such as a virtual network a compute instance or even a higher level component such as a dns record so let's have a look at an example of a resource block so let's look at the structure of a resource block the resource block starts with the word resource followed by the type of that particular resource and the name that is given to that particular type so here the type represents the function that the particular resource will do for example here the aws underscore vpc type will create a particular vpc in aws and apart from that each type also has a set of arguments associated with it so here for example aws underscore vpc has the argument as cider block now based on the different types there would be different set of arguments now apart from that each particular type has a set of meta arguments associated with it so they include the depends on count for underscore each provider and lifestyle now these meta meta arguments I'll explain to you in the further chapters going forward now once you execute this particular resource block what happens is that a particular infrastructure gets created so this particular infrastructure that gets created would be a vpc so the most important question is how do you find the correct resource type well since you are just a beginner the easiest way to find the resource type is to just google it now let me show you an example of how you can do that now if you want to create an instance in aws and you want to know the correct terraform type you can just type terraform create instance aws or something of that sort and what you get as the response and the first entry would always be the correct type so if you look at this this is basically the type that you would want to use to create an instance in aws so that is the easiest way to do it if you're a beginner just google whatever you want to do and follow it up with the word terraform and the first result that you would get would be the type that you require so I'll see you in the next lecture so let's look at a very basic example of how you can use a resource block so to my left is basically the architecture that I want to build I want to build a vpc and within that vpc I want to build one particular subnet and the cider ranges are also mentioned in the diagram so let's see how we can do that so the first resource block that we need to use is basically the aws.vpc resource block so this resource block we had seen previously so all that you need to have as an argument is just a cider block and here the cider block I mentioned is 10.0.0.0 slash 16 so this corresponds to this particular cider block so before we proceed let's look at the documentation of this particular resource block so I go back to my terraform documentation page about this particular resource so this particular resource like I said previously is used to create vpc so if you keep going down you can see a list of arguments so these arguments are basically the inputs that you would want to give to this particular resource block so they include the cider block so cider block as I said previously is a required argument so you need to give this the rest of them are basically defaults so based on how you want to design your architecture you can optionally choose some of these fields now apart from the argument reference there's also something called as the attribute reference so in addition to all the arguments above the following attributes are exported so this includes the arn the id now some of these entries are basically calculated only once your resource has been deployed or created so for example the id of the vpc would only be known after you do a terraform apply and similarly the arn as well so these are some important things that you need to remember that is the argument reference so this is basically the inputs or the type of inputs that you can give to this particular resource block and apart from that the attribute reference is also something that you need to remember and you should remember that most of the entries within the attribute reference are calculated only after you do a terraform apply so an important attribute reference that we would need to use is basically the id so let's go back to our code so once you've created your vpc the next thing you want to do is you want to create your subnet and this subnet is basically and this particular subnet has to be in this particular vpc that we previously created and so we basically need the id of that particular vpc so to get the id of that particular vpc what we need to do is we need to give the resource type so here the resource type is aws underscore vpc and that name of that particular resource type so this is very important so the name that I had given for my type was example so you need to give that name and you need to append that with the id so this particular attribute will give you the id for this particular vpc that we previously created apart from that you can also you need to also mention the availability zone and the cider block now let's go look at the aws underscore subnet documentation and let's see which are the attributes that are important or required and which are the optional ones so I copy this so here I get the aws underscore subnet and let's keep going down so here again the only two important or the only two arguments that are required are basically the cider block and the vpc underscore id so apart from these two everything else is optional so I've also mentioned the availability zone but that is not an optional that is that is an optional block so you do not need to actually mention it so this availability zone is an optional block whereas the other two attributes are required as well as the tags the tags is also something that's not required so let's do a terraform apply and let's see what happens so once you do a terraform apply you can see all the resources that will be created so in our example there are two resources that get created the first one is the vpc and then the subnet within that particular vpc so so let's do a yes and let's create both these resources so you can see both those resources got created so let's go back to a console and let's check whether these resources have been created so the vpc id is this so let me just copy this so as you can see that my particular vpc got created here and if I open this you can see the cider block is 10 000 slash 16 and let's also check the subnet within this particular vpc so if you click on the subnet here as you go to the subnet page you can see that this particular vpc is connected to this particular subnet and this subnet has the cider range that we had mentioned in the resource block so that is how you create your vpc and subnet using terraform I hope this was a useful lecture I'll see you soon so let's talk about data block and data sources so data sources allows data to be fetched or computed for you somewhere else in a terraform configuration now use of a data sources allows the terraform configuration to make use of information that is defined outside of terraform or defined by some other separate terraform configuration so in our previous section we had seen and we had created a vpc now let's assume that you want to create another terraform configuration and in that particular terraform configuration you want to use the previously created vpc so how do you do that so to do that you need to use a data block so let's look at an example of a data block so here I have created a vpc network in my previous section and it has one particular subnet now let's assume that you want to create another terraform configuration let's assume that you want to create a virtual machine and that particular virtual machine needs the vpc er in for example or some other information related to vpc so how do you get that now to get that particular information what you need to do is you need to use the data block and the data block starts with the word data and after that it's basically the type so this basic so this particular type corresponds with the resource type so the only difference here is that instead of using resource we're using data and then again the name of that particular type so the name can be anything and you also need to provide certain attributes so here the attribute that I'm providing is the id so this id corresponds to the vpc id now let's assume that you want to get the er in for this particular vpc so to do that you can use the data attribute and then append it with the type and the name and then the corresponding er in dot er in so this particular data block the output of this particular data block would give you the output that you see at the bottom of your screen so let's look at an example of this so what I'll do is I'll just copy this piece of code here I have a very basic terraform configuration so this just has the provider information nothing else so let me just paste this particular piece of code so here I have copied my piece of code again this is basically the type this is the name of that particular type and this is the id and what I'm getting as the response is basically the concatenation of data that is this and the type that is aws underscore vpc get underscore network is the name of the type and the er in so this er attribute is not currently present so when I run this particular attribute I will get the er in so let's run this particular terraform configuration so I'll do a terraform apply I think I need to save this I'll do a yes so you can see here is the particular er and for that particular vpc now another interesting fact is if you go to the tf state you will under the resource block you'll get all the information about that particular vpc so here if you keep browsing you'll get everything with regards to that particular vpc including the cider block the id the owner underscore id the state and of course the er in that you have just outputted so this is how you use a data block now your terraform documentation is divided into two halves the resources and the data sources now the resources is what you would put into the resource block and the data sources is what you would put into the data block so in our previous two examples we had seen using the resource block how we were able to create a particular vpc so if you go underneath the resource block you will find the aws underscore vpc resource block documentation so here you will get all information about how to create that particular resource now if you go back and if you search under the under the data sources you will get all the data block documentation for the same so let's assume you want to get information about a particular vpc then you would go under the aws underscore vpc data source so that is how the documentation for your terraform registry is divided into now again to reiterate your documentation in terraform would be divided into two halves for each particular resource it would be divided into the resources and the data sources so whenever you want to create a particular resource you would look into the resources section and whenever you want to get get information about an already created resource then you would look into data sources so i hope this was a useful lecture i will see you in the next okay so in our previous lecture we had seen how to create a vpc in this particular lecture i'll show you two things the first thing is i will show you how using just one particular resource block you can create multiple subnets we'll use of a we'll make use of a meta argument called for each to do that so that could come in handy especially if you want to create multiple resources of the same time using using just one particular block so that's something that will be very useful for you and the other thing that i'll show you is how you can use a data block to fetch information about a resource that already exists or is already created so for example we've already created the vpc so we'll be using a data block just to fetch information about that data block so our final architecture should look something like this so there should be four subnets and these subnets would be just created using one resource block so let's proceed and let's see how this is done so here is our code base so we have a main.tf file a terraform.tfs and a variables.tfs so let's look at the variable.tfs so the variable.tfs has the definition for the vpc id so we'll be inputting a vpc id and that particular vpc id would be stored in the terraform.tfs so this is basically the vpc that we've created so we have to input the id for that particular vpc now let's go look at the main.tf this is the everything all our configuration lies so here we have basic configuration so nothing special here and then comes the data block so this is our fetch data block in this particular data block what we are inputting as one of the parameter is just the variable.vpc.id so this is the variable that we had got from the terraform.tfs file once we get this particular information the next most important thing we need to do is we need to get that cider block for this particular vpc and we need to chop them into four different blocks so to do that I have used a function called cidr subnets so to get more information about how this function works I will send a link in the description below so this basically what this particular function will do is this will chop the cider block into four specific halves based on the parameters that you've set here and it will return a list of cider blocks this particular list will act as the cider block for each of the four subnets that we are going to create so once you've done that then we come to our main part so this is basically the resource block and this resource block contains the for each meta argument so what this for each will do is it will loop this particular resource based on the number of elements in this particular cider block and one important thing to note is that this particular cider block is a list and it has to be converted to a set so for each only works if in case it is a set and not in case of a list so to do that we have another function called as two set and this particular two set will convert the list into a set and once you've done that this particular resource like I said previously will loop based on the number of elements that are there in that list and each particular cider block in this particular list will act as the cider block for each of the subnet that is created and apart from that the only other thing you need to populate is the VPC ID so this VPC ID is what we get from the data block as well so this particular value we can also get it directly from the variable as well but you also have the option of getting it from the data block so let's execute this particular terraform configuration and let's see what the output is so again let's just do a terraform apply and it states here that it's going to add four subnets so let's just do a yes so you can see the subnets having created here and you can see that four subnets got created so let's go back to a console just to verify so here i'm at my aws console so you can see that this was the vpc so i'll just open this i'll go to the subnet and if i search for this vpc you can see that four subnets have got created so this is how you can create multiple resources using just one resource block so i'll see you in the next lecture i hope you've learned a bit thank you hi folks welcome back so in our previous lecture we had seen how we could create a vpc and within that we could create subnets just by using a single resource block so let's take architecture a little further so the natural progression would always be to create an ec2 machine so let's do that so our first ec2 machine that we would create would be the most basic we wouldn't add any extra parameter and we would just be creating a default ec2 machine so as you know whenever you create a account in aws it always comes with a default vpc and that default vpc has a set of subnets within that so our first exercise would be to create an ec2 machine within that particular default vpc so we will also be using a meta argument called as count so we would be creating two ec2 machines just by using one single resource block so this is how a final architect should look like it should have a subnet and within that subnet should be two ec2 instances so this subnet is basically the subnet which belongs to the default vpc so now let's look at the code so our code is very simple it's hardly a few lines so here what we need to mention is the count now count again here is the meta argument that i need to give a value of two because i will be creating two instances then i need to give the amid now to get the amid you can always go back to your console and fetch the amid and here the instance type i have given is t2 micro and here i've also mentioned tags so the tags has a value of server and here there is a variable called count dot index so this is again a part of the meta argument count so you can do a count dot index to find which particular iteration of the count is being executed so now let's look at how to find the amid so now let's talk about the unenviable task of getting your amid so to do that you can go to your ec2 dashboard and under the ec2 dashboard you'll find something called as images so you can just click on amis under images okay so once you click on amis you can see that there are quite a lot of amis present so you need to be very specific about what you need so let's go through each one of these resource attributes and let's just pick that one appropriate amid that we need so let's start by talking about the platform so what we need is an amazon linux so you can always choose based on what you want so what i need for my particular ec2 machine is an amazon linux and then the next thing that i can choose is which particular architecture i need this platform to be in so you can click on architecture and then you can choose a 64 bit of intel so i just choose x86 so here again you need to choose based on what particular operating system you have and after this again you can see that it has come down to it has whittled down to 4007 so let's keep adding more and more filters so again the owner would be amazon itself so you can just click on amazon so after that you can also choose the creation dates you can see that most of these images are very old so you can just click on creation date and you can choose which particular after which particular date you want the images so i'll just click on after this particular date so i'll choose the beginning of the year so i just choose 01 01 i'll click on set so it has come down to 278 now i can also choose which particular root device type i need so it's going to be an ebs it's not going to be an instant store so you can just click on ebs and another important aspect is basically the kind of virtualization so there are two kinds of virtualization that is present as hvm and para virtualization so i'll give you a link on the difference between both of them but for this particular example i'll be choosing hvm so if we just click on hvm and after you've added all these filters you get around 248 so you can just choose any appropriate am i id of course you can keep adding more filters and making it more elaborate so that there are fewer number of entries present but just for this example i'll just pick any particular am i id and i'll just run this particular terraform configuration so that's exactly what i've done i've picked up our random am i from that list and the only thing left for me to do is just to run this so let's do that so you go back to my terminal click on new terminal and let me just do a terraform apply and it says that you're going to add two so basically it'll be two ec2 machines so i'll just click on yes so let's just wait for the machine to be created okay so find your resources are created so let's go back to our ec2 machine so again i'm back in my console you can just click on ec2 dashboard click on instances so here you can see that there are two instances running so these are the same instances that we had created so if you so if you open any one of these instances you can see that it's the same am i that we had provided and it's basically also in the default vpc if you just open this vpc and if you keep going to the right you can see that this was the default vpc because we had not provided any details about the vpc so that is how you create your ec2 machines in the most simplest manner possible so i'll see you in the next lecture where we'll add a few more complexities to this particular configuration so i'll see you there so in our previous example we had seen how we could create an ec2 machine within our default vpc so in this particular lecture let's do something different so as you know we had already created a vpc and we had created four subnets within that vpc so what we will do in this particular lecture is we'll create an ec2 machine within that particular subnet so i will choose subnet four and within subnet four i will create a ec2 instance now to create an ec2 instance within a subnet the first thing you need to do is you need to create an elastic network interface and that elastic network interface has to be connected to the subnet so once you've connected your nic to that subnet the next thing you do is you create your ec2 machine and that ec2 machine has to be linked to that nic that you had previously created and that is how you can link your ec2 machine to a particular subnet so let's go ahead and let's see how this is done so let's look at a code base so it consists of the main.tf the terraform.tfs and the variable.tf so let's look at the variable and the terraform.tf so what we will input in this particular terraform configuration is the subnet id and the terraform.tfs contains the subnet id now there are many better ways of doing this for example you can use a data block to get the subnet id but just for the sake of simplicity i just inputted or hardcoded the subnet id directly now let's go back and check our main.tf file so the main.tf file is pretty straightforward again there is just the provider with the basic configuration now in this particular configuration i've also used a data block called as aws underscore ami now this particular data block will return me the ami id based on the filter that i have given now here the filter that i have given is amazon linux 2 version the filter has an architecture of 8664 and the virtualization is hvm and here the interesting thing is that i have also said the most recent to true and this will only return one value as an output so you don't have to worry about multiple values being outputted by this and another important factor is the owner and the owner that i have given is amazon so this is an amazon owned image and not a user owned image and after i get the ami id the next thing i do is i create a network interface and this network interface like i showed in the diagram just needs to be connected with the subnet id again this is the subnet id that i have in my terraform.tfs which i have hardcoded let's go down and now comes the important part of creating a server and here the ami id is basically the id i get from the data block that i showed you previously so this one and once i get the ami id and the instance type again is going to be t2 micro so it's not going to incur much cost and then i have a network interface interface block within my aws underscore instance block and this network interface block i just need to connect it to the id of the network interface that i had previously connected and then i also need to specify the device index which i have set to zero because this is the first network interface that i've connected to this instance so that's about it there's nothing else apart from this so let's run the terraform configuration and it says that it will create two resources and that is true because it will create not just the instance but it'll also create a network interface as i had shown in the diagram so if you go back to the diagram you can see that there is one ec2 and a network interface and they're both interconnected so let's just say yes here so let's go back to a ec2 dashboard so you can see that there's one instance running so this this is the instance that i just created so you can just open this so let's open this instance id so you can see that this is basically the same subnet that we had in our variable file so if you go back to the tfs so you can see that this and this are the same so we've created the instance in this particular subnet so the next thing you can see is the nic so if you keep going down under the networking tab you can see that there was a network interface connected so you can just open this network interface and this particular network interface has basically the same subnet attached to it and it's basically using this particular vpc and as you can see that it is in use because it's connected to the instance and you can also get the instance id so this is the instance that we just created so you can just open this as well so yeah this is the instance that we just created so that's about it so that is how you can create an ec2 instance in a particular subnet so i'll see you in the next lecture