【问题标题】:Terragrunt path resolution - locally referenced modules - splitting into multiple environmentsTerragrunt 路径解析 - 本地引用的模块 - 拆分为多个环境
【发布时间】:2020-12-19 14:44:23
【问题描述】:

出于与业务相关的原因,我无法使用版本化模块拆分我的基础架构。由于拆分环境仍然有益,并且我想避免复制/粘贴根模块,它们基本上只是子模块的实例化,在多个目录中,每个目录代表自己的环境,我想指定的来源terragrunt.hcl

中的根模块

为了让生活更轻松,我构建了当前基础架构的一小部分,只需一个模块即可加快开发速度。

目前的项目结构是这样的:

├── config
│   ├── common.tfvars
│   └── terragrunt.hcl
├── environments
│   └── dev
├── infrastructure-modules
│   └── ecs
├── terraform-modules
│   ├── terraform-aws-ecr

我的所有基础架构都在infrastructure-modules/ecs/*.tf 文件中进行了描述,这些文件基本上只是实例化terraform-modules/terraform-aws-*/ 中声明的子模块。

有了它,我可以简单地从infrastructure-modules/ecs 目录执行 terragrunt(terraform 命令)。

为了有可能在另一个帐户中创建相同的环境,我引入了一个新目录 environments/dev/eu-central-1/ecs,如根目录的树输出所示。

environments/dev/eu-central-1/ecs 仅包含两个文件,terragrunt.hclcommon.tfvars

我想,common.tfvars 的用法是不言自明的,我的 terragrunt.hclremote_state {}terraform {} 块组成。

terragrunt配置文件的重要部分:

remote_state {}

terraform {
  source = "../../../../infrastructure-modules/ecs"

  {...}
}

上面我基本上是在引用我的根模块,在infrastructure-modules/ecs/*.tf 中声明。我的根模块正在实例化terraform-modules/terraform-aws-*/ 中声明的子模块。

infrastructure-modules/ecs/*.tf 的子模块被实例化如下:

module my_module {
  source = "../../terraform-modules/terraform-aws-*"

  {...}
}

在理想情况下,我可以从 environments/dev/eu-central-1/ecs 目录执行 terragrunt (terraform) 命令,但是由于我使用的是本地(相对)路径,因此在 初始化模块,作为根模块 my_module 加载具有以下相对路径的子模块:

module my_module {
  source = "../../terraform-modules/terraform-aws-*"

  {...}
}

这导致environments/dev/eu-central-1/ecs 中的模块实例化失败,因为相对路径不同,基于父模块实例化。

Initializing modules...
- my_module in 

Error: Unreadable module directory

Unable to evaluate directory symlink: lstat ../../terraform-modules: no such
file or directory

到目前为止,根据文档,path_relative_* 应该能够返回其包含块中指定的路径与当前 terragrunt.hcl 之间的相对路径,但这里的问题是我没有任何 @我的terragrunt.hcl 文件中有 987654339@ 块,因此这种方法不起作用。符号链接是最后的选择。

编辑

如果我检查路径environments/dev/eu-central-1/ecs 上的.terragrunt-cache/*,我可以确认所有“根”模块都已下载(复制)到缓存目录中。

但是,模块是这样实例化的,它会尝试从上面两级的目录中获取实际的模块(Terraform 模块)。

module my_modules {
  source = "../..//terraform-modules/terraform-aws-ecr"

所以基本上,我需要告诉 Terragrunt 从其他路径下载/获取模块。

编辑 2:

在我运行init 的目录中检查 .terragrunt-cache 显示,terraform-modules 从未下载到 terraform-infrastructure/environments/dev/eu-central-1/ecs/.terragrunt-cache/.

如果我将terraform-infrastructure/infrastructure-modules/ecs/ecr-repos.tf 更改为

module ecr_lz_ingestion {
  source = "../../terraform-modules//terraform-aws-ecr"

{<MODULE_ARGUMENTS>}
  }
}

到:

module ecr_lz_ingestion {
  source = "../../../../../../../../terraform-modules//terraform-aws-ecr"

{<MODULE_ARGUMENTS>}
  }
}

Terraform 能够初始化子模块,因为我在根目录中给出了 terraform-modules/ 的相对路径,这显然是一种解决方法。

不知何故,我希望 Terragrunt 下载两个目录,terraform-modulesinfrastructure-modules,以便模块实例化中的相对路径起作用。

【问题讨论】:

  • 你在模块路径中使用双斜杠吗?
  • 我尝试了// 和没有。

标签: amazon-web-services terraform terragrunt


【解决方案1】:

根据您提供的其他信息,我知道这是您当前的目录结构:

terraform-infrastructure
├── config
│   ├── common.tfvars
│   └── terragrunt.hcl
├── environments
│   └── dev
│       └── eu-central-1
│           └── ecs
│               └── terragrunt.hcl
├── infrastructure-modules
│   └── ecs
├── terraform-modules
│   ├── terraform-aws-ecr
│   
├── terragrunt.hcl

注意我添加的父目录中的terragrunt.hcl

terragrunt.hcl 被视为父文件,可以包含可以在其他 terragrunt 文件之间共享的代码。

它可以包括这样的东西:


remote_state {}

在您的 eu-central-1/ecs 文件夹中,将以下内容添加到您的 terragrunt 文件中:


include {
  // searches up the directory tree from the current terragrunt.hcl file 
  // and returns the absolute path to the first terragrunt.hcl
  path = find_in_parent_folders()
}

  // using the path relative from the path stated in the include block
terraform {
  source = "${path_relative_from_include()}//infrastructure-modules”
}

这应该在子模块实例化时保持相对路径不变。

编辑

根据您的 GitHub 问题,最好将双斜杠移动到模块共享公共路径的位置。或者只是将您的模块合并到一个文件夹中。

【讨论】:

  • 不,没有父 terrragrunt.hcl。但是,无论如何,Terraform 都会失败,因为它无法解析 `infrastructure/modules/ecs 中的模块。
  • 能否添加一个父 terragrunt 文件以将其用作锚点?
  • 整个文件夹集合的父目录是什么?
  • 我想我现在明白了。父目录是terraform-infrastructure,因为我在里面有其他与 docker 相关的目录。
  • 始终,并删除 .terragrunt-cache。打开 Github 问题后,我能够解决该问题。 github.com/gruntwork-io/terragrunt/issues/1324.Feel免费更新答案,我很乐意接受。
猜你喜欢
  • 2021-02-22
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 2012-05-27
  • 2018-01-29
  • 2014-08-21
相关资源
最近更新 更多