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, 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.
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
}
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
}
}
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”).
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.
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!!
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
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
Top comments (0)