Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services Terraform-对于每个对象,您提供了对象列表类型的值_Amazon Web Services_Terraform - Fatal编程技术网

Amazon web services Terraform-对于每个对象,您提供了对象列表类型的值

Amazon web services Terraform-对于每个对象,您提供了对象列表类型的值,amazon-web-services,terraform,Amazon Web Services,Terraform,我在尝试使用_each(这是对象列表的一种类型)时出现以下错误,但在动态块中同样可以正常工作 ingress_rules = [ {description: "Port 3306", cidr_blocks: ["10.0.0.0/24", "10.0.4.0/24"], port: 3306, protocol: "tcp"}, {description: "Port 22"

我在尝试使用_each(这是对象列表的一种类型)时出现以下错误,但在动态块中同样可以正常工作

  ingress_rules = [
    {description: "Port 3306", cidr_blocks: ["10.0.0.0/24", "10.0.4.0/24"], port: 3306, protocol: "tcp"},
    {description: "Port 22",   cidr_blocks: ["0.0.0.0/0"], port: 22, protocol: "tcp"},
    {description: "port 80",   cidr_blocks: ["0.0.0.0/0"], port: 80, protocol: "tcp"}
  ]

resource "aws_security_group" "security_group" {
  name = var.name

  dynamic "ingress" {
    for_each = var.ingress_rules

    content {
      description = ingress.value.description
      cidr_blocks = ingress.value.cidr_blocks
      from_port   = ingress.value.port
      to_port     = ingress.value.port
      protocol    = ingress.value.protocol
    }
  }
 }
以下是错误:

给定的“for_each”参数值不合适:“for_each”参数必须是映射或字符串集,并且您提供了对象列表类型的值。

这是我得到错误信息的模块

      vpc_subnets = [
        {name: "public_test_a", cidr_block: "10.0.0.0/28", map_public_ip_on_launch: true,  availability_zone: "ap-south-1a"},
        {name: "public_test_b", cidr_block: "10.0.0.16/28", map_public_ip_on_launch: true,  availability_zone: "ap-south-1b"},
        {name: "private_test_a", cidr_block: "10.0.0.32/28", map_public_ip_on_launch: false,  availability_zone: "ap-south-1a"},
        {name: "private_test_b", cidr_block: "10.0.0.48/28", map_public_ip_on_launch: false,  availability_zone: "ap-south-1b"}
      ]

# Create Subnets
resource "aws_subnet" "subnets" {
    for_each = var.vpc_subnets

    vpc_id = aws_vpc.vpc.id
    cidr_block = each.value.cidr_block
    map_public_ip_on_launch = each.value.map_public_ip_on_launch
    availability_zone = each.value.availability

    tags = merge({
      Name = each.value.name
    }, var.subnet_tags)
}
但在另一个模块中,它运行良好。唯一的区别是它位于动态块中

  ingress_rules = [
    {description: "Port 3306", cidr_blocks: ["10.0.0.0/24", "10.0.4.0/24"], port: 3306, protocol: "tcp"},
    {description: "Port 22",   cidr_blocks: ["0.0.0.0/0"], port: 22, protocol: "tcp"},
    {description: "port 80",   cidr_blocks: ["0.0.0.0/0"], port: 80, protocol: "tcp"}
  ]

resource "aws_security_group" "security_group" {
  name = var.name

  dynamic "ingress" {
    for_each = var.ingress_rules

    content {
      description = ingress.value.description
      cidr_blocks = ingress.value.cidr_blocks
      from_port   = ingress.value.port
      to_port     = ingress.value.port
      protocol    = ingress.value.protocol
    }
  }
 }
它不起作用,因为当用于创建资源时,只接受一个映射或一组字符串,而您正在传递一个映射列表

因此,您必须将其修改为仅使用地图:

# Create Subnets
resource "aws_subnet" "subnets" {

    for_each = {for idx, subnet in var.vpc_subnets: idx => subnet}

    vpc_id = aws_vpc.vpc.id
    cidr_block = each.value.cidr_block
    map_public_ip_on_launch = each.value.map_public_ip_on_launch
    availability_zone = each.value.availability

    tags = merge({
      Name = each.value.name
    }, var.subnet_tags)
}

在动态块中使用没有这样的限制,因此您可以在地图列表中进行迭代。

很酷,谢谢。你也能回答这个问题吗?我一直在尝试一种不同的方法。但我也会尝试这种方法。这是另一个问题@约翰多:没问题。如果答案有帮助,我们将不胜感激。