如何在terraform中的其他模块中调用for_each输出值

问题描述 投票:0回答:1

如何在terraform中调用

for_each
输出值到其他模块

我试图在其他模块(ec2-instance)中调用 VPC 的输出,但它不起作用..

我正在粘贴我的 vpc 模块、vpc 输出、

variables.tf
.tfvars
文件 我在 ec2-instance 模块中调用这些 vpc 输出值

# availability_zone = module.vpc.availability_zone
# subnet_id         = module.vpc.public_subnet_ids

专有网络模块:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.2.0"


 for_each = var.vpcs
  

  name                = each.value["vpc-name"]
  cidr                = each.value["vpc-cidr"]   
  azs                 = each.value["vpc-azs"]
  private_subnets     = each.value["vpc-pvt-sub"]
  public_subnets      = each.value["vpc-pub-sub"]
  
  

  public_subnet_tags = {
    Type = var.public_subnet_tags["Name"]
  }

  private_subnet_tags = {
    Type = var.private_subnet_tags["Name"]
  }

  vpc_tags = {
    Name = "Krish-VPC"
  }
}

变量.tf:

variable "vpcs" {
  type = map(
    object(
    {
    vpc-name              = string
    vpc-cidr              = string
    vpc-azs               = list(string)
    vpc-pvt-sub           = list(string)
    vpc-pub-sub           = list(string)
    enable_nat            = bool
    single_nat            = bool
    dns_hostnames         = bool 
    dns_support           = bool
    public_ip             = bool

  }
 )
)
}
variable "public_subnet_tags" {
  type = map(string)
  default = {
    Name = "public-subnets"
  }
}

variable "private_subnet_tags" {
  type = map(string)
  default = {
    Name = "private-subnets"
  }
}

变量.tfvars

vpcs = {
    vpc-1 = {
vpc-name              = "Terraform-VPC"
    vpc-cidr              = "10.0.0.0/16"
    vpc-azs               = ["us-east-1a", "us-east-1b"]
    vpc-pvt-sub           = ["10.0.1.0/24", "10.0.2.0/24"]
    vpc-pub-sub           = ["10.0.101.0/24", "10.0.102.0/24"]
    enable_nat            = true
    single_nat            = true
    dns_hostnames         = true 
    dns_support           = true
    public_ip             = true
    }
}

输出.tf

output "vpc_ids" {
  value = { for key, value in module.vpc : key => value.vpc_id }
}

output "private_subnets" {
   value = { for key, value in module.vpc : key => value.private_subnets }
 }
 output "public_subnet_ids" {
   value = { for key, value in module.vpc : key => value.public_subnets }
   
 }

 output "availability_zone" {
   value = { for key, value in module.vpc : key => value.azs }
 }

ec2 实例:

module "ec2-instance" {
  source = "terraform-aws-modules/ec2-instance/aws"

  for_each = var.instances

  ami                         = data.aws_ami.amazon_linux.id
  instance_type               = each.value.instance_type
  availability_zone           = module.vpc.availability_zone **#I tried giving like this but didnt work**
  subnet_id                   = module.vpc.public_subnet_ids  **#I tried giving like this but didnt work**
  vpc_security_group_ids      = [aws_security_group.main.id]
  associate_public_ip_address = true

}

这是我遇到的错误:

Error: Invalid value for input variable
│ 
│   on ec2-instance.tf line 17, in module "ec2-instance":
│   17:   availability_zone           = module.vpc.availability_zone
│ 
│ The given value is not suitable for module.ec2-instance.var.availability_zone declared at
│ .terraform/modules/ec2-instance/variables.tf:43,1-29: string required.
╵
Error: Invalid value for input variable
│ 
│   on ec2-instance.tf line 18, in module "ec2-instance":
│   18:   subnet_id                   = module.vpc.public_subnet_ids
│ 
│ The given value is not suitable for module.ec2-instance.var.subnet_id declared at .terraform/modules/ec2-instance/variables.tf:215,1-21:
│ string required.
terraform devops terraform-provider-aws
1个回答
0
投票

如果模块中的变量需要一个字符串,则您不能传递字符串以外的任何内容,否则您将收到所看到的错误,这是一个使用假模块的简单示例

variable "vpcs" {
  type = map(any)
  default = {
    a = {x = 1}
    b = {x = 2}
  }
}

module "vpc" {
  for_each = var.vpcs
  source   = "git::https://github.com/gruntwork-io/terraform-fake-modules.git//modules/aws/vpc?ref=main"
}

module "eks" {
  source = "git::https://github.com/gruntwork-io/terraform-fake-modules.git//modules/aws/eks?ref=main"
  vpc_id = module.vpc["a"].id
}

output "test" {
  value = module.eks
}

您可以看到如何

vpc_id = module.vpc["a"].id
这就是我们如何获得一件物品




如果两个模块都有for循环,那就有点复杂了,第一个映射和第二个元素映射中的键必须存在某种关系,这里是一个例子


variable "vpcs" {
  type = map(any)
  default = {
    a = {x = 1}
    b = {x = 2}
  }
}

variable "instances" {
  type = map(any)
  default = {
    x = {vpc = "a"}
    y = {vpc = "b"}
    z = {vpc = "a"}
  }
}

module "vpc" {
  for_each = var.vpcs
  source   = "git::https://github.com/gruntwork-io/terraform-fake-modules.git//modules/aws/vpc?ref=main"
}

module "eks" {
  for_each = var.instances
  source   = "git::https://github.com/gruntwork-io/terraform-fake-modules.git//modules/aws/eks?ref=main"
  vpc_id   = module.vpc[each.value.vpc].id
}

output "test" {
  value = module.eks
}

© www.soinside.com 2019 - 2024. All rights reserved.