如何在Terragrunt工作区之间传递变量



我是terragrunt的新手,试图弄清楚如何在模块之间传递变量。我在这里遵循terragrunt文档,但是没有运气,并且出现错误:

apps tg run-all plan    
INFO[0000] The stack at /Users/xxx/sri/aws/Infrastructure/apps will be processed in the following order for command plan:
Group 1
- Module /Users/xxx/sri/aws/Infrastructure/apps/networks-vpc
- Module /Users/xxx/sri/aws/Infrastructure/apps/sri-s3-bucket
Group 2
- Module /Users/xxx/sri/aws/Infrastructure/apps/ec2-amz-lnx

No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
module.vpc.aws_vpc.vpc: Refreshing state... [id=vpc-xxx]
module.vpc.aws_internet_gateway.igw: Refreshing state... [id=igw-xxx]
module.vpc.aws_subnet.public_subnet_a: Refreshing state... [id=subnet-xxx]
module.vpc.aws_route_table.private_b: Refreshing state... [id=rtb-xxx]
module.vpc.aws_route_table.public_b: Refreshing state... [id=rtb-xxx]
module.vpc.aws_subnet.private_subnet_b: Refreshing state... [id=subnet-xxx]
module.vpc.aws_route_table.public_a: Refreshing state... [id=rtb-xxx]
module.vpc.aws_subnet.public_subnet_b: Refreshing state... [id=subnet-xxx]
module.vpc.aws_route_table.private_a: Refreshing state... [id=rtb-xxx]
module.vpc.aws_subnet.private_subnet_a: Refreshing state... [id=subnet-xxx]
module.vpc.aws_security_group.sri_sg1: Refreshing state... [id=sg-xxx]
module.vpc.aws_route.r_a0: Refreshing state... [id=r-rtb-xxx]
module.vpc.aws_route.r_b0: Refreshing state... [id=r-rtb-xxx]
module.vpc.aws_security_group_rule.sri_sg1_ingress_rule1: Refreshing state... [id=sgrule-xxx]
module.vpc.aws_security_group_rule.sri_sg1_egress_rule1: Refreshing state... [id=sgrule-xxx]
module.vpc.aws_security_group_rule.sri_sg1_ingress_rule3: Refreshing state... [id=sgrule-xxx]
module.vpc.aws_security_group_rule.sri_sg1_ingress_rule2: Refreshing state... [id=sgrule-xxx]
module.vpc.aws_route_table_association.public_b: Refreshing state... [id=rtbassoc-xxx]
module.vpc.aws_route_table_association.private_a: Refreshing state... [id=rtbassoc-xxx]
module.vpc.aws_route_table_association.public_a: Refreshing state... [id=rtbassoc-xxx]
module.vpc.aws_route_table_association.private_b: Refreshing state... [id=rtbassoc-xxx]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
ERRO[0008] Module /Users/xxx/sri/aws/Infrastructure/apps/ec2-amz-lnx has finished with an error: /Users/xxx/sri/aws/Infrastructure/apps/networks-vpc/terragrunt.hcl is a dependency of /Users/xxx/sri/aws/Infrastructure/apps/ec2-amz-lnx/terragrunt.hcl but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block.  prefix=[/Users/xxx/sri/aws/Infrastructure/apps/ec2-amz-lnx] 
ERRO[0008] 1 error occurred:
* /Users/xxx/sri/aws/Infrastructure/apps/networks-vpc/terragrunt.hcl is a dependency of /Users/xxx/sri/aws/Infrastructure/apps/ec2-amz-lnx/terragrunt.hcl but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block.

