Learn how to define input variables in Terraform that accept only specific allowed values, ensuring consistent and validated infrastructure deployments.
In Terraform, input variables provide flexibility by allowing you to customize deployments. However, without proper validation, these variables can lead to unexpected issues. This guide demonstrates how to restrict input variable values to a predefined set of choices, ensuring consistency and preventing invalid configurations.
Enable Variable Validation: Before you can restrict input variables, ensure you have variable validation enabled in your Terraform configuration:
terraform {
experiments = [variable_validation]
}
Define the Variable: Declare your input variable and specify its type. For a list of allowed choices, use the string
type:
variable "environment" {
type = string
}
Implement Validation: Within the variable block, add a validation
block. This block will contain the logic to enforce the allowed choices:
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Invalid environment. Choose from: dev, staging, prod."
}
contains
function to check if the provided value for var.environment
exists within the list ["dev", "staging", "prod"]
.error_message
is displayed if the condition evaluates to false, guiding the user on valid inputs.Complete Example:
terraform {
experiments = [variable_validation]
}
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Invalid environment. Choose from: dev, staging, prod."
}
}
Now, when you run Terraform, it will verify if the value provided for the environment
variable is one of the allowed choices. If not, it will halt execution and display the specified error message.
This Terraform code defines a variable named "environment" restricted to "dev", "staging", or "prod" values. It uses variable validation to enforce these choices and provides an example of using this variable when creating an AWS instance.
terraform {
experiments = [variable_validation]
}
variable "environment" {
type = string
description = "Target environment (dev, staging, or prod)"
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Invalid environment. Choose from: dev, staging, prod."
}
}
# Example usage of the validated variable
resource "aws_instance" "example" {
ami = "ami-0c94855a61da8a300" # Replace with your desired AMI
instance_type = "t2.micro"
tags = {
Name = "Example Instance - ${var.environment}"
}
}
Explanation:
terraform { experiments = [variable_validation] }
line enables the feature.variable "environment"
block declares the variable with type string
.validation
block enforces the allowed choices ("dev", "staging", "prod") using the contains
function. If an invalid value is provided, the error_message
is displayed.aws_instance
resource demonstrates how to use the validated var.environment
variable within your Terraform code.How to Use:
.tf
file (e.g., main.tf
).terraform init
to initialize the working directory.terraform apply -var="environment=dev"
terraform apply -var="environment=test"
(This will fail with the error message).This example demonstrates how to restrict input variables to a predefined list of choices, ensuring that your Terraform configurations are applied with valid and expected values.
terraform plan
stage, rather than during deployment, saving time and potential issues.validation
block guide users on providing correct input values, making your Terraform modules more user-friendly.contains
: While this example uses contains
, Terraform offers other functions like length
, regex
, and more for complex validation scenarios.condition
block.experiments = [variable_validation]
in your Terraform configuration.variable_validation
experiment is expected to become a standard feature in future Terraform versions.By incorporating these notes and the provided code examples, you can leverage variable validation to write cleaner, more maintainable, and error-resistant Terraform code.
This guide explains how to limit the values users can input for a Terraform variable.
Steps:
Enable Variable Validation: Add the following to your Terraform configuration file:
terraform {
experiments = [variable_validation]
}
Define the Variable: Declare your variable and its type (use string
for a list of choices):
variable "environment" {
type = string
}
Implement Validation: Inside the variable block, add a validation
block with the logic to enforce allowed choices:
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Invalid environment. Choose from: dev, staging, prod."
}
contains
checks if the input value exists in the specified list.error_message
guides the user on valid inputs if the condition fails.Example:
terraform {
experiments = [variable_validation]
}
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Invalid environment. Choose from: dev, staging, prod."
}
}
This configuration ensures that the environment
variable can only accept "dev", "staging", or "prod" as values. Any other input will halt execution and display the error message.
By restricting input variable values, you enhance the reliability and predictability of your Terraform deployments. This practice is essential for managing infrastructure at scale and ensuring that your configurations are applied consistently and without errors.