terragrunt.hcl
文件的相对路径标记每个AWS资源?理想情况下,该解决方案(如果存在)也将与本地/相对引用的模块一起使用(而不仅仅是来自GIT回购的模块)。
# In root terragrunt.hcl
locals {
# ...
aws_default_tags = jsonencode({ # This is line 48 in the error below.
ManagedBy = "Terraform"
TerraformBasePath = path.cwd # What is a (working) equivalent of this?
)}
}
generate "provider" {
# ...
contents = <<EOF
provider "aws" {
# ...
default_tags {
tags = jsondecode(<<INNEREOF
${local.aws_default_tags}
INNEREOF
)
}
}
EOF
}
在
terragrunt apply
上的错误,root上述错误:
terragrunt.hcl
通过简化问题中的第一个片段,将相对路径添加为标签:
嵌套函数可以使用一些简化。
这是一个很好的问题。我发现在资源标签中看到的IAC路径是“资源”配置所在的IAC路径。我测试的解决方案就是这样:
在root terragrunt.hcl文件中,来自project_name/terragrunt.hcl(推荐是使用root.hcl代替terragrunt.hcl:# In root terragrunt.hcl
locals {
# ...
# terraform-git-repo = "infrastructure" # For future use in CD pipeline.
terraform-git-repo = "/local/path/infra"
}
generate "provider" {
# ...
contents = <<EOF
provider "aws" {
# ...
default_tags {
tags = {
Terraform-base-path = replace(replace(path.cwd, "${local.terraform-git-repo}", ""), "/.terragrunt-cache/.*/", "")
}
}
}
EOF
}
Whather
replace
,locals {
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
}
inputs = merge(
local.account_vars.locals,
local.region_vars.locals,
local.environment_vars.locals,
{
custom_tags = "${local.aws_mandatory_tags}",
tags = "${local.aws_mandatory_tags}",
aws_mandatory_tags = "${local.aws_mandatory_tags}",
}
)
和custom_tags
是Terraform块中使用的Terraform模块的变量。
:
tags
aws_mandatory_tags
我的计划显示:
# For IaC relative path to be added in tags
full_path_iac_repo_for_tags = get_original_terragrunt_dir()
iac_repo_root = dirname(get_terragrunt_dir()) # this depends on the IaC repo directory structure
relative_path_iac_repo_for_tags = replace(local.full_path_iac_repo_for_tags, local.iac_repo_root, "")