Continuing with my learning on Terraform, I realized an exciting type of variable -map type and how to refer to it. And that was when I decided to prepare my notes on Terraform variable types, ways to declare them, assign values, and refer to them. These are my running notes, so I’ll keep updating them as I learn more.
Note: I’m keeping off of going into the details about -what are variables, why we use them, benefits, reusability, etc
An excellent place to start and far more informative than my notes are Terraform Docs. There are some pertinent examples, and Hashicorp maintains the document.
Per Terraform Docs, input variables can be classified into two broad categories: primitive (string, number and boolean) and complex (list, map, object).
Here is an example of how these variables are declared and referred to.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# string variable declare | |
variable "region" { | |
description = "The region where to provision resources" | |
type = string | |
default = "us-west-2" | |
} | |
# string variable usage | |
provider "aws" { | |
region = var.region # this will assign the value "us-west-2" to region | |
access_key = var.access_key | |
secret_key = var.secret_key | |
} | |
#—————————————————————————- | |
# list variable declare | |
variable "bucket_name" { | |
description = "The name of S3 bucket" | |
type = list(string) | |
default = ["beans", "pepper", "tomato"] | |
} | |
# list variable usage | |
resource "aws_s3_bucket" "aws-b1" { | |
bucket = "${var.bucket_name[count.index]}–${random_integer.rand_int.result}" | |
acl = "private" | |
count = 3 | |
tags = { | |
Name = "My bucket" | |
Environment = "Dev" | |
} | |
} | |
#—————————————————————————- | |
#map variable declare | |
variable "ami_location" { | |
type = map | |
default = { | |
us-east-1 = "ami-0412e100c0177fb4b" | |
us-east-2 = "ami-0354df7841220296c" | |
} | |
} | |
#map variable usage | |
resource "aws_instance" "server" { | |
ami = "${var.ami_location["${var.region}"]}" | |
instance_type = "t2.micro" | |
key_name = "terraform-key" | |
security_groups = ["${aws_security_group.allow_rdp.name}"] | |
tags = { | |
Name = "Web-App-Prod-1" | |
} | |
} |
Particularly interesting is the list and map variables. I created a list variable with three desired Amazon S3 bucket names and then, using the count
parameter, was able to create multiple buckets instead of having a separate resource block for each bucket. The map variable type is a collection of key pair values that can be useful under specific conditions like listing a particular AMI image from an AWS region. I created two entries for the variable ami_location
and then referenced that in the resource "aws_instance" "server"
. The AMI image selected will depend on the value of the variable var.region
that I declared in the variable.tf file and assigned in the terraform.tfvars file (in my case). I found the map variable type very interesting to work with especially looking at flexibility and modularity. And I will admit that it took me time to get my code to be working finally.
Continuing, here are a few ways of assigning values to these variables.
—using .tfvars or .tfvars.json: This is my preferred option for secured variables like credentials. Also, update .gitignore not to track and commit this file.
—using variables.tf: Keep variable declaration in a separate file/s (for e.g. variable.tf and/or variables.tfvars). That way, managing and locating them is quicker than declaring them across all the Terraform configuration files.
—using environment variables: Save environment variables with the prefix -“TF_VAR_” in the machine where terraform configuration files are run.
—using the -var option on commandline: A variable can be declared in a .tf configuration file but no value assigned to it there. On the command prompt the variable can be passed such as terraform plan -var="access_key=ABVDEFKGLESIFGJWEH" -var="secret_key=fgljk;rtue[p6ruyegdfssgh[t&%^&rfhGUyu%46-rjt"
Note: There are certain restrictions on variable names—full list here.
Some good articles and documentation on Terraform variables:
Terraform Docs
Cloudskills.io, Getting started with Terraform on Azure: Variables by Luke Orellana
UpCloud.com, How to use Terraform variables by Janne Roustemma. I found the comments informative too.
Learning Terraform – Part 2: Variables, Expressions and Functions by Dave McCollough