首页 > 解决方案 > 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 模块的输入,但前提是它处于相同的环境中。

此致。

标签: amazon-web-servicesterraformterragrunt

解决方案


在上面显示的目录结构中,您在 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 文档。


推荐阅读