在我的一些项目中,我将包括一个名为gabrielle的terraform模块。在某些项目中,我将不包括terraform模块gabrielle。
我有另一个名为s3-bucket的terraform模块,它将驻留在gabrielle模块也可以使用的同一个项目中。我希望将s3-bucket terraform模块中的变量设置为true或false,具体取决于gabrielle模块是否也在同一个项目中(如果存在,则为true;如果没有,则为false )。
让我们调用我的布尔变量,它将位于s3-bucket模块gabrielle_enabled中。
换句话说,在terraform s3-bucket模块中,我想检查terraform gabrielle模块是否存在于同一个项目中,如果存在,则将s3-bucket模块中的bool设置为true。
听起来比较容易,但到目前为止我还不知道怎么做。
谢谢你的帮助!
发布于 2022-04-28 23:23:46
可以将表示一个模块的对象传递到另一个模块:
module "gabrielle" {
source = "./modules/gabrielle"
# ...
}
module "s3-bucket" {
source = "./modules/s3-bucket"
gabrielle = modules.gabrielle
# ...
}在./modules/s3-bucket中,您可以将这个变量声明为具有gabrielle的任何属性对s3-bucket的行为都很重要的对象类型
variable "gabrielle" {
type = object({
# If the gabrielle module has an output value
# named "example" which you'll use inside
# s3-bucket, you can declare an attribute for
# it like this.
example = string
})
default = null
}我有意将这个变量声明为default = null,因为这样可以选择在module "s3-bucket"块中指定,如果没有指定,那么它的值将为null。
然后,您可以在s3-bucket模块中声明一个本地值,当一个gabrielle对象是提供程序时,它是true:
locals {
using_gabrielle = var.gabrielle != null
}在模块的其他地方,您可以使用local.using_gabrielle根据该对象是否被传递来做出决策。
https://stackoverflow.com/questions/72046000
复制相似问题