首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terragrunt路径解析-局部引用模块-分裂成多个环境

Terragrunt路径解析-局部引用模块-分裂成多个环境
EN

Stack Overflow用户
提问于 2020-08-31 13:11:40
回答 1查看 5.4K关注 0票数 2

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

为了让生活变得更简单,我已经构建了当前基础设施的一小部分,只是用一个模块来加速开发。

该项目的当前结构如下:

代码语言:javascript
复制
├── 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配置文件的重要部分:

代码语言:javascript
复制
remote_state {}

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

  {...}
}

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

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

代码语言:javascript
复制
module my_module {
  source = "../../terraform-modules/terraform-aws-*"

  {...}
}

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

代码语言:javascript
复制
module my_module {
  source = "../../terraform-modules/terraform-aws-*"

  {...}
}

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

代码语言:javascript
复制
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模块)。

代码语言:javascript
复制
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,从

代码语言:javascript
复制
module ecr_lz_ingestion {
  source = "../../terraform-modules//terraform-aws-ecr"

{<MODULE_ARGUMENTS>}
  }
}

至:

代码语言:javascript
复制
module ecr_lz_ingestion {
  source = "../../../../../../../../terraform-modules//terraform-aws-ecr"

{<MODULE_ARGUMENTS>}
  }
}

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

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

EN

回答 1

Stack Overflow用户

发布于 2020-08-31 18:55:31

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

代码语言:javascript
复制
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文件之间共享的代码。

它可以包括这样的内容:

代码语言:javascript
复制
remote_state {}

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

代码语言:javascript
复制
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问题出发,最好将双斜杠移到模块共享路径的某个位置。或者只是将模块合并到一个文件夹中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63671501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档