Terraform variables

1.numbers


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

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


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

resource "aws_instance" "anitha" {
  count = "${var.usercount}"
  ami = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "Arindam.${count.index}"
  }
  
}

2.strings

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

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


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

resource "aws_instance" "Arindam" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "${var.username}"
  }  
}

3.list

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

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


variable "users" {
    type    = list
    default = ["devopsschool11", "devopsschool2", "devopsschool3"]
    description = "This is for demo of list variable"
}

resource "aws_instance" "arindam" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  tags = {
    Name = "${var.users[0]}"
  } 
 


}

4.Maps

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

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

variable "region" {

  type = string
  default = "us-west-2"

}

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


resource "aws_instance" "Arindam" {
  ami           = "${var.amis[var.region]}"
  instance_type = "t2.micro"
  
  tags = {
    Name = "Arindam"
  }
}