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 ].
![](https://www.devopsschool.com/blog/wp-content/uploads/2023/04/terraform-variable-types-1-1024x355.png)
Quick Tutorials on Terraform Variable
Terraform Variables and Configuration explained – 5 mins reading!
Terraform Variable Naming convention
Types of Terraform variable
- Types of Terraform variable – Number
- Types of Terraform variable – String
- Types of Terraform variable – List
- Types of Terraform variable – Map
- Types of Terraform variable – Boolean
Location of Declaring Terraform Variable
![](https://www.devopsschool.com/blog/wp-content/uploads/2023/04/terraform-variable-location-1.png)
- Environment Variables
- Local Values
- Command Line
- terraform.tfvars
- anyotherfile.tfvars
- Terrform Outout Variable
- Example of terraform terraform.tfvars
- Terraform Environment Variables Exaplained!
One Example Code
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
}
![loading](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/gallery-page-loader.gif)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![play](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/playhover.png)
![loading](https://www.devopsschool.com/blog/wp-content/plugins/youtube-embed-plus/images/gallery-page-loader.gif)
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND