Terraform Input Variables


Terraform input variables can be used to pass values from outside of the configuration or module. They are used to assign dynamic values to resource attributes.

Input variables must be declared using a variable block in your Terraform configuration file.

Variables can then be assigned values using the command line, environment variables, interactive input, or a fallback default.

Refer to the Terraform documentation about the precedence of a variable definition when multiple assignment methods are used for the same variable.

Declaring a Variable

Each input variable must be declared in your Terraform configuration file using a variable block.

The label after the variable keyword is an arbitrary name for the variable, which must be unique among all variables in the same module. This name is used to assign a value to the variable from outside and to reference the variable's value from within the module.

The following example shows 4 input variables being declared in preparation for creating a user account in your Files.com site:

// Variable for Partner Company Name

variable "company" {
  type        = string
  description = "The company name of the partner." 
}

// Variable for Partner User's Name

variable "name" {
  type        = string
  description = "The full name of the partner user." 
}

// Variable for Partner User's Email Address

variable "email" {
  type        = string
  description = "The email address of the partner user." 
}

// Variable for Partner Username

variable "user_name" {
  type        = string
  description = "The login username for the partner user." 
}

The name of a variable can be any valid identifier except the following: source, version, providers, count, for_each, lifecycle, depends_on, locals.

Using Input Variable Values

A variable's value can be accessed from within expressions as var.<NAME>, where <NAME> matches the label given in the declaration block. That is, input variables are created by a variable block, but you reference them as attributes on an object named var.

For example, to use the variables declared above for creating a user account:

// Create Partner Account Using Input Variables

resource "files_user" "partner_user" {
  email                 = var.email
  authentication_method = "password"
  name                  = var.name
  company               = var.company
  notes                 = "Internal notes about ${var.name}."
  password              = "ARea11y5tr0ngP@ssw0rd"
  self_managed          = "true"
  time_zone             = "Pacific Time (US & Canada)"
  user_root             = "partners/${var.company}"
  username              = var.user_name
}

Interactive Input of a Variable

The terraform CLI will automatically prompt you to enter the input values for declared variables.

Using the above examples, running the terraform apply command will prompt you for the following inputs:

$ terraform apply
var.company
  The company name of the partner.

  Enter a value: Acme

var.email
  The email address of the partner user.

  Enter a value: jane.doe@acme.com

var.name
  The full name of the partner user.

  Enter a value: Jane Doe

var.user_name
  The login username for the partner user.

  Enter a value: acme_jane_doe

These manually entered values will be used for the creation of the user account resource.

Command Line Assignment of a Variable

Input variable values can be assigned on the Terraform command line using the -var option when running the terraform plan and terraform apply commands:

$ terraform apply -var="company=Acme" -var="name=Jane Doe" -var="email=jane.doe@acme.com" -var="user_name=acme_jane_doe"

Environment Variable Assignment of a Variable

Environment variables can used to assign values to input variables.

Terraform searches the environment of its own process for environment variables named TF_VAR_ followed by the name of a declared variable.

Windows Example

set TF_VAR_company="Acme"
set TF_VAR_name="Jane Doe"
set TF_VAR_email="jane.doe@acme.com"
set TF_VAR_user_name="acme_jane_doe"

Linux/MacOS Example

export TF_VAR_company="Acme"
export TF_VAR_name="Jane Doe"
export TF_VAR_email="jane.doe@acme.com"
export TF_VAR_user_name="acme_jane_doe"

Fallback Default Value of a Variable

The variable declaration can also include a default argument. If present, the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform.

The default argument requires a literal value and cannot reference other objects in the configuration.

For example, we can configure the company input variable to be optional and have a default fallback value by using this definition in the configuration file:

// Variable for Partner Company Name with Default Fallback

variable "company" {
  type        = string
  description = "The company name of the partner."
  default     = "Acme Corp."
}

Get Instant Access to Files.com

The button below will take you to our Free Trial signup page. Click on the white "Start My Free Trial" button, then fill out the short form on the next page. Your account will be activated instantly. You can dive in and start yourself or let us help. The choice is yours.

Start My Free Trial

©2024 Files.com. All right reserved

FILES.COM

  • Start My Free Trial
  • Pricing
  • Docs
  • API and SDKs
  • Contact

CONTACT & SUPPORT

support@files.com

(800) 286-8372

Monday–Friday

9am–8pm Eastern