terraform variable script

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

– 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

#Types of Terraform variable - Number
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"
  access_key = "XXXXXXXXXXXXXXXXX"
  secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}


variable "usercount" {
  type = number
  description = "This is for demo of number variable"
  default = 3
}

resource "aws_instance" "manideep_number" {
  ami = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  count = "${var.usercount}"
  tags = {
    Name = "Manideep.${count.index}"
  }
  
}
#Types of Terraform variable – String 

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"
  access_key = "XXXXXXXXXXXXXXXXX"
  secret_key = "XXXXXXXXXXXXXXXXX"
}


variable "username" {
  type = string
  description = "This is for demo of string variable"
  default = "demouser"
}

resource "aws_instance" "manideep" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "mani.${var.username}"
  }  
}
#Types of Terraform variable – List

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"
  access_key = "XXXXXXXXXXXXXXXXX"
  secret_key = "XXXXXXXXXXXXXXXXX"
}


variable "users" {
    type    = list
    default = ["dev1", "dev2", "dev3"]
    description = "This is for demo of list variable"
}

resource "aws_instance" "Manideep" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "new.${var.users[0]}"
  } 
 
}
#Types of Terraform variable – Map

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"
  access_key = "XXXXXXXXXXXXXXXXX"
  secret_key = "XXXXXXXXXXXXXXXXXXXXXXXX"
}

variable "amis" {
  type = "map"
  default = {
    
    "us-west-2" = "ami-03d5c68bab01f3496"
  }
}


resource "aws_instance" "manideep" {
  ami           = "{$var.amis[var.region]}"
  instance_type = "t2.micro"
  
  tags = {
    Name = "Manideep_map"
  }
}
# Types of Terraform variable – Boolean

variable "pass" {
  type    = bool
  default = false
}

resource "git_password" "example" {
  create_password = ${var.pass}
}