If you are planning to automate your cloud infrastructure then nothing could be better than Terraform. Terraform is the most widely used and popular automation (infrastructure as code) tool that allows you to build and manage infrastructure easily and efficiently.
In this Terraform series, you are going to learn from the ground up what Terraform is and what you need to know to manage and build your infrastructure using Terraform.
This article will be a step by step guide. If you would like to follow along, be sure to have the following in place:
Terraform is a tool for building, versioning, and updating the infrastructure. It is written in GO Language and the syntax language of configuration files is HCL i.e HashiCorp Configuration Language(much easier than YAML or JSON).
Terraform has been in use for quite a while now. It is an amazing tool to build and change the infrastructure in a very effective and simpler way. It can be used with a variety of cloud providers such as Amazon AWS, Oracle, Microsoft Azure, Google Cloud, and many more. Let's learn more about it.
Terraform as a tool is a big game changer to manage cloud infrastructure because of the ease of writing code which is such that the same code can be easily made to work with various cloud providers. There are several other key features of Terraform which make this tool so popular and handy. Let's discuss a few of them now.
Terraform code consists of Terraform configuration files that follow a standard structure - an arrangement of files and directories that improves code readability. Lets checkout details about these files and folders.
Terraform modules are top-level directories. A single module contains mainly five Terraform configuration files: main.tf, vars.tf, providers.tf, output.tf and terraform.tfvars which are written with .tf format or .tf.json or .tfvars format. These are known as root modules.
The Root module can call other child modules from either local directories or Terraform Registry or from anywhere in the disk
As you can see in the below image there are two child modules - AWS EC2 and AWS S3. Modules containing all the required configurations(main.tf, vars.tf, provider.tf, terraform.tfvars etc) are the ROOT modules for themselves but also acts as child module for the other ROOT module that contains both EC2 and AWS S3.
Now, let's dive into five configuration files that a root module requires.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/fb8c47ee2b74a984921e024348fe6fa2.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/f62b400dc760c7b8a3fa9a4364c266d5.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/71ce72316d47fc6d663246d7f8aa5823.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/d407411d6a3d16dcc4b092c56cad6038.js</p>
Previously you learned about the theoretical aspects of what Terraform is and how it works. But before you actually write a Terraform configuration file to create a resource, it is important to know the syntax.
With Terraform, you can simply create the resources using resource blocks or by creating a module (A module is a container for multiple resources that are used together). Let's checkout the syntax.
The below code is a resource block in Terraform where:
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/37b57f4d33fb25e3d96fdaf80656a1d5.js</p>
The Below code is a module block in Terraform where:
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/c52cacfdff90cf98cea0f244d5161064.js</p>
Terraform's code configuration files are stored in plain text as .tf format or in JSON based formats as .tf.json.
Lets quickly look at the .tf format configuration file where resource type is "aws_instance" containing two arguments: 'instance_type' and 'ami'.
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/3a1d27df4d70b9d305e3a227b2c1af0e.js</p>
Next, some of the Terraform files are JSON compatible, suitable for nested blocks and must have .tf.json format syntax. Let’s check out how the terraform code is declared in the .tf format.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/f98b82adda4b8c4c751f66dc17a1cdb6.js</p>
Well, Now you have a sound theoretical knowledge of what Terraform is, its configuration file structure and how they are declared. But before jumping in to learn and use Terraform, you must have terraform installed on your machine.
Installing Terraform on Ubuntu
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/e773935ddccff3b17bcec76a7993ff15.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/0575dc989b11264865390ccc1135c96e.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/a9b62eba7649ac3edc60e9f5670ea720.js</p>
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/1562113ed9424020c3a39548c4898ef0.js</p>
<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/352692a51ce7a2a2b5678eba12cb8e84.js</p>
Terraform backend is basically a location where terraform's state file resides. This state file has all the resource details and tracking which were provisioned or will be provisioned via terraform plan or apply command.
There are two types of Backends,
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/8ba8d2fd0df15c5ebb1cf2a07efce1de.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/7c849ead415879b9d11cf26e39860d21.js</p>
In this tutorial you learnt the basics of Terraform and what you need to create or manage your infrastructure using Terraform.
This is the first blog in our 2-part blog series on Terraform. If you wish to continue reading about Terraform, and how multiple instances can be deployed in one go using Terraform, then click here.