Logo

dev-resources.site

for different kinds of informations.

Terraform input validation

Published at
1/15/2025
Categories
terraform
automaton
devops
infrastructureascode
Author
marko_milosavljevic_97169
Author
25 person written this
marko_milosavljevic_97169
open
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."
  }
}
Enter fullscreen mode Exit fullscreen mode

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."
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

infrastructureascode Article's
30 articles in total
Favicon
Introduction to Terraform: Revolutionizing Infrastructure as Code
Favicon
Terraform input validation
Favicon
2024 Product Release Highlights
Favicon
Thrilled to Announce the Launch of My Book "Mastering Infrastructure as Code with AWS CloudFormation"
Favicon
New Backstage Plugin: Manage and Deploy IaC from Your Internal Developer Portal
Favicon
AWS CloudFormation: Infrastructure as Code for Efficient Cloud Management
Favicon
Mastering Managed IaC Self-Service: The Complete Guide
Favicon
Declarative in Terraform: Simple, Until It’s Not! 🚧
Favicon
Build vs. Buy: Choosing the Right Approach to IaC Management
Favicon
Infrastructure as Code
Favicon
Streamlining env0 Onboarding with Environment Discovery
Favicon
How we handle Terraform downstream dependencies without additional frameworks
Favicon
Introduction à Terraform avec Proxmox
Favicon
Automating Docker Workflows with Chef: A Complete Guide
Favicon
Terraform Cookbook: Development Environment Recipe
Favicon
Automating Docker Management with Terraform
Favicon
Top DevOps Tools for Infrastructure Automation in 2025
Favicon
Setting Up a Production-Ready Kubernetes Cluster with RKE2 in vSphere Using Terraform
Favicon
Mastering Ansible Playbooks: Step by Step Guide
Favicon
Guia de Comandos PM2
Favicon
Terraform Basics
Favicon
Part 1: Setting Up Initial AWS Infrastructure for the Intrusion Detection System with Terraform (Tutorial)
Favicon
AWS CloudFormation Tutorial: Automating Infrastructure as Code
Favicon
Harnessing the Power of AWS Security Services
Favicon
What is infrastructure as code and how its transforming DevOps
Favicon
Building a Smart Log Pipeline: Syslog Parsing, Data Enrichment, and Analytics with Logstash, Elasticsearch, and Ruby
Favicon
Docker for Infrastructure as Code (IaC): Automating Infrastructure with Containers
Favicon
Concept of Infrastructure Monitoring
Favicon
Terraform Map Variable: A Complete Guide with Practical Examples
Favicon
Terraform Workspaces Guide: Commands, Examples and Best Practices

Featured ones: