amazon-web-services - Terragrunt 使用来自其他环境的资源
问题描述
我想在另一个环境中使用资源,在这种情况下是 vpc 模块的输出。目标是在同一个 vpc 中使用 stage 和 dev 资源为客户降低成本。Stage 和 dev 具有单独的 ecs-cluster、asg、lc、ecr 等中的不同 docker 映像,但应该位于具有相同负载均衡器的同一个 vpc 中,然后是一个主机标头侦听器以转发到特定的目标组。两者都应该使用相同的数据库和相同的负载平衡器。
要求是有 n 个客户,每个客户都有阶段、开发和生产环境。所有客户文件夹都应包含三个环境。
我的文件夹结构是
├── Terraform
│ ├── Customer1
│ ├── Customer2
│ ├── Customer3
│ ├── Customer4
│ ├── Customer5
│ ├── Global
│ │ ├── iam
│ │ │ └── terragrunt.hcl
│ ├── README.md
│ └── Customer6
│ ├── non-prod
│ │ ├── eu-central-1
│ │ │ ├── dev
│ │ │ │ ├── cloudwatch
│ │ │ │ │ └── terragrunt.hcl
│ │ │ │ ├── ec2
│ │ │ │ │ └── terragrunt.hcl
│ │ │ │ ├── ecs
│ │ │ │ │ └── terragrunt.hcl
│ │ │ │ ├── lambda
│ │ │ │ │ └── terragrunt.hcl
│ │ │ │ ├── rds
│ │ │ │ │ └── terragrunt.hcl
│ │ │ │ ├── terragrunt.hcl
│ │ │ │ ├── vars.hcl
│ │ │ │ └── vpc
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── region.hcl
│ │ │ └── stage
│ │ │ ├── cloudwatch
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── ec2
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── ecs
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── lambda
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── rds
│ │ │ │ └── terragrunt.hcl
│ │ │ ├── terragrunt.hcl
│ │ │ ├── vars.hcl
│ │ │ └── vpc
│ │ │ └── terragrunt.hcl
│ │ └── terragrunt.hcl
│ └── prod
│ └── eu-central-1
│ ├── prod
│ │ ├── cloudwatch
│ │ │ └── terragrunt.hcl
│ │ ├── ec2
│ │ │ └── terragrunt.hcl
│ │ ├── ecs
│ │ │ └── terragrunt.hcl
│ │ ├── lambda
│ │ │ └── terragrunt.hcl
│ │ ├── rds
│ │ │ └── terragrunt.hcl
│ │ ├── terragrunt.hcl
│ │ ├── vars.hcl
│ │ └── vpc
│ │ └── terragrunt.hcl
│ └── region.hcl
└── Modules
├── cloudwatch
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── ec2
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── ecs
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── iam
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── lambda
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── rds
│ ├── Main.tf
│ ├── Outputs.tf
│ └── Variables.tf
├── vpc
│ ├── Main.tf
│ ├── Outputs.tf
│ ├── Variables.tf
└── vpc-stage
├── Main.tf
├── Outputs.tf
└── Variables.tf
我已经阅读了有关数据 terraform_remote_state 的信息,但那是在模块层。对我来说,在模块层执行此操作不是一个好方法,因为它仅适用于舞台环境。有没有办法从开发环境的 stage 文件夹中的 terragrunt.hcl 中获取远程状态的输出,以将其用作 ec2 模块的输入?
我用过
dependency "vpc" {
config_path = "../vpc"
}
接着
vpc_id = dependency.vpc.outputs.vpc_id
对于 ec2 模块的输入,但前提是它处于相同的环境中。
此致。
解决方案
在上面显示的目录结构中,您在 dev 和 stage 环境中都有一个 VPC。听起来您希望 dev 和 stage 共享一个 VPC,所以要做的第一件事就是将该 VPC 目录移到 dev 和 stage 之外。将 vpc 放在 下eu-west-1
,然后您可以根据需要将其用作 dev 和 stage 中的依赖项。
Customer6
│ ├── non-prod
└── eu-central-1
├── dev
│ └── ecs
├── stage
│ └── ecs
└── vpc
dependency "vpc" {
config_path = "../../vpc"
}
请参阅有关在模块之间传递输出的 Terragrunt 文档。
推荐阅读
- dynamics-crm - 用于操作 msdyn_BookingResource 和 msdyn_BookingResourceRequirement 的 Web Api 查询
- javascript - 在 contentEditable div 中获取光标位置,打开弹出窗口,然后在光标位置插入新的 html
- amp-html - 无法读取 AMP 页面中未定义的属性“创建”
- angular - 嵌套表单的角度反应表单问题。如何在ts文件中声明并访问html文件中的字段
- azure - 使用 Vnet 对等互连连接 Azure VM
- c# - DTO如何在内部绑定到实体框架中
- javascript - 拆分方法在 FileReader.result 上不可用
- c# - 如何创建多态indate类型c#
- c# - 如何让 WndProc 消息在 WPF 中工作?
- ruby-on-rails - 如何在 _form.html.erb 和 .controller 中设置“创建”以使用自嵌套路由进行单继承