Terraform variable types for beginners
Tags: terraform, aws, devops, iac
Published: 2021-12-20 19:17:49

---
title: Terraform variable types for beginners
published: true
date: 2021-08-08 19:30:08 UTC
tags: terraform,aws,devops,iac
canonical_url: https://jamesmiller.blog/terraform-variable-types/
cover_image: https://i0.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/terraformVariables1.jpg
---

Terraform variable types are a core principle of infrastructure as code to understand and are key for configuring your cloud architecture.

In my [previous post I went through the process of how to set up Terraform on Mac,](https://jamesmiller.blog/terraform-aws-mac-setup/) this post will be a part 2 – where I discuss how Terraform variables work.

The last post showed how to set up a basic VPC in Terraform, this this post I’ll show how to create variables and define configurations within the VPC based on those variables.

### Terraform Variable Types Step 1: Create a variables.tf file

In the root of your folder where your other .tf files are, create variables.tf like in the image below and copy the below code into it.

![Terraform Variable Types Root Directory](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-16.06.36.png?resize=640%2C406&ssl=1)
_A screenshot of the root folder from the previous tutorial, with the addition of a variables.tf file._

In that variables.tf file, enter the following variable types:

#### String variable

String variables can be used to define unique unicode configurations / identifiers / names, in the below example – a CIDR block address.

```
// STRING VARIABLE
variable "cidr_block" { // name of the variable
  type = string // type of the variable (in the case string)
  default = "10.0.0.0/16" // value of the string variable, in this case the cidr block details
}
```

#### Number variable

Number variables can be used to define any whole or fractional number such as internal / external port numbers. The below example is not a strong usecase for using this variable – but works as a demonstration.

```
// NUMBER VARIABLE
variable "projectDuration" { // name of the variable
  type = number // type of the variable (in this case number)
  default = 28 // value of the number
}
```

#### Boolean variable

Boolean variables are used to determine whether certain features are enabled within resources.

```
// BOOLEAN VARIABLE
variable "enabled" { // name of the variable
  default = true // value of the variable
}
```

#### List variable

List variables can be used to define different types configurations for a particular such as availability zones and ports. In this case, the different types of instance\_tenancy are outlined as options:

```
// LIST VARIABLE (ARRAY)
variable "instance_tenancy" { // name of the variable
  type = list(string) // type of the variable, set as a list that accepts only string data
  default = ["default", "dedicated", "host"] // string values defined within the list
}
```

#### Map variable

Map variables are useful when defining information that is linked in key value paris – like environments (e.g “dev” = “dev-bucket”). Below is not a good example of this, but it does demonstrate map variables working.

```
// MAP VARIABLE (key value pairs)
variable "assign_generated_ipv6_cidr_block" { // name of the variable
  type = map // type of the variable, that accepts string keys and string values 
  default = {
    "False" = "false"
    "True" = "true"
  }
}
```

#### Tuple variable

Tuple variables are useful when defining a resource with parameters required to set it up (e.g vcn = [“my\_vpc”,”10.0.0.0/16″,false]). Below is not a good example of this, but it does demonstrate tuple variables working.

```
// TUPLE VARIABLE
variable "launchDate" { // name of the variable
  type = tuple([number, string]) // type of the variable, a list that accepts two values, the first being a number and the second being a string 
  default = [4, "September"]
}
```

#### Object variable

Object variables, similar to tuple variables are also useful for defining resources with their configurations – but in a JSON-like format. Below is not a good example of this, but it does demonstrate object variables working.

```
// OBJECT VARIABLE
variable "projectArchitecture" { // name of the variable
  type = object({style = string, resourceNumber = number}) // type of the variable, an object that is expecting a string variable called 'style' and a number variable called 'resourceNumber'
  default = {
    style = "Serverless"
    resourceNumber = 7
  }
}
```

#### Input variable

Input variables are handy when you want to define a resources value at runtime, such as defining the name of a resource.

```
// INPUT VARIABLE
variable "inputname" { // name of the variable
  type = string // type of the input variable, which will be string stype
  description = "Set the name of the VPC" // set the description message that appears as a prompt for when you type an input as the variables value
}
```

#### Output variable

Output variables are great for recording the value of a resource once it has been creaed by Terraform.

```
// OUTPUT VARIABLE
output "vpcid" { // name of the variable
  value = aws_vpc.myfirstvpc.id // the value that you'd like to output once the resource is created
}

// OUTPUT VARIABLE
output "vpcarn" { // name of the variable
  value = aws_vpc.myfirstvpc.arn // the value that you'd like to output once the resource is created
}
```

I’ve put the whole Variables.tf file below, which contains the different ways that variables can be defined within Terraform:

#### Variables.tf

```
// STRING VARIABLE
variable "cidr_block" { // name of the variable
  type = string // type of the variable (in the case string)
  default = "10.0.0.0/16" // value of the string variable, in this case the cidr block details
}

// NUMBER VARIABLE
variable "projectDuration" { // name of the variable
  type = number // type of the variable (in this case number)
  default = 28 // value of the number
}

// BOOLEAN VARIABLE
variable "enabled" { // name of the variable
  default = true // value of the variable
}

// LIST VARIABLE (ARRAY)
variable "instance_tenancy" { // name of the variable
  type = list(string) // type of the variable, set as a list that accepts only string data
  default = ["default", "dedicated", "host"] // string values defined within the list
}

// MAP VARIABLE (key value pairs)
variable "assign_generated_ipv6_cidr_block" { // name of the variable
  type = map // type of the variable, that accepts string keys and string values 
  default = {
    "False" = "false"
    "True" = "true"
  }
}

// TUPLE VARIABLE
variable "launchDate" { // name of the variable
  type = tuple([number, string]) // type of the variable, a list that accepts two values, the first being a number and the second being a string 
  default = [4, "September"]
}

// OBJECT VARIABLE
variable "projectArchitecture" { // name of the variable
  type = object({style = string, resourceNumber = number}) // type of the variable, an object that is expecting a string variable called 'style' and a number variable called 'resourceNumber'
  default = {
    style = "Serverless"
    resourceNumber = 7
  }
}

// INPUT VARIABLE
variable "inputname" { // name of the variable
  type = string // type of the input variable, which will be string stype
  description = "Set the name of the VPC" // set the description message that appears as a prompt for when you type an input as the variables value
}

// OUTPUT VARIABLE
output "vpcid" { // name of the variable
  value = aws_vpc.myfirstvpc.id // the value that you'd like to output once the resource is created
}

// OUTPUT VARIABLE
output "vpcarn" { // name of the variable
  value = aws_vpc.myfirstvpc.arn // the value that you'd like to output once the resource is created
}
```

### <meta http-equiv="content-type" content="text/html; charset=utf-8">Terraform Variable Types Step 2: Update main.tf with data from variables.tf

Open main.tf and copy the below code into it. This will update the vpc resource with the details you’ve defined in variables.tf.

#### main.tf

```
// VPC RESOURCE
resource "aws_vpc" "myfirstvpc" { // title of resource
  cidr_block = var.cidr_block // accessing string variable data
  enable_dns_support = var.enabled // accessing string boolean data
  instance_tenancy = var.instance_tenancy[0] // accessing list variable data
  assign_generated_ipv6_cidr_block = var.assign_generated_ipv6_cidr_block["False"] // accessing map variable data
  tags = {
    Name = var.inputname // accessing input variable data
    ProjectDurationInWeeks = var.projectDuration // accessing number variable data
    LaunchDay = var.launchDate[0] // accessing tuple variable data
    LaunchMonth = var.launchDate[1] // accessing tuple variable data
    ArchitectureType = var.projectArchitecture["style"] // accessing object variable data
    NumberOfResources = var.projectArchitecture["resourceNumber"] // accessing tuple variable data
  }
}

```

### <meta http-equiv="content-type" content="text/html; charset=utf-8">Terraform Variable Types Step 3: Create the terraform resources 

In your terminal window, run this command in the folder that contains your terraform files

```
terraform apply -auto-approve
```

Once the prompt ‘Enter a value:’ appears, type in the a value you’d like (in this case I typed “TEST”).

![Terraform Variable Type: Input Variable](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-19.15.52.png?resize=640%2C100&ssl=1)
_Terminal window that shows the prompt to enter a value, along with the value that I entered: “TEST”_

Once the resource has been created, the two output variables that were defined are logged in the terminal window.

![Terraform Variable's generate VPC](https://i0.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-19.16.02.png?resize=640%2C157&ssl=1)
_The terraform file has been run, the resources have been created and the two output variable values are logged_

In AWS, check your VPC’s and validate that the resource has been created, if the configurations of the VPC should match the terraform file you’ve written – then success!! ![😀](https://s.w.org/images/core/emoji/13.1.0/72x72/1f600.png)

![AWS VPC](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-20.27.43.png?resize=640%2C222&ssl=1)

![AWS Tags](https://i2.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-20.28.05.png?resize=640%2C228&ssl=1)

Once you’d like to destroy the VPC resource, then run the below command and type the name of the VPC resource you’d like to terminate (in this case it is “TEST”):

```
terraform destroy -auto-approve
```

![Delete VPC](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/08/Screen-Shot-2020-08-22-at-20.28.42.png?resize=640%2C195&ssl=1)
_Screenshot of the successful deletion of the VPC resource tagged as “TEST”._

### Summary

In this post, I’ve demonstrated how to set variables in several different ways. If you’re keen to learn more, check out [Terraform’s official documentation](https://www.terraform.io/docs/language/values/index.html) ![🙂](https://s.w.org/images/core/emoji/13.1.0/72x72/1f642.png)