Terraform modules: Difference between revisions

From wikinotes
No edit summary
No edit summary
 
Line 3: Line 3:


= Input Variables =
= Input Variables =
<blockquote>
== Basics ==
<blockquote>
<blockquote>
Input variables expose optional configuration to the commandline interface.<br>
Input variables expose optional configuration to the commandline interface.<br>
Line 22: Line 24:
terraform apply -var="image_id=foobar"
terraform apply -var="image_id=foobar"
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->
== Validation ==
<blockquote>
You can assign validators to input variables.
<syntaxhighlight lang="tf">
variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."
  validation {
    condition    = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
    error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
  }
}
</syntaxhighlight>
</blockquote><!-- Validation -->
</blockquote><!-- Input Variables -->
</blockquote><!-- Input Variables -->

Latest revision as of 17:52, 25 September 2022

A terraform module is a collection of *.tf files within a specific directory.
A root module is defined at the root of your project.

Input Variables

Basics

Input variables expose optional configuration to the commandline interface.
They are defined in the root module, within variable blocks.

variable "image_id" {
  type = string
}

variable "availability_zone_names" {
  type    = list(string)
  default = ["us-west-1a"]
}

Modify variables on CLI

terraform apply -var="image_id=foobar"

Validation

You can assign validators to input variables.

variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."

  validation {
    condition     = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
    error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
  }
}