IAAC — Introduction to Terraform

In this blog, we shall be looking majorly on following two concepts :-

  • Traditional IT Workflows.

Question:- How does traditional IT workflow looks like ?

Answer:- Here is how the application development lifecycle works in a traditional IT setup :-

  • First thing is business always create a requirement of very, very high level requirement, detailing about the features that needs to be added to the application.

Question:- What are the problems with company’s own data-center ?

Answer:- Following can be probable issues in owning an own data-centre :-

  • This flow has a very, very slow deployment cycle.

Question:- What’s the solution to overcome the aforementioned problem ?

Answer:-

  • Going to a cloud based infrastructure can probably solve these problems. In cloud, this resource provisioning is very much fast.

Question:- How do we interact with the cloud ?

Answer:- There are two options for the same :-

  • Either you can use website like a cloud console or a portal through which you can interact with, but it is good enough if you are managing a limited resource. For example, let’s say you are just managing five different little machines. That’s good enough.
  • But let’s say you need to manage thousands of different resources. In that case, this click based approach will not be a good solution. So, there is another way to interact with our public cloud i.e. programmatically through Application Programming Interface (API).

Question:- What are available tools for Infrastructure Provisioning ?

Answer:- There are many tools available for infrastructure provisioning inside the cloud :-

  • Docker

Question:- What can be the easy way to do Infrastructure Provisioning ?

Answer:- By using shell-script, we can provision infrastructure too, for example, creating a virtual machine. So, that is nothing but you are creating infrastructure as a code, but writing and maintaining such a code inside the shell script or Python script will be a very much tedious task.

And that’s why we need some more robust language to work with this.

Question:- What do we mean by Infrastructure Provisioning ?

Answer:- For example, say we wanted to perform below mentioned steps into the Google Cloud Environment :-

Note: Above steps can be very well scripted through Shell-script or Python.

Question:- What’s Terraform ?

Answer:-

  • TerraForm is one of the most popular tools currently available in the market for infrastructure provisioning.

Question:- Does Terraform provides support for multiple clouds as well ?

Answer:-

  • Terraform does provides support for different cloud- vendors like AWS or Azure or GCP.

Question:- What can be the possible things, that we can perform through Terraform ?

Answer:- Following can be the possible things, that we can perform through Terraform :-

  • Network provisioning.

Here is a sample script for Terraform :-

Question:- What are other differences between Infra Provisioning through Python Vs Infra Provisioning through HCL ?

Answer:-

1.) Let’s say you write a script in a python or something in a shell script to create some virtual machine inside the Google Cloud platform. You executed the script three times. So, definitely resources shall be created for 3 times and it is going to cost you three times.

2.) In contrast, with HCL script for TerraForm, It will only going to create one resource even if you run this script three times, so it will not cost you three times and It is just going to create one resource and it will cost you for one single resource only.

Question:- Does Cloud Providers provide any native tools, as well for Infra Provisioning and where does Terraform fits in ?

Answer:- A lot of native tools are available inside the public cloud for creating infrastructure. For example :-

  • Note that, whatever you learn for Google Cloud’s Deployment Manager will not be applicable to the AWS’s Cloud formation. So, there is no unifying common approach.

So, that is the basic idea behind the terraform. TerraForm is not a native solution. It’s a cloud agnostic solution.

Question:- Can you explain the Terraform Workflow ?

Answer:- Here is how the sample workflow with Terraform looks like :-

  • Scoping → To identify the infrastructure for your project i.e. what are the different resources you are going to create inside the infrastructure?

Question:- Can you share some examples for scoping ?

Answer:-

  • Creating a virtual machine inside the Google Cloud platform.

Question:- Can you show the code for creating a simple local file using TF ?

Answer:- We are going to create this sample file and writing some content inside it.

  • resource → This resource is nothing but a block. We are going to create the file-name as mentioned in resource block

Question :- What types of resources can we create through a local provider ?

Answer:- We can only create resource of type local_file through the use of local provider.

Question:- Can you explain init, plan and apply steps of the Terraform Workflow ?

Answer:- Following are the details for each of the steps :-

1.) init → This is the very first command.

  • We need to execute after writing our configuration file.

Note :- Above step also creates an additional file named “.terraform.lock.hcl”, which contains details about all the plugins that has been downloaded so far.

2.) plan → This step creates execution plan.

  • It doesn’t mean any infrastructure, but it will tell us that what are the necessary change you are going to apply?

3.) apply → This step will execute all the changes provisioned resources in a specified configuration file.

On the left hand side, you can see there is a two more file gets created :-

  • One file which we actually want to create i.e. sample.txt.

Question:- What are various arguments with the local_file resource ?

Question:- Demonstrate the use of file_permission argument for the file ?

Step #1.) Note here that, we have specified to create a new file with name as “newFile.txt” and permission as 0700. We go ahead and init with terraform.

Step #2.) Next, we want to see the plan with terraform. Note below that, one create option is there, which is suggesting that one local file with below shown properties shall be formed.

Step #3.) Next, we wanted to apply with terraform i.e. we wanted to execute the action and as a result of which we see that, our desired resource has been created.

Step #4.) Finally, we can observe that, the permissions of file being created is 700 i.e. :-

  • User have got all full permissions of RWX.

Question:- Demonstrate generation of multiple resources through use of Terraform ?

Step #1.) terraform init → Note here that, we have specified to create two new files with following names :-

  • cat.txt

Note :- Above step also creates an additional file named “.terraform.lock.hcl”, which contains details about all the plugins that has been downloaded so far.

Step #2.) plan → This step creates execution plan and would tell us that what are the different resources we are going to create ?

Step #3.) apply → This step will execute all the changes provisioned resources in a specified configuration file.

On the left hand side, you can see that, our requested files have been created.

Question:- Demonstrate use of random-provider through use of Terraform ?

Answer:- Terraform provides the “random-provider” plugin.

It provides the variety of resources, which can be generated using TF :-

Question:- Demonstrate use of random_integer resource through use of Terraform ?

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

  • Range has been pre-defined i.e. minimum and maximum from which random integer could be generated.

Step #2.) plan →

Step #3.) apply → Observe the output of 7840, has been generated below :-

Also, we can investigate the tfstate file :-

Question:- Demonstrate use of random_string resource through use of Terraform ?

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

  • length has been defined upfront and it can very well contain upper characters.

Step #2.) plan →

Step #3.) apply → Observe the output of 7840, has been generated below :-

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 :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store