Types of variables
In Terraform, there are several types of variables that you can define, including:
- String: A sequence of characters. Example: “hello”.
- Number: A numeric value. Example: 42.
- Boolean: A logical value that is either true or false.
- List: A sequence of values of the same type. Example: [ “apples”, “oranges”, “bananas” ].
- Map: A collection of key-value pairs, where the keys and values can be of any type. Example: { “name” = “John”, “age” = 30 }.
- Object: A complex data type that can contain multiple attributes. Example: { name = “John”, age = 30, address = { street = “123 Main St”, city = “Anytown”, state = “CA” } }.
- Tuple: A sequence of values of different types. Example: [ “John”, 30, true ].
How to declare variables in Terraform?
As per below image we can declare variables :
- variables.tf – Good for providing non-sensitive values, reasonable defaults
- environmental variables – Easy to set , Use TF_VAR_<Variable> pattern
- Command line – Assign with runtime flag such as -var= ‘my_var=value’
For more details please go through below link – https://www.devopsschool.com/blog/terraform-variables-complete-reference-guide/
Where we can declare the value?
We can also declare value in any of below:
- variables.tf
- environmental variables
- Command line
And below files are only specific to value , we can declare here also.
.auto.tfvars
terraform.tfvars.json
-
terraform.tfvars
Mostly we prefer terraform.tfvars
only for value declaration
Note: If we declare the variables but not set the values , terraform will prompt for the value, terraform will ask for required values not otherwise provided
Precedence Order
How variables are assigned and used in Terraform, along with their precedence.
When multiple methods are used to assign values to variables, Terraform uses the following precedence order, from highest to lowest:
- Command Line
.auto.tfvars
filesterraform.tfvars.json
fileterraform.tfvars
file- Environment Variables
variables.tf
1. Command Line
Highest Precedence
You can assign variable values directly from the command line using the -var
flag.
Example:
terraform apply -var="instance_type=t2.micro"
2. .auto.tfvars
Files
Terraform automatically loads any files ending with .auto.tfvars
or .auto.tfvars.json
in the current directory.
Example:
default.auto.tfvars
:
instance_type = "t2.micro"
3. terraform.tfvars.json
File
You can define variables in a JSON file named terraform.tfvars.json
.
Example:
terraform.tfvars.json
:
{
"instance_type": "t2.micro"
}
4. terraform.tfvars
File
This is a common method to assign values to variables by creating a file named terraform.tfvars
.
Example:
terraform.tfvars
:
instance_type = "t2.micro"
5. Environment Variables
You can set environment variables using the TF_VAR_
prefix. This method is useful for setting sensitive values like API keys.
Example:
export TF_VAR_instance_type="t2.micro"
6. variables.tf
Lowest Precedence
This file contains the default values for variables. If no other values are provided, Terraform uses these defaults.
Example:
variables.tf
:
variable "instance_type" {
description = "The type of instance to use"
type = string
default = "t2.micro"
}
Interactive
If a required variable is not provided by any of the methods above, Terraform will prompt you to enter the value interactively during the plan or apply phase.
Example:
terraform apply
var.instance_type
The type of instance to use
Enter a value: t2.micro
Module Invocation
When invoking a module, you can pass variables to the module directly. This method provides another opportunity to assign values to variables.
module "my_module" {
source = "./path_to_module"
instance_type = "t2.micro"
}
Summary
- Command Line: Highest precedence, overrides all other methods.
.auto.tfvars
Files: Automatically loaded, useful for shared configurations.terraform.tfvars.json
andterraform.tfvars
Files: Common methods for defining variable values in separate files.- Environment Variables: Convenient for setting sensitive or default values.
variables.tf
: Contains default values for variables, lowest precedence.- Interactive: Prompts for values if not provided.
- Module Invocation: Passes values directly to modules when called.
Code Example
variable "instance_count" {
type = number
default = 1
}
variable "instance_name" {
type = string
default = "Rajesh"
}
resource "aws_instance" "example-number" {
count = var.instance_count
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = var.instance_name
}
}
variable "security_groups" {
type = list(string)
default = ["sg-0541801a7a059ba17"]
}
resource "aws_instance" "example-list" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}
variable "instance_tags" {
type = map(string)
default = {
Name = "my-instance4mMap-Rajesh"
}
}
resource "aws_instance" "example-map" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
variable "create_vm" {
description = "If set to true, it will create vm"
type = bool
}
resource "aws_instance" "example-bool" {
count = var.create_vm ? 1 : 0
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
Break down Description for above steps:
Let’s break down each part to understand how these variables work and are used in the resource configurations.
Variable Declarations
- Number Variable
variable "instance_count" {
type = number
default = 1
}
- Usage: This variable determines the number of instances to create. It has a default value of 1.
- String Variable
variable "instance_name" {
type = string
default = "Rajesh"
}
- Usage: This variable sets the name tag for the instance. It has a default value of “Rajesh”.
- List Variable
variable "security_groups" {
type = list(string)
default = ["sg-0541801a7a059ba17"]
}
- Usage: This variable holds a list of security group IDs. It has a default list containing one security group ID.
- Map Variable
variable "instance_tags" {
type = map(string)
default = {
Name = "my-instance4mMap-Rajesh"
}
}
- Usage: This variable holds a map of tags for the instance. It has a default map with a single key-value pair.
- Boolean Variable
variable "create_vm" {
description = "If set to true, it will create vm"
type = bool
}
- Usage: This variable is a boolean that determines whether to create an instance. It’s typically used for conditional resource creation.
Resource Configurations
- Using a Number Variable
resource "aws_instance" "example-number" {
count = var.instance_count
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = var.instance_name
}
}
- Explanation: The
count
meta-argument is set to the value ofvar.instance_count
. Thetags
argument usesvar.instance_name
for the Name tag.
- Using a List Variable
resource "aws_instance" "example-list" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}
- Explanation: The
vpc_security_group_ids
argument is set to the value ofvar.security_groups
, which is a list of security group IDs.
- Using a Map Variable
resource "aws_instance" "example-map" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
- Explanation: The
tags
argument is set to the value ofvar.instance_tags
, which is a map of tag key-value pairs.
- Using a Boolean Variable
resource "aws_instance" "example-bool" {
count = var.create_vm ? 1 : 0
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = var.instance_tags
}
Count can use global in terraform which we call meta argument , from above examples
if variable var.create_vm is true than set count is 1 if variable is false than it will create 0
- Explanation: The
count
meta-argument is set to 1 ifvar.create_vm
is true, otherwise it’s set to 0. This allows for conditional creation of the instance.
Summary
- Number Variable: Controls the number of instances.
- String Variable: Sets the name tag for instances.
- List Variable: Provides a list of security group IDs.
- Map Variable: Provides a map of tags for instances.
- Boolean Variable: Conditionally creates an instance based on the boolean value.
Terraform plan will give below result