Terraform Variable Types

Terraform Variable Types

Let’s go through some basic concepts about Terraform Variable Types. Like any language or framework, we need to learn how to work with the core variables of any language. And because we use variables to store any data, we also need to classify them according to the data type.

The Types of variables on Terraform follow the same idea and scope for other languages. However, using variables on Terraform provides us great flexibility to build our code very dynamically, to adapt to any scenario.

Before deep diving into Terraform Variable Types, let’s see how to declare them.

Terraform Variable Block

To declare variables in Terraform, we need to use the Variable block, regardless of the variable’s type.

variable "db_host" {}

Above is a simple Variable block. There are no required attributes. Also, we don’t need to specify a default value or the variable type. The Terraform can detect the type of variable.

For example, if you don’t specify the value for “db_host”, we must provide a value at some point. Like the example below:

terraform apply -var db_host="db.www.bitslovers.com"

Terraform String

Working with String on Terraform it’s pretty easy and natural. Without secret. To use String on Terraform, we just need to declare a variable block:

variable "web_service_protocol" {
   type = string
   default = "https://"

From the example above, you noticed that we use double quotes, and we provide the type of that variable (not required).

Heredoc Syntax

There is another way to work with String. Sometimes, we need to define a big String with multiple lines and spaces. Let’s see one example using the heredoc syntax:

variable "policy" {
   type = "string"
   default = POLICY
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "*",
    "Resource": "*"


To represent numbers on Terraform, we use the type numeric, which is also optional.

variable "db_port" {
   type = numeric
   default = 5432


To represent true or false values, we can use the Boolean type—for example, one variable to enable or not a verbose mode for some process.

variable "enable_debug" {
  default = false

We didn’t specify the variable type in the example above, and it works perfectly. Also, we can override the “enable_debug” value at any moment.


terarform apply -var enable_debug="true"

The Terraform Variable Type Boolean supports another approach to specify “true” or “false.”

We can specify zero “0” with double quotes, and the Terraform will automatically convert it to false. The same idea happens for “true,” we can specify “1,” and it will be converted to “true.”


Map it’s another Terraform Variable Type. The typical example of usage for a map variable is to declare the AWS AMI, for example, by region. It’s much better than declaring one variable String for each region. So, we can declare one variable that holds all possible combinations of AMI and Regions at the same variable.

variable "images_map" {
   type = "map"
   default = {
	"ap-east-1"  = "ami-0b59bfac6be064b78"
    "us-west-1" = "ami-e7768380"


Another powerful Terraform Variable Type is the List. It’s a sequence of values. And each value contains one index. And the first element of this List is zero.

variable "aws_zones" {
  type = "list"
  default = ["us-east-1a", "us-west-1c"]

So, from the example above:

aws_zones[0] contains the value “us-east-1a”. and the aws_zones[1] holds the value “us-west-1c”.

Terraform Complex Variables

We can use advanced techniques from Program Language and create our Objects.

Let’s suppose that we need to store the information regarding the configuration from a database, like the ports we need to use to connect to it externally and internally.

So we need this object:

db_config = object({
    external = number
    internal = number
    protocol = string

We can do something more complex, for example, if we have a list of configurations:

For example:

variable "db_port" {
  type = list(object({
    external = number
    internal = number
    protocol = string
  default = [
      external = 5432
      internal = 5433
      protocol = "tcp"

In our example above, we are creating a list of objects on Terraform.

We can also mix and merge multiple Terraform Variable Types, like List, Map, and Object.

variable "block_device_mappings" {
  description = "Specify volumes to attach to the instance besides the volumes specified by the AMI"
  type = list(object({
    device_name  = string
    no_device    = bool
    virtual_name = string
    ebs = object({
      delete_on_termination = bool
      encrypted             = bool
      iops                  = number
      kms_key_id            = string
      snapshot_id           = string
      volume_size           = number
      volume_type           = string
  default = []

This last example shows that it is possible to declare a List of Objects on Terraform. The objects could also contain other objects on them. For instance, we have one attribute on “block_device_mappings” called “ebs” that also it’s an object.

Boost your Terraform skills:

How to use Terraform Modules

How to create complex expressions using Terraform Template.

What is the difference between Locals and Terraform Variables?

How to use the Terraform Data on your modules.

What are the advantages of Terraform Output?

Learn how to create multiple copies from the same resource on Terraform.

How to create a Pipeline on Gitlab to execute a Terraform code?


In this article, you have learned all Terraform Variable Types: String, Numeric, Boolean, Map, and List. The Terraform variables are a powerful feature to help us reuse values without repeating them in all locations that we need them.

Also, be aware that it’s possible to build Object and List of Objects, giving us more alternatives to make our configuration more robust and easy to maintain.

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.