ERRO[0008] Unable to determine underlying exit code, so Terragrunt will exit with error code 1 
My Folder structure:
aws tree
.
├── Infrastructure
│   ├── README.md
│   ├── apps
│   │   ├── ec2-amz-lnx
│   │   │   ├── graph.svg
│   │   │   ├── main.tf
│   │   │   ├── outputs.tf
│   │   │   ├── providers.tf
│   │   │   ├── terragrunt.hcl
│   │   │   ├── variables.tf
│   │   │   └── versions.tf
│   │   ├── graph.svg
│   │   ├── networks-vpc
│   │   │   ├── main.tf
│   │   │   ├── outputs.tf
│   │   │   ├── providers.tf
│   │   │   ├── terragrunt.hcl
│   │   │   ├── variables.tf
│   │   │   └── versions.tf
│   │   ├── sri-s3-bucket
│   │   │   ├── main.tf
│   │   │   ├── providers.tf
│   │   │   ├── terragrunt.hcl
│   │   │   ├── variables.tf
│   │   │   ├── versions.tf
│   │   │   └── website
│   │   │       └── index.html
│   │   └── website-s3
│   ├── binaries
│   │   ├── darwin
│   │   │   ├── terraform
│   │   │   └── terragrunt
│   │   ├── linux
│   │   │   ├── terraform
│   │   │   └── terragrunt
│   │   └── windows
│   │       ├── terraform.exe
│   │       └── terragrunt.exe
│   ├── environments
│   │   ├── ap-southeast-2
│   │   │   ├── npe
│   │   │   │   ├── apps
│   │   │   │   │   ├── ec2-amz-lnx.tfvars
│   │   │   │   │   ├── networks-vpc.tfvars
│   │   │   │   │   └── s3.tfvars
│   │   │   │   └── environment.hcl
│   │   │   └── prod
│   │   └── us-east-2
│   ├── modules
│   │   ├── ec2
│   │   │   ├── data.tf
│   │   │   ├── files
│   │   │   │   └── user_data_lx_amazon.tpl
│   │   │   ├── iam.tf
│   │   │   ├── main.tf
│   │   │   ├── outputs.tf
│   │   │   └── variables.tf
│   │   ├── s3
│   │   │   ├── main.tf
│   │   │   ├── outputs.tf
│   │   │   └── variables.tf
│   │   └── vpc
│   │       ├── main.tf
│   │       ├── outputs.tf
│   │       └── variables.tf
│   └── terragrunt.hcl
├── test.jpg

因此,我在模块文件夹下为本地的vpc, ec2, s3编写了地形模块,这些模块由应用程序(工作区)和配置/tfvars用于基于区域和环境的环境,如上图所示。

正如您在上面看到的,terragrunt将network -vpc放在组1中,将ec2-amz-lnx放在组2中。我已经确保我已经在VPC中声明了输出(如下所示)

Outputs:
SecurityGroupID = "sg-xxx"
private_subs = {
"private_subnet_a" = "subnet-xxx"
"private_subnet_b" = "subnet-xxx"
}
public_subs = "subnet-xxx"
vpc_id = "vpc-xxx"

在ec2-amz-lnx文件夹中,我创建了terragrunt。HCL文件:

include {
path = find_in_parent_folders()
expose = true
}
dependency "shared_infra" {
config_path = "../networks-vpc"
# mock_outputs = {
#   public_subs = "subnet-xxx"
# }
}
dependencies {
paths = ["../networks-vpc"]
}
inputs = {
public_subs = dependency.shared_infra.outputs.public_subs
}

和在ec2-amz-lnx中,我像下面这样传递public_sub值:

module "ec2" {
source = "../../modules/ec2"
vpc_public_subnet_id = public_subs
instance_type_lx      = var.instance_type_lx_value
security_group_ids     = var.security_group_ids_value
ec2keypair_name     = var.ec2keypair_name_value
instance_count            = var.instance_count_value
}

我花了一些时间和terrgarunt一起挖掘。

对于错误"要么目标模块尚未应用,要么模块没有输出。如果这是预期的,设置skip_outp…">

这个短语的问题是:如果你有模块A和模块B并且模块B依赖于模块A,那么==>您必须部署模块B,然后才能部署模块A。否则terragrunt无法从模块A找到输出(变量)并将其传递给模块B,因此您无法同时部署模块A和模块B。你需要两个独立的步骤。哦. ."模拟outputs"不能解决问题。

这使得terragrunt现在有点没用了。

纯地形没有这样的问题。

最新更新