Skip to content

Commit b5393f1

Browse files
add terraform validation docs
1 parent 4ab88e6 commit b5393f1

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

source/Terraform/index.html.md.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Terraform Best Practices
3+
last_reviewed_on: 2024-07-26
4+
review_in: 12 months
5+
weight: 210
6+
---
7+
8+
# <%= current_page.data.title %>
9+
10+
This is a space for any guides relating to work with Terraform.
11+
12+
- [Terrafrom Validation](./terraform-validation.html)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Terraform Variable Validation
3+
last_reviewed_on: 2024-08-26
4+
review_in: 12 months
5+
weight: 210
6+
---
7+
8+
# <%= current_page.data.title %>
9+
10+
## What is it & Why is it useful?
11+
12+
• Variable validation in Terraform is a feature that helps ensure the values assigned to variables meet specific criteria set in the code. This feature has been available since Terraform version 0.13.0.
13+
14+
• Restricting user input prevents users from entering invalid data or catching misconfigured code, which can help avoid errors and correct mistakes in the code early on.
15+
16+
• Finally, improving code quality and resiliency by ensuring that only valid data is used, the overall quality and reliability of the code are enhanced.
17+
18+
## Recent Enhancements
19+
20+
• Recent updates (as of Terraform 1.9) have improved variable validation by allowing variables to reference other variables outside of their own scope. This means you can create more complex and interdependent validation rules. [Find more info on Hashicorp blog post](https://www.hashicorp.com/blog/terraform-1-9-enhances-input-variable-validations#).
21+
22+
23+
## Example usage
24+
25+
### Validating aks node os maintenance window
26+
27+
```terraform
28+
variable "node_os_maintenance_window_config" {
29+
type = object({
30+
frequency = optional(string, "Weekly")
31+
interval = optional(number, 1)
32+
duration = optional(number, 4)
33+
day_of_week = optional(string, "Monday")
34+
start_time = optional(string, "23:00")
35+
utc_offset = optional(string, "+00:00")
36+
start_date = optional(string, null)
37+
is_prod = optional(bool, true)
38+
})
39+
default = {}
40+
41+
...
42+
43+
validation {
44+
condition = var.node_os_maintenance_window_config.duration >= 4
45+
error_message = "Maintenance window duration must be at least 4 hours when node_os_channel_upgrade is enabled."
46+
}
47+
48+
validation {
49+
condition = try(contains(["Daily", "Weekly"], var.node_os_maintenance_window_config.frequency), false)
50+
error_message = "Maintenance window frequency must be set to 'Daily' or 'Weekly'."
51+
}
52+
53+
validation {
54+
condition = var.node_os_maintenance_window_config.interval >= 1
55+
error_message = "Maintenance window interval must be at least 1."
56+
}
57+
...
58+
}
59+
```
60+
61+
## Explanation
62+
63+
In the code above we can validate the object variable to follow certian inputs that we want. For the full code follow the [link here](https://github.com/hmcts/aks-module-kubernetes/blob/1f358466d410b4bdcf3d053477af4de5315a3c82/10-kubernetes-inputs.tf#L100) to access the repo.
64+
65+
```terraform
66+
validation {
67+
condition = var.node_os_maintenance_window_config.duration >= 4
68+
error_message = "Maintenance window duration must be at least 4 hours when node_os_channel_upgrade is enabled."
69+
}
70+
```
71+
72+
With the following code above we want to ensure that maintenance window duration is at least 4 hours as per requirments in the docs, we can validate the inputs before the terraform planning stage.
73+
74+
```terraform
75+
validation {
76+
condition = try(contains(["Daily", "Weekly"], var.node_os_maintenance_window_config.frequency), false)
77+
error_message = "Maintenance window frequency must be set to 'Daily' or 'Weekly'."
78+
}
79+
```
80+
81+
For the above code we want to ensure that the frequency is set to "Daily" or "Weekly" explicitly to ensure no misconfiguration in the maintenance window.
82+
83+
### More examples
84+
85+
Further explanation and examples can be found here at [Hashicorps website](https://developer.hashicorp.com/terraform/language/values/variables#custom-validation-rules).

0 commit comments

Comments
 (0)