terraform - 如何让 terragrunt 将 tfvars 文件读入依赖模块
问题描述
任何人都知道如何让 terragrunt 将 tfvars 文件读入依赖模块?如果我在根 terragrunt.hcl 中将所有 tfvar 声明为输入,则一切正常,但当然我不能按环境自定义它们。我尝试添加extra_arguments
块,但变量未在根模块中声明。它们在依赖模块中声明,我不想在两个地方都声明它们。
这是我的设置:
// terraform/terragrunt.hcl
terraform {
extra_arguments "common_vars" {
commands = ["plan", "apply"]
arguments = [
"-var-file=${find_in_parent_folders("account.tfvars")}",
"-var-file=./terraform.tfvars"
]
}
}
locals {
environment_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
bucket = local.environment_vars.locals.bucket
}
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
bucket = local.bucket
}
}
dependencies {
paths = ["../../../shared/services", "../../../shared/core"]
}
// terraform/accounts/dev/account.tfvars
aws_region = "us-east-1"
// terraform/accounts/dev/william/terraform.tfvars
aws_vpc_cidr = "10.1.0.0/16"
// terraform/accounts/dev/william/terragrunt.hcl
include {
path = find_in_parent_folders()
}
这不起作用,因为变量值实际上并没有传递给依赖模块。当我试图运行一个 terragrunt 计划时,我得到了这个:
''' TERMINAL OUTPUT
$ terragrunt plan
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
Warning: Value for undeclared variable
The root module does not declare a variable named
"aws_region" but a value was found in file
"/Users/williamjeffries/code/parachute/infrastructure/terraform/accounts/dev/account.tfvars".
To use this value, add a "variable" block to the configuration.
Using a variables file to set an undeclared variable is deprecated and will
become an error in a future release. If you wish to provide certain "global"
settings to all configurations in your organization, use TF_VAR_...
environment variables to set these instead.
实际上有 26 个这样的警告,我在这里只粘贴了一个,但你明白了。似乎应该有某种方法可以用 terragruntgenerate
块解决这个问题,但我不确定如何。有任何想法吗?
解决方案
我一直在关注这里的文档,它建议具有目录结构:
live
├── prod
│ ├── app
│ │ └── terragrunt.hcl
│ ├── mysql
│ │ └── terragrunt.hcl
│ └── vpc
│ └── terragrunt.hcl
├── qa
│ ├── app
│ │ └── terragrunt.hcl
etc...
和
# content of qa/app/terragrunt.hcl
terraform {
# Deploy version v0.0.3 in qa
source = "git::git@github.com:foo/modules.git//app?ref=v0.0.3"
}
inputs = {
# tfvars for qa
instance_count = 3
instance_type = "t2.micro"
}
和
# content of prod/app/terragrunt.hcl
terraform {
# Deploy version v0.0.3 in prod
source = "git::git@github.com:foo/modules.git//app?ref=v0.0.3"
}
inputs = {
# tfvars for prod
instance_count = 20
instance_type = "t2.2xlarge"
}
然后源可以在同一个git repo中。即:只是应用程序目录。然后您可以通过不同的环境(甚至不同环境中的不同版本)自定义模块应用程序
推荐阅读
- batch-file - 如何使用 cmd.exe 批处理文件(for 循环)将 JSON 语法转换为另一种语法
- javascript - TypeError:food.map 不是 Reactjs 中的函数
- c# - EF core gives error after adding a function inside "Where" lambda expression [EF-CORE 3.1]
- javascript - export html to PDF in JSPDF
- sql - Masking ID and Name in SQL
- android - Google Play Internal Testing App Rejected
- powershell - PowerShell - get-hotfix on multiple servers - error handling
- unity3d - Unity Rig 重量不会通过动画剪辑立即更新
- elasticsearch - 如何在无痛脚本中深度克隆对象字段
- ionic-framework - 每当我登录我的应用程序时,Ionic 框架(网络)都显示没有互联网连接