Terraform input validation
When managing infrastructure with Terraform you will for sure use variable which play crucial role for environment specific configurations. Sometimes human errors may happen and variables values can lead to certain issues. Meaning if you do not use Terraform input validation you could provide value such as prodd instead of prod could have consequences: Resources being created in wrong environment Deployment pipeline breaking unexpectedly Wasting time debugging issues Another example of Terraform input validation could be for AWS Lambda memory and preventing engineers from accidentally assigning higher memory or wrong value since minimum value is 128MB. Not providing validation for this variable could lead to: Deployment failure due to incorrect configuration Higher costs and unused resources Wasting time debugging issues Terraform Example: Validating AWS Environment Stages Here is a code sample for aws_stage variable variable "aws_stage" { description = "The AWS account stage (dev, qa, prod)" type = string default = "dev" validation { condition = contains(["dev", "qa", "prod"], lower(var.aws_stage)) error_message = "Invalid input, options: dev, qa, prod." } } Explanation: The validation block ensures that the input matches one of the values (dev, qa, prod). If an invalid value is provided, Terraform throws an error with the custom message: "Invalid input, options: dev, qa, prod." If no value is specified, Terraform defaults to dev. Terraform Example: Validating AWS Lambda memory To prevent invalid memory sizes, we can use a validation block: variable "lambda_memory_size" { description = "Memory size for the Lambda function in MB" type = number default = 128 validation { condition = var.lambda_memory_size >= 128 && var.lambda_memory_size
When managing infrastructure with Terraform you will for sure use variable which play crucial role for environment specific configurations. Sometimes human errors may happen and variables values can lead to certain issues. Meaning if you do not use Terraform input validation you could provide value such as prodd instead of prod could have consequences:
- Resources being created in wrong environment
- Deployment pipeline breaking unexpectedly
- Wasting time debugging issues
Another example of Terraform input validation could be for AWS Lambda memory and preventing engineers from accidentally assigning higher memory or wrong value since minimum value is 128MB. Not providing validation for this variable could lead to:
- Deployment failure due to incorrect configuration
- Higher costs and unused resources
- Wasting time debugging issues
Terraform Example: Validating AWS Environment Stages
Here is a code sample for aws_stage variable
variable "aws_stage" {
description = "The AWS account stage (dev, qa, prod)"
type = string
default = "dev"
validation {
condition = contains(["dev", "qa", "prod"], lower(var.aws_stage))
error_message = "Invalid input, options: dev, qa, prod."
}
}
Explanation:
The validation block ensures that the input matches one of the values (dev, qa, prod).
If an invalid value is provided, Terraform throws an error with the custom message: "Invalid input, options: dev, qa, prod."
If no value is specified, Terraform defaults to dev.
Terraform Example: Validating AWS Lambda memory
To prevent invalid memory sizes, we can use a validation block:
variable "lambda_memory_size" {
description = "Memory size for the Lambda function in MB"
type = number
default = 128
validation {
condition = var.lambda_memory_size >= 128 && var.lambda_memory_size <= 512
error_message = "Memory size must be between 128 MB and 512 MB."
}
}
Explanation:
The validation block ensures that the input matches minimum value of 128 and maximum value of 512MB
If an invalid value is provided, Terraform throws an error with the custom message: "Memory size must be between 128MB and 512MB."
If no value is specified, Terraform defaults to 128MB.
Advantages of using Terraform Input Validation
- Ensure consistency for naming, resource allocation, bad inputs, etc.
- Avoid possible costly mistakes
- Implement standards and improves team collaboration
- Reduce time wasted to debugging since input validation catches errors during plan
- Standardize naming patterns across resources
- Validate more complex data structures (e.g. lists, maps, or other) by preventing invalid input
Conclusion
Terraform input validation is simple yet very powerful feature to enforce standards and improve reliability especially in multi-environments setups.