我可以传递ignore_changes
给 terraform 模块吗?就我而言,我不想在更新 AMI 时更新自动缩放组。
简单回顾了一下,好像做不到——https: //github.com/hashicorp/terraform/issues/21546
当然,我可以有两个复制粘贴的模块版本——一个有ignore_changes
一个没有,但看起来不太好。也许我只是错过了什么?
我可以传递ignore_changes
给 terraform 模块吗?就我而言,我不想在更新 AMI 时更新自动缩放组。
简单回顾了一下,好像做不到——https: //github.com/hashicorp/terraform/issues/21546
当然,我可以有两个复制粘贴的模块版本——一个有ignore_changes
一个没有,但看起来不太好。也许我只是错过了什么?
As of 2-16-2022, it seems this isn't possible. There are two issues documenting this on GitHub, one as you pointed out here and another more generic issue about handling interpolation in lifecycle attributes at: https://github.com/hashicorp/terraform/issues/3116 . As of now, the reasoning seems to be:
@phinze:
The real issue here is that lifecycle blocks cannot contain interpolated values. This is because lifecycle modifications can change the shape of the graph, which makes handling computed values for them extra tricky. It's something we can theoretically do, but requires some thought and effort. Tagging as an enhancement.
For a workaround (albeit not optimal), we can hardcode the values into ignore_changes
on the module scope and then use count
with a variable e.g. create_resource_with_ignore_changes = 1
to get our resource provisioned with ignore_changes
from the module scope. I know this isn't what the question requested, instead this is hardcoding ignore_changes
into the module and provisioning it via a count switch. Below is an example of how this could work:
variables.tf
variable "create_resource_with_ignore_changes" {
type = number
description = "Choose whether to create a version that uses hardcoded ignore_changes"
default = 1
}
calling-a-module.tf
module "servers" {
source = "./app-cluster"
create_resource_with_ignore_changes = var.create_resource_with_ignore_changes
}
inside-servers-module.tf
resource "a_terraform_resource" "example" {
count = var.create_resource_with_ignore_changes
# ...
lifecycle {
ignore_changes = [
# your hardcoded changes to ignore here
]
}
}
A benefit of this approach is that you can have different configurations by still using a single module. You can also nest other logic into the count argument, for instance, via creating a string and determining if it matches using a ternary operator:
resource "a_terraform_resource" "example" {
count = var.my_resource_config == "someHardcodedNamedConfig" ? 1 : 0
# ...
}
This also got me thinking if it's possible just to conditionally control the lifecycle
block. There's a SO post which answers that question here: Terraform conditionally apply lifecycle block
Unfortunately this also isn't possible, for similar reasons to the explanation to this particular question, making the duplicate resource (one with and one without the lifecycle
block) the current feasible workaround.