我很难理解如何反映我在Terragrunt中习惯的本地Terraform模块实例化结构。以下面的Terraform结构为例,它创建了sns主题模块的不同实例。
module "sns_topic" {
source = "terraform-aws-modules/sns/aws"
version = "~> 3.0"
name = "my-topic"
}
module "sns_topic_two" {
source = "terraform-aws-modules/sns/aws"
version = "~> 3.0"
name = "my-topic-two"
content_based_deduplication = true
}
module "sns_topic_three" {
source = "terraform-aws-modules/sns/aws"
version = "~> 3.0"
name = "my-topic-three"
http_success_feedback_role_arn = x
http_success_feedback_sample_rate = y
http_failure_feedback_role_arn = z
}
...任何模块本身中的字段都可以填充该模块的给定实例。
我不明白如何在Terragrunt 通过他们的投入中实现这一点,因为在这些情况下,模块的每个实例都可能因字段的使用而有所不同。例如,在sns模块中,可以有使用content_based_deduplication的主题A和使用完全不同字段的lambda_success_feedback_role_arn和主题C的主题B,并且在环境中可能有100个不同的主题。在本机Terraform中,您只需实例化每个模块,如上面所示。但你怎么能通过Terragrunt做到这一点呢?
发布于 2021-10-23 10:55:54
(免责声明:下面的内容代表了我的理解&工作流,这可能不是确定的:)
我的理解是,虽然您可以在顶级模块中编写本机Terraform,但最佳实践是将其移动到另一个模块,即从terraform块调用terragrunt.hcl文件。此模块充当业务流程层。我认为,Terragrunt项目的顶层很可能代表了您希望在每个目录中使用相同资源的东西(例如dev、test、prod),因此使用这个编排模块可以使代码干涸,而不是在顶级目录之间重复使用。
因此,在您的例子中,我认为您需要编写一个模块,它看起来有点像这样,并将从您的terragrunt.hcl文件中被调用:
top_level/main.tf
variable "sns_topics" {
type = map(map(string))
}
module "sns_topic" {
source = "terraform-aws-modules/sns/aws"
version = "~> 3.0"
for_each = var.sns_topics
name = each.key
content_based_deduplication = lookup(each.value, "content_based_deduplication", null)
http_success_feedback_role_arn = lookup(each.value, "http_success_feedback_role_arn", null)
}然后,您的terragrunt.hcl中的terragrunt.hcl看起来可能有点像这样:
inputs = {
sns_topics = {
sns_topic = {},
sns_topic_two = {
content_based_deduplication = true
},
sns_topic_three = {
http_success_feedback_role_arn = "x"
}
}
}最后,您将调用top_level模块在terragrunt.hcl的terraform块中。
terraform {
source = "git::git@github.com:<your_account>/modules.git//top_level?ref=v0.0.1"
}注意:使用null作为lookup函数中的默认值,在特定主题映射不包含该特定键的情况下,应该会省略该参数。
Terragrunt文档中的此页将更多地讨论这种方法。
https://stackoverflow.com/questions/69683502
复制相似问题