Input variables serve as parameters for a Terraform module,allowing aspects of the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations.
Let us explore the different types supported by terraform.
Strings:
strings are just a sequence of characters. If no type is specified, then Terraform assumes a variable is a string.Declaration:
variable "tag_nm" {
type = "string"
default = "environment"
}
A string variable can then be used in resource plans by using the interpolation below..
Interpolation:
tag_name = "${var.tag_nm}"
We can escape interpolation with double dollar signs:
$${tag_nm}
will be rendered as a literal ${tag_nm}
.Lists:
Another type of Terraform variables lists.
Declaration:
variable "block_display_name" {
type = "list"
default = ["MyVolume2","jay"]
}
Lists can be used in the resource plans similarly to
strings, but we need to denote the index of the value.Interpolation of list variables would be.
Interpolation:
Interpolation:
block_name= "${var.block_display_name[0]}" -- It will return MyVolume2
Terraform in built function can be used to get the index value. By combining count attribute and length function ,we can loop over the resources.
count = length(var.block_display_name)
block_name = var.block_display_name[count.index]
Maps:
A map value is a lookup table from string keys to string
values. This is useful for selecting a value based on some other provided value.
Declaration:
variable "instance_image_ocid" {
type = "map"
default = {
linux6 = "ocid1.image.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
windows ="ocid1.image.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
Interpolation:
image_ocid = "${var.instance_image_ocid["linux6"]}"
image_ocid = "${var.instance_image_ocid["linux6"]}"
Boolean
It is recommended for now to specify boolean values for
variables as the strings "true" and "false"..
variable "is_available" {
default = true
}
The above example boolean can be used similarly to a string variable by simply marking down the correct variable. Interpolation also similar to string type.
Ref: https://www.terraform.io/docs/configuration/variables.html
Nice and good article. Thanks for sharing this Information.
ReplyDeleteDevOps Training
DevOps Online Training