微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让 terragrunt 将 tfvars 文件读入依赖模块

如何解决如何让 terragrunt 将 tfvars 文件读入依赖模块

有人知道如何让 terragrunt 将 tfvars 文件读入依赖模块吗?如果我在根 terragrunt.hcl 中将所有 tf​​var 声明为输入,则一切正常,但当然我无法按环境自定义它们。我尝试添加 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 个这样的警告,我在这里只粘贴了一个,但你明白了。似乎应该有一些方法可以用 terragrunt generate 块来解决这个问题,但我不确定如何。有什么想法吗?

解决方法

我一直在关注 documentation here,它建议使用目录结构:

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. 中,即:只是 app 目录。然后你可以根据不同的环境(甚至不同环境下的不同版本)定制模块应用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。