For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-aws-security-group-with-unrestricted-access-to-ssh.md.
A documentation index is available at /llms.txt.
This check verifies that AWS security groups do not allow unrestricted inbound access to port 22 (SSH) from the public internet (cidr_blocks = ["0.0.0.0/0"]). Allowing public SSH access exposes instances to unauthorized access attempts and automated attacks, increasing the risk of successful brute-force compromises. To mitigate this vulnerability, the cidr_blocks attribute in the ingress block should be restricted to trusted IP ranges only, as shown below:
If left unaddressed, this misconfiguration can lead to remote attackers gaining entry to instances via SSH, putting sensitive data and critical infrastructure at risk.
Compliant Code Examples
resource"aws_security_group""negative1"{name="allow_tls"description="Allow TLS inbound traffic"vpc_id=aws_vpc.main.idingress{description="TLS from VPC"from_port=22to_port=22protocol="tcp"cidr_blocks=["192.120.0.0/16","75.132.0.0/16"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="allow_tls"}}
module"vote_service_sg"{source="terraform-aws-modules/security-group/aws"version="4.3.0"name="user-service"description="Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"vpc_id="vpc-12345678"ingress{description="TLS from VPC"from_port=22to_port=22protocol="tcp"cidr_blocks=["192.120.0.0/16","75.132.0.0/16"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="allow_tls"}}
Non-Compliant Code Examples
resource"aws_security_group""positive1"{name="allow_tls"description="Allow TLS inbound traffic"vpc_id=aws_vpc.main.idingress{description="TLS from VPC"from_port=22to_port=22protocol="tcp"cidr_blocks=["0.0.0.0/0"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="allow_tls"}}
resource"aws_security_group""positive2"{name="allow_tls"description="Allow TLS inbound traffic"vpc_id=aws_vpc.main.idingress{description="TLS from VPC"from_port=22to_port=22protocol="tcp"cidr_blocks=["192.120.0.0/16","0.0.0.0/0"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="allow_tls"}}
module"vote_service_sg"{source="terraform-aws-modules/security-group/aws"version="4.3.0"name="user-service"description="Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"vpc_id="vpc-12345678"ingress{description="TLS from VPC"from_port=22to_port=22protocol="tcp"cidr_blocks=["0.0.0.0/0"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="allow_tls"}}
1
2
rulesets:- Terraform / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.