Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Tutorials: Variables Complete Reference

In Terraform, there are several types of variables that you can define, including:

  1. String: A sequence of characters. Example: “hello”.
  2. Number: A numeric value. Example: 42.
  3. Boolean: A logical value that is either true or false.
  4. List: A sequence of values of the same type. Example: [ “apples”, “oranges”, “bananas” ].
  5. Map: A collection of key-value pairs, where the keys and values can be of any type. Example: { “name” = “John”, “age” = 30 }.
  6. Object: A complex data type that can contain multiple attributes. Example: { name = “John”, age = 30, address = { street = “123 Main St”, city = “Anytown”, state = “CA” } }.
  7. Tuple: A sequence of values of different types. Example: [ “John”, 30, true ].

Quick Tutorials on Terraform Variable

Terraform Variables and Configuration explained – 5 mins reading!

Terraform Variable Naming convention

Types of Terraform variable

Location of Declaring Terraform Variable


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
}

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.