Terraform – introducing input variables

In my last Terraform post I talked a little bit about the basics what Terraform is and how it works from ground up. In this post I want to focus on input variables and how they can be used to create flexible deployments.

Terraform Series

1. A first introduction
2. Introducing input variables (this post)
3. Using multiple files for configurations and variables

1. Defining and using variables

Using variables is a common concept in Terraform. Instead of distributing values across your configuration file, you can use variables in the Terraform file that can be populated during the deployment process. And they can contain default values in case no values are submitted during runtime. Terraform supports multiple different variables types. Here are 2 examples.

You can get the demo files from this GitHub repo > https://github.com/MarcelZehner/blogcodeexamples


These variable types allow you to store string values.

Defining a string variable:

variable “azure_region” {
  type = “String”
  description = “The Azure region used to deploy resources”
  default = “West Europe”

Using a string variable in the configuration to deploy a resource group:

resource “azurerm_resource_group” “rg1” {
  name = “${var.azure_rg_name}”
  location = “${var.azure_region}”


Maps allow you to define mapping tables to lookup values. You could define multiple storage account types in a mapping table where you then select which one to use based on the environment you deploy a resource.

Defining a map variable:

variable “storageaccounttype” {
  type = “map”
  description = “Type of the storage account depending on environment”
  default = {
    dev = “LRS”
    prod = “GRS”

Using a map variable in the configuration to deploy a storage account:

resource “azurerm_storage_account” “storage1” {
  name = “${format(“terra2018%s”, var.environment)}”
  resource_group_name = “${azurerm_resource_group.rg1.name}”
  location = “${var.azure_region}”
  account_tier = “standard”
  account_replication_type = “${lookup(var.storageaccounttype,   var.environment)}”

In this example I not only use the map variable, but also two Terraform functions. To define the name of the storage account, I use the format() function to concatenate two strings. Depending on the environment variables value (“dev” or “prod”) I will set the storage account name to either “Terra2018prod” or “Terra2018dev”. Next, I use the lookup() function and use the environment variables value (again “dev” or “prod”) as the key. This will return either LRS or GRS from the map which is then used to set the replication property of the storage account.

There is more

There are more variable types available, lists and booleans. The procedure to use those will be more or less the same as with the ones I showed you here, so I will go into the details here. Check out the documentation for more details about those.

2. Assigning Variable Values

Variables need to be populated with values. If a default value is set for a variable and no value is assigned, the default value will be used. If you want to assign a value yourself, there are several options you can go for. I will use this simple example to demonstrate this.

variable “azure_region” {}
output “azure_region” {
  value = “${var.azure_region}”

Apply without setting a value

When just applying this, Terraform will recognize that there is no value set for the variable. It will ask for a value.

.\terraform.exe apply


Using the Terraform CLI

You can also use the CLI to submit a value to one or multiple variables.

.\terraform.exe apply -var azure_region=”North Europe”

Using a .tfvars variables file

This might be the preferred way of handling variables. All values are stored in a specific variables file (.tfvars). When planning or applying a Terraform configuration, you just inform the CLI to use this file.

.\terraform.exe apply –var-file .\myvars.tfvars

That should be enough for now. You now have a basic understanding of Terraform variables and how they can be used. However, it will get a bit more complicated when you are using multiple Terraform configuration and variable files. But I will cover this in the next post. So stay tuned!


This entry was posted in ARM, Azure, IaC and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Terraform – introducing input variables

  1. Pingback: Terraform – using multiple files for configurations and variables | marcelzehner.ch

  2. Pingback: Terraform – a first introduction | marcelzehner.ch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s