How to use terraform blocks

In this blog, we are going to learn the anatomy and use-cases of the different types of blocks that are provided by terraform. namely the following
1. resource
2. variable
3. output

1. Resource block

The resource block is the core of terraform design patterns. with these, you will be able to create the resources. The below picture shows how to define/use the resource block.

resource block definition.

On the left, you can see the blueprint of a resource block. The keyword resource is the word that defines this block as a resource. Then the resource type is something defined by the terraform which will have two parts, provider name, and resource type.



For example, you have to create a resource group in azure the provider is azurerm and the resource type is “resource group”. Hence you can use an “azurerm_resource_group” as the resource type. Then the resource_block_name is meant for internal coding purposes. for example, In case you want more than one resource group then you need to have similar blocks but you can give your own respective resource block name so that you can differentiate resource blocks related to different resource groups. Then within the curly braces, you can give the respective parameters or configuration values in a key-value pair manner which are the values used to create a resource group. The below example will help you understand how to create a resource group in azure.

resource group in azure

2. Variable block

Variables are one of the most fundamental design patterns in any of the programming languages, similar here in terraform. we have variables that can be used as a placeholder for the values that you can give during the time of execution. Various ways of giving input to a variable is a topic for another blog. These are mainly given importance as placeholder and re-usability. variable block in terraform is defined as below.

variable block definition.

Here variable is the keyword that tells terraform to create a variable, similar to the resource keyword. here it only takes one thing which is the name of the variable which should be given by you. The variable block accepts three arguments namely type, description, and default.


Most importantly all the three are optional. Then how does it works, that’s a topic for another blog. There are various data types provided by terraform eg: string, number, bool, and any etc. you can use it based on the use-case. Then the description is just a string that can be used for the readability of code so that others can easily understand how the variable is being used. default accepts the value of the string since it is optional you can remove this line code in which case terraform will ask you on terminal for the value of the variable during execution.

3. Output

If you are aware of any other programming languages, all others have a concept called printing to the terminal like printf in C, console.log in JavaScript, and System.out.println in java. Here in terraform, it is an output block. Mainly it is being used in two scenarios, one is to print the values required to the terminal and the other is to take one attribute of a module to reference in another module. The below block defines an output.

Output block definition.

By now you would have understood there is a keyword attached to every block, here the keyword is the output.

output name is the name of the output so that you can differentiate various outputs among your terraform configuration files. it only accepts one key-value pair as an argument that is the value, In the above block, I have given the value to be as the resource group’s id which we have created with the resource block at the start. so this block will print the value of the resource group id to the terminal after execution.

Thanks for reading your way through, Hope you understand the basics of Terraform and the different blocks used in it. Please feel free to share your comments or doubts in the comment section below.

4 comments

  1. Itís hard to come by knowledgeable people for this subject, but you sound like you know what youíre talking about! Thanks

Leave a comment

Your email address will not be published. Required fields are marked *