Infrastructure as Code provides a convenient way to manage and automate deployments to cloud infrastructure. In this blog, learn how Terraform can be used to perform Infrastructure as Code.
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.
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.
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/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,
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.
Squadcast is an incident management tool that’s purpose-built for SRE. Your team can get rid of unwanted alerts, receive relevant notifications, work in collaboration using the virtual incident war rooms, and use automated tools like runbooks to eliminate toil.