由于与业务相关的原因,我无法使用版本化模块来拆分我的基础设施。由于拆分环境仍然是有益的,我希望避免复制/粘贴根模块,这基本上只是子模块的实例化,在多个目录上,每个目录代表自己的环境,因此我想在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文件中没有任何include {}块,因此这种方法无法工作。Symlink是最后一个选项。
编辑
如果我检查路径上的.terragrunt-cache/* environments/dev/eu-central-1/ecs,就可以确认所有“根”模块都已下载(复制)到缓存目录中。
但是,该模块正在像这样实例化,它试图从上面的目录二级获取实际模块(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,以便模块实例化中的相对路径能够工作。
发布于 2020-08-31 18:55:31
根据您提供的其他信息,我理解这是您当前的目录结构:
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问题出发,最好将双斜杠移到模块共享路径的某个位置。或者只是将模块合并到一个文件夹中。
https://stackoverflow.com/questions/63671501
复制相似问题