How to use ingress multiple times in security group creation

Error: Attribute redefined

  on Security_group.tf line 15, in resource "aws_security_group" "mysite_sg":
  15:     ingress = [ {

The argument "ingress" was already set at Security_group.tf:3,5-12. Each
argument may be set only once.

I have already applied the ingress for port 22, now i need to add one more port 80 by using another ingress, but its not allowing me… throwing a error as i mentioned above.

Existing one:

ingress = [ {

      cidr_blocks = [ "0.0.0.0/0" ]

      description = ""

      from_port = 22

      ipv6_cidr_blocks = [ ]

      prefix_list_ids = [ ]

      protocol = "tcp"

      security_groups = [ ]

      self = true

      to_port = 22

    } ]

I am looking how to use ingress multiple times in single SG file.

The issue here is the ‘=’ and square brackets are obsolete. I had the same issue with vscode auto-populating the ingress argument.

Simply remove the ‘=’ and the square brackets and that will sort it. See below:

ingress {
  cidr_blocks = [ "0.0.0.0/0" ]
  description = ""
  from_port = 22
  ipv6_cidr_blocks = [ ]
  prefix_list_ids = [ ]
  protocol = "tcp"
  security_groups = [ ]
  self = true
  to_port = 22
} 

Hi,

I’m having the same issue.(Sorry to hijack post. I couldn’t post my own one.)

This is my current security group attributes.

ingress = {
description = “HTTPS”
from_port = 443
to_port = 443
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

ingress = {
description = “HTTP”
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

ingress = {
description = “SSH”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

egress = {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
ipv6_cidr_blocks = ["::/0"]
}

This is the error message I am receiving.

Error: Attribute redefined

on main.tf line 61, in resource “aws_security_group” “allow_web”:
61: ingress = {

The argument “ingress” was already set at main.tf:53,3-10. Each argument may
be set only once.

hi, here a example:

resource “aws_security_group” “name_security_group” {
vpc_id = aws_vpc.name_vpc.id
egress = [
{
cidr_blocks = [“0.0.0.0/0”, ]
description = “”
from_port = 0
ipv6_cidr_blocks =
prefix_list_ids =
protocol = “-1”
security_groups =
self = false
to_port = 0
}
]
ingress = [
{
cidr_blocks = var.name_variable
description = “”
from_port = 8080
ipv6_cidr_blocks =
prefix_list_ids =
protocol = “tcp”
security_groups =
self = false
to_port = 8080
},
{
cidr_blocks = var.name_variable
description = “”
from_port = 22
ipv6_cidr_blocks =
prefix_list_ids =
protocol = “tcp”
security_groups =
self = false
to_port = 22
},
]
tags = {
Name = “name_security_group”
}
}