简体   繁体   English

aws_security_group 入口块使用动态块

[英]aws_security_group ingress block using dynmaic block

i think I should be able to do something like this.我想我应该能够做这样的事情。 but the map of ports, protocal, and cidrs is wrong... how do I make a map of lists and interate over the map.但是端口、协议和 cidrs 的地图是错误的......我如何制作列表地图并在地图上进行交互。

variable "master-sg-ingress-ports" {
  //depends_on [aws_security_group.master-lb-sg, aws_security_group.worker-sg]
  description = "List of port numbers for specific security group"
  type        = map(any)

  //  format should be [ sg1 =  [from_port, to_port, protocol, from_ip_cidr] ]  ]
  default     = [ "ingress1" =  [80, 80, "TCP", "0.0.0/0"],
                  "ingress2" =  [80, 80, "TCP", "::0/0"],
                  "ingress3" =  [443, 80, "TCP", "0.0.0.0/0"],
                  "ingress4" =  [443, 80, "TCP", "::0/0"],
                  "ingress5 "=  [0, 0, "-1", "172.30.0.0/16"],]
}

resource "aws_security_group" "master_sg" {
  depends_on  = [aws_security_group.master_lb_sg, aws_security_group.worker_sg]
  provider    = aws.region_master
  name        = "master-sg"
  description = "security group for Jenkins master"
  vpc_id      = aws_vpc.vpc_master.id


  dynamic "ingress" {
    # this for_each is not identical to for_each in line 21
    for_each = toset(var.master-sg-ingress-ports) # iterator can be (need to be) configured
    iterator = it                                 # set the name of the iterator, which can be any name, but "each" (!!)
    content {
      from_port   = it[0].value
      to_port     = it[1].value
      protocol    = it[2].value
      cidr_blocks = [it[3].value]
    }
  }
}

terraform init is giving me the following with a underline under ingress1 terraform init在 ingress1 下给我以下下划线

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
╷
│ Error: Invalid default value for variable
│ 
│   on security_groups.tf line 64, in variable "master-sg-ingress-ports":
│   64:   default     = [ "ingress1" =  [80, 80, "TCP", "0.0.0/0"],
│   65:                   "ingress2" =  [80, 80, "TCP", "::0/0"],
│   66:                   "ingress3" =  [443, 80, "TCP", "0.0.0.0/0"],
│   67:                   "ingress4" =  [443, 80, "TCP", "::0/0"],
│   68:                   "ingress5 "=  [0, 0, "-1", "172.30.0.0/16"],].
╵```

The correct default value is a map , not a list of maps as you have now.正确的默认值是map ,而不是您现在拥有的地图列表。 So it should be:所以应该是:

variable "master-sg-ingress-ports" {
 
  description = "List of port numbers for specific security group"
  type        = map(any)

  default     = { "ingress1" =  [80, 80, "TCP", "0.0.0.0/0"],
                  "ingress2" =  [80, 80, "TCP", "::/0"],
                  "ingress3" =  [443, 80, "TCP", "0.0.0.0/0"],
                  "ingress4" =  [443, 80, "TCP", "::/0"],
                  "ingress5 "=  [0, 0, "-1", "172.30.0.0/16"]}
}

Update for master_sg : master_sg更新:

resource "aws_security_group" "master_sg" {
 # depends_on  = [aws_security_group.master_lb_sg, aws_security_group.worker_sg]
 # provider    = aws.region_master
  name        = "master-sg"
  description = "security group for Jenkins master"
  vpc_id      = data.aws_vpc.default.id


  dynamic "ingress" {
    # this for_each is not identical to for_each in line 21
    for_each = var.master-sg-ingress-ports 
    content {
      from_port   = ingress.value[0]
      to_port     = ingress.value[1]
      protocol    = ingress.value[2]
      cidr_blocks = [ingress.value[3]]
    }
  }
}

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

相关问题 条件表达式在 aws_security_group 资源出口块 terraform 中不起作用 - Conditional Expression not working in aws_security_group resource egress block terraform Terraform:成功创建资源(aws_security_group),但它采用来自所有给定安全组的入口/出口规则 - Terraform: create resource(aws_security_group) successfully but it takes ingress/egress rules from all given security groups 使用动态块通过 Terraform 引用 AWS 中的安全组 - Referencing Security Group in AWS via Terraform using Dynamic Block terraform aws_security_group 资源中的 CIDR 地址无效,文件中包含 cidr_blocks - invalid CIDR address in terraform aws_security_group resource with cidr_blocks from file 如何使用多个 eip 和 ec2 实例创建 terraform aws_security_group? - How can I create terraform aws_security_group with multiple eip and ec2 instances? AWS为特定安全组授权Ingress - AWS Authorize Ingress for a specific Security Group AWS 网络/安全组入口问题 - AWS networking / security group ingress question 撤销所有 AWS 安全组入口规则 - Revoke all AWS security group ingress rules 在安全组入口 cli 命令中使用变量 - Using a variable in security group ingress cli command JClouds在AWS EC2中创建默认安全组,如何阻止它? - JClouds creates default security group in AWS EC2, how can I block that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM