Terraform - Starting From The Basics - Part One

Use Case

This will enable the Infrastructure as Code concept for your team.

Why Terraform?

In my experience, it gives the flexibility to begin with only knowledge of JSON operations, in place of many other tools that demand that you  learn a new script/language for their usage. Also, there aren’t any agent or client/server tools to run separately other than a single simple .exe. Terraform is simple and straightforward and supports OS, Cloud, etc. It is huge and still growing very fast.

Why this article?

It begins with the basics. Many people read multiple articles and still struggle to find a starting point. So, I thought to share the tips I used during my own self-learning. Please follow the steps carefully to start from the basics.

Download Terraform

Use the link https://www.terraform.io/downloads.html to download Terraform. You will get terraform.exe from the downloaded zip file; copy that .exe only to a separate folder. I used the folder path as “D:\JAISH\TERRAFORM”. All of my demos are on Windows 10 even though Terraform supports multiple OS.

Verify the first command in Terraform

Open Windows Command Prompt as an Administrator and change the directory to “D:\JAISH\TERRAFORM”, where terraform.exe has been copied. Now, just type “terraform” as a command and make sure that your screen appears like below. You are now ready to play with this tool.

Terraform

Terraform Configuration

You need the configuration files to pass to terraform.exe. These configuration files are plain text files with ".tf" extension and the contents of these configuration files resemble the JSON format. Normally, one .tf file is kept in a single folder.

What to Write Inside Terraform Configurations

You have multiple pre-defined keywords to use inside Terraform Configuration files. See a full list from here, where you will notice the keywords like provider, resource, module etc. In the below section, we will create our first terraform configuration file using “resource” keyword.

Write your first Terraform Configuration File

Create a “myconfig.tf” under “D:\JAISH\TERRAFORM”. The file can have any name. Copy the below lines into it.

  1. resource "null_resource"  
  2. "example" {  
  3.     provisioner "local-exec" {  
  4.         command = "Get-Process > completed.txt"  
  5.         interpreter = ["PowerShell""-Command"]  
  6.     }  

The lines above are saying that by passing a “null_resource” I don’t want to create any new resource and the term “example” is a variable to represent this entire block. Another keyword, “provisioner,” is used and among provisioners supported by Terraform, I used “local-exec”. A “local-exec” provisioner can run a local .exe or a standard command (Dos, Perl, PowerShell etc.). I mentioned a PowerShell command “Get-Process” above, which will list all locally running processes. I am also routing the command output to a text file “completed.txt”. Please note down that the paths are relative to the Terraform execution path. So, completed.txt would be created inside “D:\JAISH\TERRAFORM” and the file will have the list of all locally running processes.

Run Your First Terraform Operation

Now, we have a small local operation in the above Terraform configuration file, which needs to execute. Terraform execution has mainly 3 commands:

  1. Init – It will download any supportive providers by scanning your configuration file
  2. Plan – This will show you a plan about what Terraform is going to do, as per the Terraform configuration file
  3. Apply – The real execution, as per the Terraform configuration file

Please see the screenshots below on running each of these commands. For the last command, “Apply”, I entered “yes” as a response.

Terraform
Terraform

Terraform

Verify Your Terraform Operation

You can open “D:\JAISH\TERRAFORM\ completed.txt and verify the contents of it.

Terraform

Please practice the activities mentioned in this tutorial. I have attached a .tf file and output file as a reference here. In the next session, I will show you how you can connect to an Azure service and create a resource group in it. Thank you.