I have a locals block in module-1 and want to override that on dev/variables.tf
I tried everything but nothing worked. As I'm using multiple variables in this locals block, I need to keep it that way instead of converting to a variable. How can I accomplish that?
├── _modules
│ ├── module-1
│ │ ├── main.tf
│ │ └── variables.tf
└── env
├── dev
│ ├── main.tf
│ └── variables.tf
├── staging
│ ├── main.tf
│ └── variables.tf
As per this doc, I also tried using override.tf file but it didn't override the block.
ChatGPT says locals cannot be overwritten outside of the module if it's true. In this case, what would be the best practise to replace a block in terraform? As I mentioned, I'm using multiple variables in this block and can't use a variable for it, but in the meantime, I want to overwrite this block not to repeat myself. :thinking:
Edit:
_modules/module-1/variables.tf
...
...
variable "host_address" {
type = string
}
locals {
health_checks = {
checks-1 = {
api_name = "Name X"
api_method = "GET"
host = var.host_address
custom_headers = {
Content-Type = "application/json"
}
}
checks-2 = {
api_name = "Name Y"
api_method = "POST"
host = var.host_address
custom_headers = {
Content-Type = "application/json"
}
}
# Add more checks as needed
}
}
_modules/module-1/main.tf
...
...
resource "checkly_check" "health-checks" {
for_each = local.health_checks
name = "${var.environment} - ${each.value.api_name}"
host = "${each.value.host_address}"
# ...
}
Basically, this locals should be overwritten in env/dev, env/staging etc.
Edit 2:
env/dev/variables.tf
...
...
variable "host_address" {
type = string
}
locals {
health_checks = {
checks-1 = {
api_name = "Name Dev Test"
api_method = "GET"
host = var.host_address
custom_headers = {
Content-Type = "application/json"
}
}
}
env/staging/variables.tf
...
...
variable "host_address" {
type = string
}
variable "main_domain" {
type = string
}
locals {
health_checks = {
checks-1 = {
api_name = "Name Staging Test"
api_method = "GET"
host = var.host_address
custom_headers = {
Content-Type = "application/json"
}
checks-2 = {
api_name = "Name Staging Test 2"
api_method = "GET"
host = var.main_domain
custom_headers = {
Content-Type = "application/json"
}
}
}
checks-3 = {
api_name = "Name Staging Test 3"
api_method = "GET"
host = "www.anything.com"
custom_headers = {
Content-Type = "application/json"
}
}
}
When I add locals to env folders, terraform doesn't respect that, it only refers to the local in modules, which I understand because local doesn't go outside of modules. And I can't use variables because I need to refer to other variables in this block. That's why I used local. So what would be the best approach to this?
So personally I am also with Matthews view in the comments. that this should be a variable for the module. It should just provide the abstraction of what a health check is. Then the envs should pass in the health checks they want. You can even define common healthcheck that might be required in all envs.
foo module
Then in your root module you could have something like
So each env then passes its own healthchecks but can also use common ones etc. This gives an output like below and you can see different envs can have different checks or common ones