【发布时间】: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.hcl 和 common.tfvars。
我想,common.tfvars 的用法是不言自明的,我的 terragrunt.hcl 由 remote_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-modules 和 infrastructure-modules,以便模块实例化中的相对路径起作用。
【问题讨论】:
-
你在模块路径中使用双斜杠吗?
-
我尝试了
//和没有。
标签: amazon-web-services terraform terragrunt