作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了这个文件夹结构:
.
├── 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/
我是一名优秀的程序员,十分优秀!