gpt4 book ai didi

terraform - 如何在 Terraform 文件中使用 terragrunt.hcl 中定义的局部变量?

转载 作者:行者123 更新时间:2023-12-05 05:45:03 30 4
gpt4 key购买 nike

我创建了这个文件夹结构:

.
├── main.tf
└── terragrunt.hcl
# FILE: terragrunt.hcl

include {
path = find_in_parent_folders()
}

locals {
common_vars = read_terragrunt_config(find_in_parent_folders("common.hcl"))
cluster_name = local.common_vars.locals.cluster_name
}

terraform {
source = "./main.tf"
}
# FILE: main.tf

module "tags" {
source = "..."

eks_cluster_names = [local.cluster_name]
}

module "vpc" {
source = "..."

aws_region = local.common_vars.locals.aws_region

...

vpc_custom_tags = module.tags.vpc_eks_tags

...
}

但是对于每个 local. 我正在尝试使用我得到一个错误:

A local value with the name "blabla" has not been declared

所以现在我想找出一种方法来完成这项工作。我考虑过关注 how-to-access-terragrunt-variables-in-terraform-code ,但我不想创建 variables.tf。另外,另一个问题是我必须重新定义 main.tf 中模块的所有输出,难道没有更好的方法吗?

是否有我可以遵循的良好做法结构?我如何才能将 terragrunt.hcl 中的这些局部变量“传播”到 main.tf

最佳答案

很抱歉让您失望,但您必须创建一个 variables.tf - 这是标准的地形。您可以在其中为 terraform 配置定义所需的输入变量,并在 terragrunt 中填充这些变量。所以你的 terragrunt 文件应该是这样的:

# FILE: terragrunt.hcl  
locals {
common_vars = read_terragrunt_config(find_in_parent_folders("common.hcl"))
cluster_name = local.common_vars.locals.cluster_name
}

terraform {
source = "./main.tf"
}

inputs = {
cluster_name = local.cluster_name
aws_region = local.common_vars.locals.aws_region
}

你的 terraform main 应该是这样的:

# FILE: main.tf

module "tags" {
source = "..."

eks_cluster_names = var.cluster_name
}

module "vpc" {
source = "..."

aws_region = var.aws_region

...

vpc_custom_tags = module.tags.vpc_eks_tags

...
}

然后您的 variables.tf 将如下所示:

variable "aws_region" {
type = string
}
variable "cluster_name" {
type = string
}

此外,您可能还需要创建一个 provider.tf 和一个后端配置来让它运行。

关于terraform - 如何在 Terraform 文件中使用 terragrunt.hcl 中定义的局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71469847/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com