An AWS security group is a virtual firewall for EC2 instances to control inbound and outbound traffic. An ingress rule allowing traffic from all IP addresses to standard network ports on which administration services traditionally listen, such as 22 for SSH, can expose these services to exploits and unauthorized access.
It’s recommended to restrict access to remote administration services to only trusted IP addresses. In practice, trusted IP addresses are those held by system administrators or those of bastion-like servers.
An ingress rule allowing all inbound SSH traffic:
resource "aws_security_group" "noncompliant" {
name = "allow_ssh_noncompliant"
description = "allow_ssh_noncompliant"
vpc_id = aws_vpc.main.id
ingress {
description = "SSH rule"
from_port = 22
to_port = 22 # SSH traffic
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # from all IP addresses is authorized
}
}
An ingress rule allowing inbound SSH traffic from specific IP addresses:
resource "aws_security_group" "compliant" {
name = "allow_ssh_compliant"
description = "allow_ssh_compliant"
vpc_id = aws_vpc.main.id
ingress {
description = "SSH rule"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["1.2.3.0/24"] # Compliant
}
}