IAAC — Terraform Fundamentals Part2

aditya goel
7 min readAug 7, 2022

--

If you are landing here directly, you are requested to first read through this blog. Here in this part, we shall be looking majorly through following concepts :-

  • Introduction to variables in Terraform.
  • Usage of String type of variable with Terraform.
  • Usage of Number type of variable with Terraform.
  • Usage of Bool type of variables with Terraform.
  • Usage of List type of variable with Terraform.
  • Usage of Tuple type of variable with Terraform.
  • Usage of Map type of variable with Terraform.
  • Usage of (local_provider + random_provider) plugins together with Terraform.

Question:- Which all variables are supported by Terraform ?

Question:- Demonstrate how the variables are being referred in the main file?

Question:- Demonstrate use of variable of String Type through use of Terraform ?

Step #1.) terraform init → Note the imp points :-

  • Here is how the usage of variables being defined for filename and content.
  • ResourceType being used here is “local_file”.
  • ResourceName being used here is “sample_res”.

Step #2.) terraform plan → Note the imp points :- Here is how we have defined two variables, which were used in Step #1 above :-

  • fileName1 → This variable defines the name of the file.
  • contentStringTypeVar → This variable defines the content to be put into the file.

Step #3.) terraform apply → Note the imp points :- As soon as we execute the ‘terraform apply’ command, the resource gets created. (In this case, a local file).

Question:- Demonstrate use of variable of Integer Type through use of Terraform ?

Step #1.) Let’s now use the another variable. Note that, this variable is of type Number.

Step #2.) Below shown is how we have defined the variable, which has been used above in step #1. See below file that, we have also changed the name of the file too.

Step #3.) Now, we go ahead and apply the terraform and we can see that :-

  • The new file named “newFIleThruVar.txt” has been generated and old file has been auto-deleted.
  • Also, see the contents of the file now contains the number, as suggested in Step #1.

Question:- Demonstrate use of variable of Bool Type through use of Terraform ?

Step #1.) Let’s now use the another variable named “contentBoolTypeVar”. Note that, this variable is of type Boolean.

Step #2.) Let’s now see the variable being used above in step #1 :-

Step #3.) Let’s now go ahead and execute “terraform apply” command in terminal. We can see that, contents of the file has been changed automatically :-

Question:- Demonstrate use of variable of List Type through use of Terraform ?

Step #1.) Let’s now use the another variable named “contentListTypeVar”. Note that, this variable is List-Type.

Step #2.) Let’s now see the usage of the aforementioned variable in defining our main resource. Note that, we are specifically using the element present at Index2 from the list :-

Step #3.) Let’s now go ahead and execute “terraform apply” command in terminal. We can see that, contents of the file has been changed automatically :-

Question:- Demonstrate use of variable of Tuple Type through use of Terraform ?

Step #1.) Let’s now use the another variable named “contentTupleTypeVar”. Note that, this variable is Tuple-Type.

Step #2.) Let’s now see the usage of the aforementioned variable in defining our main resource. Note that, we are specifically using the element present at Index1 from the tuple :-

Step #3.) Let’s now see the plan through terraform :-

Step #4.) Let’s now go ahead and execute “terraform apply” command in terminal and observe that

Step #5.) We can see that, contents of the file has been changed automatically :-

Question:- Demonstrate use of variable of Map Type through use of Terraform ?

Step #1.) Let’s now use the another variable named “contentMapTypeVar”. Note that, this variable is of Map-Type. Also note that, we have changed the name of the file as well.

Step #2.) Let’s now see the usage of the aforementioned variable in defining our main resource. Note that, we are specifically using the element with key as “name” :-

Step #3.) Let’s now execute the command : “terraform plan”, which shall show us the resources involved into the process :-

Step #4.) Let’s now go ahead and execute “terraform apply” command in terminal :-

Step #5.) We can see that, name of the file as well as contents of the file has been changed automatically. Also note that, under providers/plugins, an local_provider plugin has been downloaded too :-

Question:- Demonstrate use of local_provider + random_provider through use of Terraform along with custom version of “Random Provider” terraform plugin ?

Step #1.) Let’s now use the two resources :-

  • random_string → This resourceType comes from “random provider” plugin.
  • local_file → This resourceType comes from “local provider” plugin. Also, this resource refers to the resource of random_string type.

Step #2.) Let’s now define the variables being used above in another file :-

Step #3.) Let’s now execute the command : “terraform init”. This shall download all the required plugins and does the basic setup :-

Step #4.) Let’s now execute the command : “terraform plan”. This shall show all the resources, which shall be created later on :-

Step #5.) Let’s now go ahead and execute “terraform apply” command in terminal. This shall actually perform the resource creation :-

Step #6.) We can now verify that, output file has been created very much, as expected :-

Step #7.) Let’s now specify the custom version for the “Random Provider” terraform plugin. Note here that, we have explicitly specified the version as 3.3.0.

Pl note that, as of writing this blog currently, the latest version available for this “Random Provider” terraform plugin is 3.3.2 :-

Step #8.) We would again have to initialise the terraform now :-

Step #9.) Let’s now apply the terraform execution plan :-

Step #10.) Let’s see here the plugins, that have been downloaded now. Pay attention that, version 3.3.0 has been downloaded now :-

Question:- What are the various LifeCycle methods available in Terraform ?

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next part of this series with HashiCorp Terraform.

References :-

--

--

aditya goel
aditya goel

Written by aditya goel

Software Engineer for Big Data distributed systems

No responses yet