Ala*_*Kis 6 amazon-web-services terraform terragrunt
由于业务相关的原因,我无法使用版本化模块来拆分我的基础设施。由于分割环境仍然是有益的,并且我想避免复制/粘贴根模块,这些模块基本上只是子模块的实例化,在多个目录中,每个目录代表自己的环境,所以我想指定terragrunt.hcl中的根模块
\n为了让生活更轻松,我构建了当前基础设施的一小部分,仅使用单个模块来加快开发速度。
\n该项目当前的结构如下所示:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common.tfvars\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 terragrunt.hcl\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 environments\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dev\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 infrastructure-modules\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ecs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 terraform-modules\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 terraform-aws-ecr\nRun Code Online (Sandbox Code Playgroud)\n我的所有基础设施都在文件中描述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配置文件的重要部分:
\nremote_state {}\n\nterraform {\n source = "../../../../infrastructure-modules/ecs"\n\n {...}\n}\nRun Code Online (Sandbox Code Playgroud)\n上面我基本上引用了我的根模块,在infrastructure-modules/ecs/*.tf. 我的根模块正在实例化terraform-modules/terraform-aws-*/.
子模块的infrastructure-modules/ecs/*.tf实例化如下:
module my_module {\n source = "../../terraform-modules/terraform-aws-*"\n\n {...}\n}\nRun Code Online (Sandbox Code Playgroud)\n在理想的情况下,我将能够从environments/dev/eu-central-1/ecs目录执行 terragrunt (terraform) 命令,但由于我使用本地(相对)路径,因此在模块初始化期间会失败,因为根模块my_module使用以下命令加载子模块以下相对路径:
module my_module {\n source = "../../terraform-modules/terraform-aws-*"\n\n {...}\n}\nRun Code Online (Sandbox Code Playgroud)\n这导致模块实例化environments/dev/eu-central-1/ecs失败,因为基于父模块实例化的相对路径不同。
Initializing modules...\n- my_module in \n\nError: Unreadable module directory\n\nUnable to evaluate directory symlink: lstat ../../terraform-modules: no such\nfile or directory\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,根据文档path_relative_*, 应该能够返回其 include 块中指定的路径与当前 terragrunt.hcl 之间的相对路径,但这里的问题是我include {}的文件中没有任何块terragrunt.hcl的文件因此这种方法行不通。符号链接是最后一个选项。
编辑
\n如果我检查.terragrunt-cache/*路径,environments/dev/eu-central-1/ecs我可以确认所有“根”模块已下载(复制)到缓存目录中。
然而,模块是这样实例化的,它尝试从上面两层的目录中获取实际的模块(Terraform 模块)。
\nmodule my_modules {\n source = "../..//terraform-modules/terraform-aws-ecr"\nRun Code Online (Sandbox Code Playgroud)\n所以基本上,我需要告诉 Terragrunt 从其他路径下载/获取模块。
\n编辑2:
\n检查我运行的目录中的 .terragrunt-cacheinit显示,它们terraform-modules从未下载到 \n 中terraform-infrastructure/environments/dev/eu-central-1/ecs/.terragrunt-cache/。
如果我改变我的terraform-infrastructure/infrastructure-modules/ecs/ecr-repos.tf, 从
module ecr_lz_ingestion {\n source = "../../terraform-modules//terraform-aws-ecr"\n\n{<MODULE_ARGUMENTS>}\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n到:
\nmodule ecr_lz_ingestion {\n source = "../../../../../../../../terraform-modules//terraform-aws-ecr"\n\n{<MODULE_ARGUMENTS>}\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nTerraform 能够初始化子模块,因为我已经terraform-modules/在目录根中给出了相对路径,这显然是一种解决方法。
不知何故,我期望 Terragrunt 下载这两个目录,terraform-modules并infrastructure-modules让模块实例化中的相对路径正常工作。
根据您提供的附加信息,我了解到这是您当前的目录结构:
\nterraform-infrastructure\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common.tfvars\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 terragrunt.hcl\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 environments\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dev\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 eu-central-1\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ecs\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 terragrunt.hcl\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 infrastructure-modules\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ecs\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 terraform-modules\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 terraform-aws-ecr\n\xe2\x94\x82 \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 terragrunt.hcl\nRun Code Online (Sandbox Code Playgroud)\n请注意terragrunt.hcl我在父目录中添加的 。
这terragrunt.hcl被视为父文件,并且可以包含可在其他 terragrunt 文件之间共享的代码。
它可以包括这样的内容:
\n\nremote_state {}\n\nRun Code Online (Sandbox Code Playgroud)\n在您的eu-central-1/ecs文件夹中,将以下内容添加到 terragrunt 文件中:
\ninclude {\n // searches up the directory tree from the current terragrunt.hcl file \n // and returns the absolute path to the first terragrunt.hcl\n path = find_in_parent_folders()\n}\n\n // using the path relative from the path stated in the include block\nterraform {\n source = "${path_relative_from_include()}//infrastructure-modules\xe2\x80\x9d\n}\n\nRun Code Online (Sandbox Code Playgroud)\n当子模块实例化时,这应该保持相对路径完整。
\n编辑:
\n从您的 GitHub 问题来看,最好将双斜线移动到模块共享公共路径的位置。或者只是将您的模块合并到一个文件夹中。
\n| 归档时间: |
|
| 查看次数: |
10259 次 |
| 最近记录: |