简体   繁体   English

AWS Terraform - 对资源使用动态块

[英]AWS Terraform - using dynamic block on resource

I'm trying to write a Terraform module for AWS Security Groups with the dynamic block, but I'm having this error:我正在尝试使用动态块为 AWS 安全组编写 Terraform 模块,但出现此错误:

│ 
│   on main.tf line 17, in module "security_group":
│   17:     ingress = {
│ 
│ The argument "ingress" was already set at main.tf:8,5-12. Each argument may be set only once.

I've followed the documentation but I'm still having the error I'm using Terraform 0.15.1 and AWS provider version 3.38.0我已按照文档进行操作,但仍然遇到错误我正在使用 Terraform 0.15.1 和 AWS 提供商版本 3.38.0

Here is my code这是我的代码

./modules/security_group/main.tf ./modules/security_group/main.tf

resource "aws_security_group" "main" {
   .......

  dynamic "ingress" {
    for_each = var.ingress
    content {
      description      = ingress.value["description"]
      from_port        = ingress.value["from_port"]
      to_port          = ingress.value["to_port"]
      protocol         = ingress.value["protocol"]
      cidr_blocks      = ingress.value["cidr_blocks"]
      ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"]
    }
  
  }
  .......

}

./modules/security_group/variables.tf ./modules/security_group/variables.tf

variable "ingress" {
  description = ""
  type        = object({
    description = string
    from_port   = number
    to_port     = number
    protocol    = string
    cidr_blocks = list(string)
    ipv6_cidr_blocks = list(string)
  })
  default     = {
    description      = ""
    from_port        = 80
    to_port          = 80
    protocol         = "tcp"
    cidr_blocks      = []
    ipv6_cidr_blocks = []
  }
}

./main.tf ./main.tf

module "security_group" {
    source = "./modules/security_group"

    name        = "${var.project}-sg"
    description = "security group testing"
    vpc_id      = "my-vpc"
    ingress = {
        description = ""
        from_port = 22
        to_port   = 22
        protocol  = "tcp"
        cidr_blocks = []
        ipv6_cidr_blocks = []
    }
    ingress = {
        description = ""
        from_port = 80
        to_port   = 80
        protocol  = "tcp"
        cidr_blocks = []
        ipv6_cidr_blocks = []
    }

}

You have ingress arguments.您有ingress arguments。 I think you want to have one as a list:我想你想要一个作为列表:

variable "ingress" {
  description = ""
  type        = list(object({
    description = string
    from_port   = number
    to_port     = number
    protocol    = string
    cidr_blocks = list(string)
    ipv6_cidr_blocks = list(string)
  }))
  default     = [{
    description      = ""
    from_port        = 80
    to_port          = 80
    protocol         = "tcp"
    cidr_blocks      = []
    ipv6_cidr_blocks = []
  }
}]


module "security_group" {
    source = "./modules/security_group"

    name        = "${var.project}-sg"
    description = "security group testing"
    vpc_id      = "my-vpc"
    ingress = [{
        description = ""
        from_port = 22
        to_port   = 22
        protocol  = "tcp"
        cidr_blocks = []
        ipv6_cidr_blocks = []
       }, {
        description = ""
        from_port = 80
        to_port   = 80
        protocol  = "tcp"
        cidr_blocks = []
        ipv6_cidr_blocks = []
    }]

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform 使用 aws_s3_bucket_logging 资源时出现“不支持的块类型”错误 - Terraform "Unsupported Block type" error when using aws_s3_bucket_logging resource Terraform:如何从一个资源块创建多个AWS子网? - Terraform: How to create multiple aws subnets from one resource block? 如何从带有附件块的Terraform aws_network_interface移到aws_network_interface_attachment资源 - How to move from Terraform aws_network_interface with attachment block to aws_network_interface_attachment resource Terraform 在同一资源中有一个 for_each 和动态块? 或者我可以在资源语句中有多个 for_each - Terraform to have a for_each and dynamic block in the same resource? Or can I have multiple for_each in a resource statement 使用 Terraform 在 AWS 上的现有资源上更新和 append 新属性 - Update and append new properties on exising resource on AWS using Terraform 如何使用 kubernetes_ingress terraform 资源创建 AWS ALB? - How to create AWS ALB using kubernetes_ingress terraform resource? 使用 Terraform 创建 AWS 资源组 - Create an AWS Resource Group with Terraform 条件表达式在 aws_security_group 资源出口块 terraform 中不起作用 - Conditional Expression not working in aws_security_group resource egress block terraform 无法配置 terraform 动态块 - Unable to configure terraform dynamic block Terraform - 动态块中的索引无效 - Terraform - invalid index in dynamic block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM