amazon-web-services - 在 AWS 安全组中创建多个规则

标签 amazon-web-services terraform terraform-provider-aws

我尝试创建一个具有多个入站规则的 AWS 安全组,通常我们需要在 sg 中为多个入站规则创建多个入口。我没有单独创建多个入口规则,而是尝试创建入口列表,以便我可以轻松地将模块重用于不同的应用程序。
PFB,
模块/sg/sg.tf >>

resource "aws_security_group" "ec2_security_groups" {
  name   = var.name_security_groups
  vpc_id = var.vpc_id
}
模块/sg/rules.tf >>
resource "aws_security_group_rule" "ingress_rules" {
  count             = lenght(var.ingress_rules)
  type              = "ingress"
  from_port         = var.ingress_rules[count.index][0]
  to_port           = var.ingress_rules[count.index][1]
  protocol          = var.ingress_rules[count.index][2]
  cidr_blocks       = var.ingress_rules[count.index][3]
  description       = var.ingress_rules[count.index][4]
  security_group_id = aws_security_group.ec2_security_groups.id
}
模块/sg/variable.tf >>
variable "vpc_id" {
}
variable "name_security_groups" {
}
variable "ingress_rules" {
    type = list(string)
}
在应用程序文件夹中,
应用程序/dev/sg.tf >>
module "sg_test" {
  source = "../modules/sg"

  vpc_id                   = "vpc-xxxxxxxxx"
  name_security_groups = "sg_test"
  ingress_rules                     = var.sg_ingress_rules 
}
应用程序/dev/variable.tf >>
variable "sg_ingress_rules" {
    type        = list(string)
    default     = {
        [22, 22, "tcp", "1.2.3.4/32", "test"]
        [23, 23, "tcp", "1.2.3.4/32", "test"]
    }
}
错误:
Error: Missing attribute value

  on test-sgs.tf line 21, in variable "sg_ingress_rules":
  20: 
  21: 
  22: 

Expected an attribute value, introduced by an equals sign ("=").
请帮助纠正这个问题,或者如果有任何其他方法,请提出建议。
问候,

最佳答案

谢谢@apparentlymart,他在 Terraform 讨论中帮助解决了这个问题
安全规则:-

resource "aws_security_group_rule" "ingress_rules" {
  count = length(var.ingress_rules)

  type              = "ingress"
  from_port         = var.ingress_rules[count.index].from_port
  to_port           = var.ingress_rules[count.index].to_port
  protocol          = var.ingress_rules[count.index].protocol
  cidr_blocks       = [var.ingress_rules[count.index].cidr_block]
  description       = var.ingress_rules[count.index].description
  security_group_id = aws_security_group.ec2_security_groups.id
}
和变量:
variable "sg_ingress_rules" {
    type = list(object({
      from_port   = number
      to_port     = number
      protocol    = string
      cidr_block  = string
      description = string
    }))
    default     = [
        {
          from_port   = 22
          to_port     = 22
          protocol    = "tcp"
          cidr_block  = "1.2.3.4/32"
          description = "test"
        },
        {
          from_port   = 23
          to_port     = 23
          protocol    = "tcp"
          cidr_block  = "1.2.3.4/32"
          description = "test"
        },
    ]
}

关于amazon-web-services - 在 AWS 安全组中创建多个规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62575544/

相关文章:

amazon-web-services - Terraform 上的 AWS - 如何避免 'forces new resource'

amazon-web-services - AWS : add second internal load balancer to elastic beanstalk in terraform

php - 在 AWS EC2 上重新启动 httpd 会出现异常。是不是要把/var/www/html的内容删掉?

amazon-web-services - 如何设置 InputStream 内容长度

google-cloud-platform - 是否可以在 Terraform 中更新 GCP Cloud Functions 的源代码?

terraform - Switch terraform 0.12.6 to 0.13.0 give me provider ["registry.terraform.io/-/null"] 是必需的,但它已被删除

amazon-web-services - 如何将存储桶所有者完全控制权授予从一个帐户中的 redshift 卸载到另一个帐户中的 s3 存储桶的文件?

amazon-web-services - CloudFormation 在 VPC 内?

amazon-web-services - 当它说必须更换数据库实例时应用 Terraform 计划是否安全?

amazon-ec2 - Terraform - 从 aws 实例资源输出实例 ID - 一次一个