DEV Community

Marko Milosavljevic
Marko Milosavljevic

Posted on

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay