# AWS: Terraform Architecture & Core Blocks Explained

https://www.youtube.com/watch?v=1Q9SBeqAF2Q

[00:00] Hey guys, Shawn this side.
[00:02] Welcome back.
[00:02] And today in the AWS series we are going to start with Terraform.
[00:07] Okay.
[00:08] So till now what we have done is we have already set up the network like we have created VPC within a region.
[00:14] We have created multiple subnets within this VPC.
[00:16] We have a public subnet.
[00:19] We have private subnet.
[00:20] Public subnet is connected with the internet gateway.
[00:22] Private subnet we have connected with the net gateway.
[00:25] And we have created route tables and all.
[00:28] But the problem with this is that we have created all this thing by clicking on the console.
[00:37] But in real world in companies if you work, nobody create the infrastructure using this like this way like clicking on the console.
[00:49] The problem is that like let's say in some other region currently you are in region A.
[00:52] You have all this infrastructure set up.
[00:54] Now in region B you have to done the same set up.
[00:57] So you have to manually do the all the things.
[01:01] Okay, so there is a chance of mistake.
[01:04] Also if let's say some someday someone has changed certain value in this infrastructure.
[01:11] There is no way you can check the history like okay, what was the value before?
[01:15] Why it is changed?
[01:18] What's the purpose?
[01:19] So that's why infrastructure is never you can say that created using clicking on the console.
[01:26] So that's where the Terraform comes into the picture.
[01:29] So generally if you see in here our AWS there is something called cloud formation.
[01:41] This is also you can say that similar to Terraform which is used to create infrastructure.
[01:49] But as a file like as a code.
[01:52] Okay, infrastructure as a code.
[01:53] But cloud formation generally very specific to AWS.
[01:58] But, Terraform is very, very popular and
[02:02] You can say that it is like a framework.
[02:06] On top of it, we can write for Azure, we can write for AWS, we can write for Google, etc.
[02:12] Okay?
[02:16] And to be honest, Terraform is very, very easy to understand and learn.
[02:18] Okay?
[02:20] Even if you are learning it for the first time, right?
[02:23] As when we proceed further, you will see that hey, it's very easy.
[02:25] It's just like reading.
[02:27] Okay?
[02:28] So, let's see that.
[02:30] So, the Terraform, I already showed you the problem it solved.
[02:34] The problem is that we earlier we were doing through console, which if you have to replicate, it can cause multiple human errors.
[02:42] Second, if something is changed, we don't have the history what got changed.
[02:49] So, Terraform is nothing but an infrastructure as code.
[02:53] Infrastructure as code means like we create .tf extension files.
[03:00] And we can put these files into Git and you
[03:04] Can manage its history like how we used to manage the version in the Git, right?
[03:10] So, you can let's say you have let's say currently version one and the configurations.
[03:15] Now, if anybody has to change, they will raise a PR.
[03:18] Now, you can review.
[03:21] So, there is a proper process.
[03:22] You can review, then merge.
[03:25] Now, tomorrow if anybody has to change see that hey, this value, what was the previous value?
[03:29] You can easily check.
[03:31] Why change?
[03:32] What change?
[03:33] When change?
[03:34] So, everything can be properly, you can say that organized and managed via code itself.
[03:46] So, since these files are just text, we can store them in Git to to the version history.
[03:48] This allows us to track what changed, when changed, who changed, and why changed.
[03:57] Now, Terraform is like not an imperative infrastructure as code.
[04:01] It's a declarative infrastructure as code.
[04:05] There is a difference between imperative and declarative.
[04:09] Imperative means I am saying that, "Hey, first create VPC, then wait for it to complete.
[04:23] Then create public subnet.
[04:27] Then create private subnet.
[04:30] Okay?
[04:30] After that, you have to create internet gateway.
[04:31] After that, you have to wait.
[04:34] And after that, you have to create NAT gateway.
[04:36] So, you are providing the sequence when to wait, when to start, what first, what second, which API to call.
[04:44] So, that's imperative.
[04:46] So, like you are giving a proper instruction, like create VPC, then wait.
[04:53] Then create subnet, then wait.
[04:56] Then attach subnet to VPC.
[04:57] Create internet gateway.
[04:59] Then attach internet gateway to VPC.
[05:01] So, you are giving proper command.
[05:03] If you miss one proper command, or if you miss certain sequence, there is a
[05:07] Chance that you won't get the desired result.
[05:11] So, in the declarative result in the declarative infrastructure as code, it's more like just tell me what you want.
[05:18] Hey, I want VPC and two subnets within it.
[05:26] It's like a command you are giving.
[05:27] I want a VPC and two subnets within it.
[05:29] That's all.
[05:32] So, now it will decide its own order that I have to create VPC first, and then create two subnets, and then I have to attach it.
[05:39] All I have to told is that VPC and two subnets within it.
[05:43] Okay?
[05:45] That much information we have to give.
[05:48] But we don't have to provide like all the minor minor details.
[05:51] Got it?
[05:54] It's like consider like providing a recipe for making tea or you just want I want tea.
[05:58] Like that.
[05:59] Okay?
[06:02] So, here in the declarative infrastructure as code means we describe only the desired end state.
[06:09] That is clear?
[06:11] Terraform is declarative infrastructure as code, not imperative infrastructure as code.
[06:16] Now, let me quickly tell you a Terraform architecture.
[06:21] See, we don't have to go inside Terraform and debug everything.
[06:27] Okay?
[06:27] That is unnecessary.
[06:29] We will complicate the thing, but I will try to keep one level higher what's the architecture of Terraform so that when we proceed further, we would be able to connect like what is happening.
[06:42] The very first is your configuration files, {dot} tf files.
[06:44] Here we will write our declarative instructions.
[06:49] That hey, create VPC, create public subnet within this VPC, create private subnet within this VPC.
[06:56] Like this, we will give the command.
[06:58] Cool.
[06:59] The second very important part is the Terraform core.
[07:02] This is the major you can say that the brain of the Terraform, Terraform core.
[07:08] So, it has five step.
[07:08] So, let me zoom it.
[07:10] A bit.
[07:11] The first task of the Terraform core is to read and parse all .tf files.
[07:17] So, read.
[07:20] See, you can have only one .tf files or you can have multiple .tf files.
[07:26] Okay?
[07:27] Doesn't matter.
[07:29] You can like properly manage by creating multiple .tf files that okay, only VPC related I will create into this Terraform files.
[07:39] And uh gateway related I will create in this or you can create into one itself.
[07:46] The Terraform code read all files which are ending with .tf and parse it.
[07:54] That is the first job.
[07:56] Second job is is to validate the syntax of .tf file.
[07:58] Like is there any missing bracket, any duplicate blocks, etc.
[08:04] So, second thing is validation of the .tf file.
[08:09] The third important part is the
[08:11] But, it has a built-in, you can say that knowledge, like how to create a dependency graph.
[08:11] I told you, right?
[08:14] It's a declarative.
[08:16] We don't have to tell the steps.
[08:24] So, using this dependency graph, okay.
[08:27] Hey, user told me that I need to create VPC and two subnets within that VPC.
[08:34] So, dependency graph, it will create like, okay, VPC, after that I have two child nodes, uh subnet one, subnet two.
[08:42] Let's say if you have something like, okay, uh create a net, or you can say that I'm just saying something, create a net gateway, attach with this one particular subnet.
[08:53] So, it will say that, okay, within this subnet, I have to attach net gateway.
[08:56] So, it is creating some graph, that, okay, first I have to create this, then this, then this.
[09:00] So, it will automatically determine the sequence of creation of your resources.
[09:07] Got it?
[09:07] The fourth very important part is a state manager.
[09:09] What does it mean?
[09:09] So,
[09:12] See, you have given the command, like, hey, I need to create VPC, I need to have.
[09:19] So, this is your infrastructure as code.
[09:21] You have gre-
[09:22] You have said that I need VPC, I need subnet within that VPC, I need net gateway within that subnet.
[09:34] Let's say you have this infrastructure as code.
[09:38] Okay, now let's say you have run it first time.
[09:40] Now you have run it for the first time.
[09:44] When you run it for the first first time, let's say in the AWS console, you can see and check that a VPC is created.
[09:51] Within that VPC, you have subnet created.
[09:58] And within that subnet, you have net gateway created.
[10:04] So, this is created.
[10:06] And each one has an ID.
[10:08] ID ID resource number ARN number.
[10:12] Okay?
[10:13] Now, let's say this is an infrastructure as code.
[10:15] You can run it second time also.
[10:17] So, will it create a duplicate?
[10:20] No, that's where the state manager comes into the picture.
[10:26] So, what happened here is Terraform also maintains, apart from TF, one the first time you run it, or you can say that every time you run it, it also create dot TF state file also.
[10:39] Okay?
[10:43] So, it create dot TF state file.
[10:45] In that, let's say once it created this, it has complete information.
[10:51] Okay, VPC this is what we wanted.
[10:54] Let's say whatever we wanted.
[10:56] We wanted that this is the CIDR range.
[10:58] I think I hope this is CIDR range when I'm saying it, it's already clear because we have already done it.
[11:02] Okay?
[11:08] Now, in the response, like okay, this is the ARN number.
[11:10] And let's say this is the ID or this is the name, something.
[11:13] Is the response I got?
[11:16] So, in the TF state, I have this complete information.
[11:20] Okay, this is the subnet.
[11:22] For subnet also, I have one CIDR range.
[11:25] Okay, that's what I wanted.
[11:27] But once it is created, what is the ARN number?
[11:31] Okay, uh, and certain what is the name, etc.
[11:34] All the outcome, you can say that it is stored into dot tfstate.
[11:39] Similarly for the net.
[11:41] Now, when you run it for the second time, now I'm running it second time.
[11:46] What it do?
[11:50] It first compare your TF file.
[11:51] Okay.
[11:52] In the TF file, you have a VPC.
[11:56] Here, I want to create a VPC and this is the range something.
[12:01] Okay, 198.0.0.0/23.
[12:06] This is the CIDR for the VPC I want to create.
[12:09] I want to exist.
[12:11] I want to have exist.
[12:12] So, when you run it for the second time,
[12:14] Will it directly create this VPC within your AWS resource?
[12:16] No.
[12:20] Now, it will do certain work.
[12:22] It will first check dot tfstate.
[12:25] It will check okay, the VPC which I have.
[12:27] It will check the ARN number.
[12:30] It will call your AWS API and check.
[12:34] Hey, can you describe this ARN?
[12:37] So, it will send a response.
[12:40] Now, it will compare everything.
[12:43] Okay.
[12:45] Here, I have this VPC already created for for this ARN has 198.0.0.0/23 already.
[12:55] Okay, so this is the details are coming for this ARN.
[12:59] It also match with its own.
[13:01] Okay.
[13:04] The CIDR range is also I have the same.
[13:06] So, nothing change in console and what I have.
[13:09] Nothing change.
[13:13] And what user wanted is also the same.
[13:13] So, means I don't have to create.
[13:16] So, you understand what it is doing?
[13:18] It is not blindly go and create the resource.
[13:24] So, it does three things.
[13:26] Compare three things.
[13:29] Dot files, .tf file considered like what we want at now.
[13:33] Because you are running, right?
[13:36] So, this is like what we wanted.
[13:39] .tf state is like what exist or you can say that what previously created.
[13:52] And console is like a source of truth like what previously created is this the same exist in the console also or not.
[13:59] Because we can just can't rely on .tf state.
[14:01] What if somebody directly on the console has changed it?
[14:05] Let's say.
[14:06] So, this is what you want now.
[14:09] This is the VPC and this is the CIDR range.
[14:11] What previously has been created?
[14:13] This is the details I have.
[14:15] Using the ARN number, it just
[14:17] Invoke AWS API, check the current that hey, for this ARN, is this everything same, right?
[14:23] Yes, everything is same.
[14:25] Match.
[14:26] Nothing is changed.
[14:26] And the same you wanted now.
[14:29] So, means I don't have to create something new.
[14:33] So, state manager.
[14:35] Task is to find out what all resources already exist.
[14:40] So, that it should not create duplicate resource.
[14:44] Then the last is plan generator.
[14:46] Plan generator is using this information, it will now know that okay, resource one I have to create.
[14:55] Resource two I have to update or resource three I have to delete.
[14:59] Something like that, a plan would be created.
[15:02] So, important part is that just understand that .tf is declarative.
[15:13] Let's say earlier, first time, this is your console.
[15:18] You have like resource one, resource two.
[15:21] And now when you run it, two resource got created into the console.
[15:26] And in your dot tfstate, you have information about this two state.
[15:34] Resource one, it's all information.
[15:38] Resource two, it's ARN number, everything.
[15:45] Now let's say that these are just file.
[15:48] Now you have removed resource two.
[15:51] So earlier first time you run it, so you have all this.
[15:54] Now you have removed the resource two.
[15:55] That's all.
[15:57] And now you are running it second time.
[15:59] Now what would happen?
[16:01] Will it directly remove it?
[16:03] No.
[16:03] So first it will check the dot your tfstate.
[16:06] And it will compare.
[16:09] First resource one, resource one, this is what you wanted, this is what I have, and this is what there in the console.
[16:14] All same, so no change.
[16:17] Okay?
[16:17] Resource two, this is you don't want it.
[16:23] Okay, but I have it.
[16:26] Dot tfstate, dot tfstate, I have it.
[16:29] In the console also, I have it in the same.
[16:32] But now you don't want it, so means I have to delete the resource two.
[16:38] Okay?
[16:40] So now from your dot tf file itself it determine whether I have to create, whether I have to delete.
[16:46] Okay?
[16:48] So it create a plan, plan generator.
[16:49] What resources it will create, what resources it will update, what resource it will delete.
[16:54] There are certain resources which is allowed to update.
[16:57] If not, you have to delete it and create new.
[17:02] So with that one it create a plan.
[17:04] So you can say that state manager is something helps in that.
[17:07] So Terraform is Terraform core is something the brain of your Terraform.
[17:12] That is clear?
[17:15] Now there is something called provider plug-in interface.
[17:18] So, this is as the name say it's an
[17:20] Interface.
[17:22] Like ultimately your Terraform has to talk with your AWS or Azure or GCP or multiple providers, right?
[17:35] So, we can't have directly, uh, like have all this hardcoded into Terraform core.
[17:41] So, that's where the provider plug-in interface.
[17:44] So, all the APIs which Terraform code needed, let's say create, update, delete, read, something.
[17:52] So, it invoke this interface only.
[17:54] So, interface is just like no implementation.
[17:57] It's just like API it provided.
[17:59] Which Terraform code knows.
[18:01] Now, whichever the resource you are using, let's say AWS or Azure, they will provide the implementation.
[18:09] Okay?
[18:12] So, provider plug-in interface is nothing but exposing the endpoints which Terraform code required to create the resource, to delete the.
[18:21] Resource, to update the resource, to read the resource, etc.
[18:26] Or now, this is just an interface.
[18:29] So, we need an implementation of this endpoints.
[18:35] That's where your provider specific plug-ins comes into the picture.
[18:37] AWS provider plug-in or Google provider plug-in or Azure provider plug-in.
[18:42] Like let's say if you are using AWS, then we need a AWS provider plug-in.
[18:48] So, this provider plug-in provide an implementation of this create, read, update, delete method.
[18:55] Okay?
[18:57] And now its task is to validate the arguments.
[19:00] Okay?
[19:01] Because now let's say it will say create VPC, let's say.
[19:04] Create VPC.
[19:07] So, to create a VPC it need to have certain arguments, right?
[19:12] I need a CIDR block.
[19:14] So, this is very specific to you can say that uh AWS AWS need to validate its own that there is no direct method that create
[19:23] VPC. Let's say create only.
[19:25] Now, within the create it has let's say resource conflict.
[19:28] Within the resource conflict it has let's say for create for VPC.
[19:33] Now, AWS provider plugin has to validate that okay the resource conflict is let's say for VPC but I need this argument mandatory.
[19:44] CIDR should be present.
[19:46] Okay? And rest are optional.
[19:49] So, it validate now what all the arguments which are required to create the resource in AWS that is valid or not.
[19:56] Okay?
[19:58] The Terraform core validation was very specific to .tf file.
[20:04] Okay? So, there are difference.
[20:06] There is a syntax for .tf file.
[20:09] Okay? Now, in that .tf file we are saying that again we need to create a VPC and these all values.
[20:16] Now, within the VPC these all values Terraform core doesn't know.
[20:21] Okay? So, let's say within the VPC all
[20:24] these arguments has to be validated by
[20:27] AWS that okay the argument which you are
[20:29] passing whether it is valid or not.
[20:32] But from the Terraform core
[20:34] core, okay, you have you provided the
[20:35] name of the resource. Okay, bracket is
[20:38] start bracket off. Yes, syntax is right.
[20:40] But now the resource which you want
[20:43] its argument I don't know.
[20:46] Check with the AWS provider plugin. It
[20:48] will do the validation.
[20:50] Now, if
[20:51] provider
[20:52] plugin what it does it it just have
[20:55] embedded AWS SDK code. It's not that you
[20:58] can say that separate. It's just an SDK
[21:00] which your AWS provider plugin itself
[21:02] embedded. Internally it has this.
[21:06] Now, it will invoke the AWS API. AWS API
[21:09] call the AWS resource.
[21:12] Okay? So, let's say provider plugin
[21:14] mostly do
[21:16] provide an implementation of this
[21:17] method, do certain validation, and you
[21:19] can say that internally invoke SDK. SDK
[21:23] what it will do, it actually do request
[21:27] creation, and ultimately invoke it, and
[21:29] the response comes, it will do parse the
[21:32] response because it knows that, and
[21:34] ultimately pass like this.
[21:36] Okay?
[21:37] So, this is your sequence. You write a
[21:39] configuration file.
[21:41] Ultimately, it goes to the Terraform
[21:42] core. Terraform core
[21:45] invoke an endpoint in the plugin
[21:47] interface.
[21:48] Depending upon which plugin you are
[21:50] using, it goes to the AWS, let's say
[21:52] provider plugin.
[21:53] Uh AWS provider plugin internally use
[21:57] make use of AWS SDK to call an AWS API.
[22:01] AWS API ultimately call the AWS
[22:03] resources.
[22:05] Clear? Generally, this is the high-level
[22:08] Terraform architecture.
[22:10] And these are the tasks which Terraform
[22:12] core does. Very important.
[22:16] Now, let's come to the next part of
[22:18] Terraform blocks. So, see,
[22:21] everything in Terraform is written in
[22:23] blocks. Everything.
[22:25] A block is like a container that hold
[22:27] information.
[22:29] There are different blocks. Okay, I
[22:31] won't cover all the blocks. So,
[22:34] gradually, let's say,
[22:36] I will Today, I will only cover those
[22:38] blocks which are sufficient to create
[22:41] the network which we have created.
[22:43] Like we have created VPC, subnet,
[22:45] internet gateways, NAT, and all. So,
[22:48] today I will cover the blocks those are
[22:49] sufficient
[22:52] to create the network which we have
[22:53] done. Like in future, when we cover more
[22:56] topics, and if then more blocks we have
[22:59] to add
[23:00] because everything we will have to
[23:02] create it using Terraform only.
[23:04] Okay? So, it's not like today we are
[23:06] covering the Terraform, it's like
[23:07] finished.
[23:09] It's you can say that gradually we will
[23:11] build the Terraform also. Anything
[23:15] console we have to create, we can create
[23:17] through Terraform. So, the more we
[23:19] proceed further, you can say that we can
[23:21] add new things into the Terraform.
[23:24] So, you can say that this is like
[23:26] Terraform file we are creating. Till
[23:27] now, what we have done.
[23:29] But since the Terraform we are touching
[23:30] for the first time, I'm just creating a
[23:32] building block. Otherwise, once we know
[23:34] that, we can directly
[23:36] create a
[23:38] infrastructure using directly files,
[23:40] which I will tell you in the next part
[23:41] of this video.
[23:44] So, blocks. Everything in Terraform is
[23:46] written in blocks.
[23:47] A block is like a container that hold
[23:49] information. So, you have a block type.
[23:53] Okay? So, block type you can say that
[23:54] this is what
[23:57] Terraform
[23:58] defines. This is a block type. It should
[24:00] be like this.
[24:02] Okay? Label.
[24:04] It's again
[24:06] it's a Terraform defined. We can't do
[24:09] give any label here. Label two is you
[24:12] can say that it is like a name, custom
[24:14] name. You can give any name.
[24:16] And the key value, this is I think about
[24:18] an arguments. We will see that, don't
[24:19] worry about this, but consider that a
[24:21] block is something which has
[24:23] which hold all the information.
[24:26] So, that's what I told you the note,
[24:27] there are different type of blocks.
[24:28] Today we will cover the one which are
[24:30] required to automate the below
[24:32] infrastructure. Like this is what we
[24:34] have created, right? So,
[24:36] we will just cover the blocks which are
[24:38] sufficient to create this. In future, we
[24:40] will
[24:42] add more
[24:44] and we will write its Terraform also.
[24:46] And if required,
[24:48] new blocks need to be covered, we will
[24:49] cover that. Gradually.
[24:52] Okay?
[24:53] So, the first
[24:55] the first important is Terraform block.
[24:58] It helps to configure Terraform itself.
[25:02] It does not create any resource, just
[25:04] configure Terraform setting.
[25:06] So now there, if you see, this is the
[25:08] block I have written. Terraform
[25:11] This is you can say that a keyword.
[25:14] The block you are started.
[25:16] Now within this block, we set up two
[25:17] things. First, required underscore
[25:20] version
[25:21] greater than equals to 1.0.
[25:23] Which version of Terraform core you
[25:26] need? See, this is our architecture we
[25:29] have covered, right?
[25:30] So you can say that required version is
[25:32] like you are decide the Terraform core.
[25:35] This provider plugin interface is a part
[25:37] of Terraform core itself. Okay?
[25:40] So which version?
[25:43] There are different versions of
[25:44] Terraform core present, right? So you
[25:46] are just saying that, "Hey, I want
[25:47] greater than equals to 1.0."
[25:49] So required version for the Terraform.
[25:53] And more specifically, Terraform core,
[25:55] which version you want?
[25:58] The second
[25:59] required providers. You know that you
[26:02] can have multiple provider like Azure,
[26:04] AWS, GCP. Which is the required
[26:06] provider? Like more specifically, you
[26:09] are talking about this too.
[26:11] Provider plugin, which provider plugin
[26:14] and its SDK. Okay? You have to download
[26:17] that, right?
[26:18] So
[26:20] I want AWS equals to. So you can say
[26:22] that it's a keyword. You can't just have
[26:24] anything.
[26:26] So I want AWS provider plugin.
[26:30] Okay, what is the version? Greater than
[26:32] 5.0.
[26:33] From where I have to download?
[26:36] HashiCorp/AWS.
[26:38] You can download this plugin from here.
[26:40] Terraform is developed by this company
[26:42] itself, HashiCorp.
[26:43] Okay? So they provide uh resources where
[26:47] you can download these plugins. So in
[26:49] the short the second part required
[26:52] provider is we are telling first that we
[26:53] want AWS provider plugin like this.
[26:57] And after that, okay, what version of
[26:58] it?
[26:59] And from where I can download it.
[27:02] So now, what we have specified in the
[27:05] first Terraform block is like
[27:08] this. In this architecture what
[27:10] Terraform core we have to download which
[27:13] version?
[27:15] AWS provider plugin, which version?
[27:18] Okay, SDK is embedded into this provider
[27:21] plugin itself. So we don't have to
[27:22] specifically tell for SDK
[27:24] whatever the version of provider plugin
[27:26] you will download it for the specific
[27:27] either Azure or AWS. Their specific SDK
[27:30] is embedded into that.
[27:32] So this is clear, the first block.
[27:35] We are configuring Terraform itself.
[27:39] The second is the provider block.
[27:41] Provider block in the sense is that
[27:44] now we are working with the specific AWS
[27:47] provider block.
[27:49] In this block we can configure the
[27:51] region in which we want to create the
[27:53] resource.
[27:54] Or more specifically you can say that
[27:56] how
[27:58] we can
[28:02] connect with
[28:03] provider.
[28:06] That's defined by the provider block.
[28:09] So now here provider, this is the
[28:10] keyword you can say provider block.
[28:13] And your provider AWS. Again, we can't
[28:16] use any name. This is you can say that
[28:19] like if you are using AWS provider
[28:21] the same we have to use here.
[28:24] Provider AWS.
[28:25] Now inside this we have like a region.
[28:29] Okay?
[28:30] Which region
[28:31] you have to use
[28:33] in this provider.
[28:34] It also has
[28:36] fields like access key.
[28:40] Here you can provide the access key.
[28:42] It also has like secret. You can provide
[28:45] the secret. But, generally that is not
[28:50] encouraged or recommended.
[28:53] Never provide access key and your token
[28:56] here into this provider itself.
[29:00] We can and it has the highest priority.
[29:03] Like if you provide it here, it will
[29:05] always read your token from here itself.
[29:07] But, it's never recommended to add it
[29:09] here because this files this .tf files
[29:13] are generally pushed to get and then
[29:15] your token might get leaked.
[29:18] So, that's why I haven't mentioned here,
[29:19] but I just uh theoretically mentioning
[29:21] here that apart from region, you can
[29:24] also add your access key, your secret
[29:26] key here itself within that provider AWS
[29:30] itself. Because provider block tells
[29:32] that how we can connect with the
[29:34] provider itself.
[29:36] And let's say for AWS specific,
[29:39] what it need to connect to AWS? Your
[29:42] secret key
[29:44] and the region.
[29:46] Because in the region,
[29:48] let's say US East -1, the endpoint which
[29:51] it create is specific to region only.
[29:54] US.East.1.AWS.Amazon.
[29:58] So, the end URL which it create is very
[30:01] specific to region also.
[30:03] So, that's why to connect to the AWS
[30:07] or provider, this provider blocks comes
[30:09] into the picture. And for AWS
[30:12] provider AWS, we need region, we need
[30:15] token
[30:16] to connect. So, region we have provided,
[30:19] token we can also provide here, but it's
[30:21] not recommended. Later I will show you
[30:24] how to provide the token safely.
[30:28] Okay? But, in short,
[30:30] we can configure the region in which we
[30:32] want to create the resource, but it does
[30:33] not create any resource as of now.
[30:36] So, this provider block is like, okay,
[30:39] you want till now you have configured
[30:40] that you want AWS plugin, but tell me
[30:43] how to connect to AWS. Means your
[30:46] password it is wanted in the region.
[30:49] Okay, so password we haven't provided.
[30:50] You are access key and the secret all I
[30:52] have provided region.
[30:54] During run time
[30:55] I will provide these tokens.
[30:58] Okay?
[31:01] Now, the very important part is the
[31:03] resource block. Now, everything your
[31:05] VPC, your subnet, your net gateway, your
[31:09] internet gateway, your route table is
[31:11] all but nothing but a resource
[31:14] in the AWS. So, that's why the resource
[31:16] block.
[31:18] And now when we are using the resource
[31:20] block, read it like many time we read it
[31:22] like create create.
[31:24] So, don't read it like create VPN,
[31:27] create VPC, create subnet, create net.
[31:29] No.
[31:30] Read it like I want this resource to
[31:32] exist.
[31:33] There is a minor difference. When you
[31:35] read it let's say if I have given
[31:36] resource VPC and if you read it like
[31:39] create VPC, in the mindset you have like
[31:41] it will always create. But I told you
[31:44] it has a step a state manager step where
[31:47] it check
[31:48] if the same resource is all already
[31:50] present with same configuration, it
[31:52] won't create it.
[31:54] So,
[31:55] the right way to read it read it as
[31:58] I want this resource to exist. It will
[32:00] make sure that it exist if you have
[32:02] written.
[32:03] Okay?
[32:05] So,
[32:06] this is an example. Resource is a
[32:08] keyword you have to use it.
[32:11] Then which resource you want to create?
[32:13] AWS underscore VPC. Again, you can't use
[32:16] any name in this.
[32:18] What is the name you want to give to
[32:20] this resource? Or you can say that
[32:22] not resource, you can say the block.
[32:24] This block, how you want to
[32:26] call this block? Like a function name.
[32:29] You can say that this is a function and
[32:30] this is a function name.
[32:32] So,
[32:33] I can call it like the main.
[32:36] Okay?
[32:36] And then within that, these are the
[32:38] arguments.
[32:41] Okay? So, resource is a keyword.
[32:44] We are creating a resource.
[32:46] Terraform knows that this is the
[32:47] resource we are going to create.
[32:49] Which resource?
[32:52] Okay?
[32:52] And within this, let's say I we want to
[32:54] create AWS_VPC resource.
[32:58] The custom name you want to give to this
[33:00] block.
[33:01] Okay?
[33:02] And after that, the arguments. Like,
[33:04] okay, to create the VPC,
[33:07] there are certain arguments which are
[33:08] mandatory. You have to pass that.
[33:12] Now, the question comes is that okay,
[33:14] Shreyansh, how do I know that what all
[33:16] arguments are mandatory?
[33:18] And how do I know that if I know if I
[33:21] have to create a VPC, I have to use
[33:23] AWS_VPC.
[33:24] Why can't it be just VPC?
[33:26] How do I know? Because these are like we
[33:28] can't use any name, right?
[33:30] So, see.
[33:33] There is a
[33:34] You can use this link.
[33:36] Terraform registry website.
[33:38] If you open this,
[33:40] okay? If you open this, you will see the
[33:43] resources here. Let's open this one.
[33:50] Okay?
[33:52] Let me copy it a bit.
[33:57] Okay? So, now here if you see,
[34:01] let me zoom it.
[34:05] Resources.
[34:06] Go down, down. J
[34:09] O
[34:16] Hm.
[34:18] This is it.
[34:19] Oh.
[34:21] Resources. VPC resources. Now here if
[34:23] you see, what all resources we have to
[34:25] create. So, you will find everything
[34:27] here.
[34:28] AWS_Net_Gateway.
[34:30] Okay? AWS_Route route table association,
[34:34] subnet, VPC. Now, let's say I create on
[34:37] AWS VPC. Okay, so AWS VPC, it you can
[34:41] see the example also.
[34:43] Here, if you see
[34:44] argument reference, like what all
[34:46] arguments you can pass, like what are
[34:48] optional
[34:49] CIDR BR optional, CIDR BR CIDR block
[34:52] optional.
[34:54] Which one? Like you can get to know like
[34:56] which are mandatory, which are optional.
[34:58] Like here
[35:00] all these things. Then you will see
[35:01] attribute reference, like what it can
[35:04] once you create it, an output what you
[35:06] will get. You will get ARN number, ID.
[35:09] Okay, so this all thing you will get.
[35:12] So
[35:13] we have we don't have to remember this
[35:15] like what all name. We don't have to
[35:17] remember what are the arguments we can
[35:18] pass.
[35:19] We can make use of this Terraform
[35:21] registry and know that, okay, we want to
[35:24] create VPC, okay, this is something AWS
[35:27] VPC. Go inside this, you can know
[35:30] that, okay, these are the arguments I
[35:32] can pass, this is the thing which I can
[35:34] get in the response.
[35:37] Okay?
[35:39] Now
[35:41] this one I have already told you that
[35:43] resource do not create these resources,
[35:45] it just says that resource should exist.
[35:48] That's what I have already told you that
[35:49] a state manager does this.
[35:51] So this is clear. I am not telling you
[35:54] like creating each resource like VPC,
[35:56] subnet. Now, I will show you in the next
[35:58] part when we will design our network,
[36:00] the whole network infrastructure through
[36:03] Terraform. At this point of time, you
[36:05] should just know that resource block is
[36:07] through which we can create these
[36:08] resources and resource name and
[36:12] arguments and the return type, we know
[36:15] from this registry itself.
[36:17] The last is block which I want to cover
[36:19] is the variable block.
[36:21] So variable block is like here, if you
[36:23] see till now
[36:25] the provider
[36:26] Okay, how to connect with the AWS? You
[36:28] can say that. The provider AWS, we have
[36:31] given the region
[36:32] US hyphen East hyphen 1.
[36:35] This is the value for this argument.
[36:37] Similarly, let's say while creating the
[36:39] subnet. So, the so subnet is a resource.
[36:42] So, resource AWS underscore subnet
[36:45] the name which you want to give to this
[36:47] block. Okay?
[36:49] It also required region argument. So,
[36:52] you have to use this. Similarly, private
[36:54] subnet I'm giving another
[36:57] AWS underscore subnet, but this one I'm
[36:59] giving the name private.
[37:01] And I want into the same region.
[37:04] All I wanted to tell that here if you
[37:06] see the value the same value is used in
[37:09] multiple blocks. Let's say in future you
[37:12] have to change from US hyphen East
[37:14] hyphen 1 to AP hyphen South hyphen 1.
[37:17] You have to change this value.
[37:19] So, you have to change in this block,
[37:20] this block, this block. And possible
[37:23] that all this block present into dot
[37:26] different TF file. I told you either you
[37:28] can have in the same TF file or
[37:30] different TF files.
[37:32] So, you have to make sure that you
[37:33] change this value all the places where
[37:36] it is used.
[37:37] And it is an error prone.
[37:39] That's where the variable block comes
[37:41] into the picture.
[37:42] So, all this value you can give certain
[37:46] variable, assign to that variable, and
[37:49] now in this one you can use that
[37:50] variable.
[37:51] Like this.
[37:53] So, this is again a dot TF file or
[37:55] within the same it's just like a
[37:56] variable block. Variable
[37:59] give any variable name you wanted. Okay?
[38:02] Any name. I just given AWS underscore
[38:04] region.
[38:05] Now, within this you can provide
[38:07] description, which is an optional.
[38:09] Now, you can have like type. Yes, this
[38:13] is an a string. This variable is a
[38:14] string. Type equals to a string. Default
[38:17] value is like US hyphen East hyphen 1.
[38:19] If you want to override,
[38:22] provide
[38:23] here itself. The reason equals to AP
[38:25] hyphen South hyphen 1.
[38:27] Okay?
[38:28] Otherwise, you can make use of this
[38:30] variable. So, I have created this
[38:32] variable.
[38:34] Type is a string. The value is US hyphen
[38:36] East hyphen 1. Default equals to US
[38:38] hyphen East hyphen 1. Now, here, if you
[38:40] see, in the provider, I am using
[38:43] var. So, it tells Terraform that look
[38:46] into the variable
[38:48] block.
[38:50] var.
[38:51] This is the variable name, AWS
[38:53] underscore region.
[38:55] Similarly, your resource, AWS subnet,
[38:58] region equals to var. AWS underscore
[39:01] region.
[39:03] Okay? So, now they are all using this
[39:04] variable, not using this value directly.
[39:08] Now, in future, if you have to change
[39:09] it, just change it here, only at one
[39:11] place. You don't have to change in all
[39:13] the block.
[39:15] Clear? So, that's where variable blocks
[39:18] are very handy. And it's very use very
[39:20] much frequently used.
[39:22] So, there are different types like uh
[39:24] you have a string,
[39:26] then you can use number type as number,
[39:28] integer, bool, false, true,
[39:31] list of type, means list of a string.
[39:34] Okay? Or like list of number, you can
[39:36] give. And then you have a
[39:39] like this.
[39:40] Like an array, and then you then you
[39:42] have a set of type, set of a string.
[39:45] It just remove duplicate, but ordering
[39:47] is not maintained.
[39:49] Map of type, like map of a string, like
[39:51] this, you can give key value pair within
[39:53] a block.
[39:54] So, these are the different types you
[39:56] can have, and like how their value
[39:59] you can create.
[40:02] So, with this, I have
[40:05] created a basic understanding of
[40:07] Terraform.
[40:08] And now, based on this, we can write
[40:10] whatever the infrastructure we have
[40:12] created, we can create our own and we
[40:14] Terraform files, and using that
[40:16] Terraform files, we will run it and
[40:18] create an infrastructure using that.
[40:20] Not through manually. See you in the
[40:22] next video.
[40:23] Okay, bye.
