Terraform : les variables

Posted by

Input variables :

Chaine :

variable "instance-type" {
  type        = string
  description = "The type of instance"
  default     = "t2.micro"
}

resource "aws_instance" "srv1" {
  ami           = "ami-8ee056f3"
  instance_type = var.instance-type
}

Bool :

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

resource "aws_instance" "srv1" {
  ami           = "ami-8ee056f3"
  ebs_optimized = var. ebsoptimized
  instance_type = var.instance-type
}

Tableau :

variable "iam-users" {
  type    = list(any)
  default = ["homer", "bart", "flanders"]
}

resource "aws_iam_user" "admin" {
  name = var.iam-users[1]
  path = "/system/"
}

Clé/Valeur :

variable "storage_sizes" {
  type = map(any)
  default = {
    "WebServer"  = "10"
    "DataBase"   = "30"
    "FileServer" = "100"
  }
}

resource "aws_ebs_volume" "www_srv1" {
  availability_zone = aws_instance.srv1.availability_zone
  size              = var.storage_sizes["WebServer"]
}

Depuis la CLI :

terraform apply -var instance-type=t3.micro

Output variables :

Afficher une valeur :

output "srv1_ip" {
  value = aws_instance.srv1.public_ip
}

Leave a Reply

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *