startswith ככלי לvalidation גמיש יותרTF_VAR_) עם שאר השיטותלצורך ניסוי סדר העדיפות נרצה לעבוד עם כמה סוגי instance מתוך אותה משפחה. במקום לרשום רשימה קבועה ב-contains, משתמשים בפונקציית startswith שמקבלת את המשתנה כארגומנט ראשון ואת הPrefix כארגומנט שני — שימו לב להיפוך הסדר ביחס ל-contains.
startswith(var.ec2_instance_type, "t3.") — מקבל כל סוג שמתחיל ב-t3.contains(["t2.micro", "t3.micro"], ...) המקוריterraform plan# variables.tf - temporary validation for this lab
variable "ec2_instance_type" {
type = string
default = "t2.micro"
description = "The type of the managed EC2 instances."
validation {
condition = startswith(var.ec2_instance_type, "t3.")
error_message = "Only supports T3 family."
}
}
Terraform מכבד את הסדר הבא כאשר אותו משתנה מוגדר בכמה מקומות. כל שכבה דורסת את כל השכבות שמתחתיה: