Review default security group use on EC2 instances

Use security groups tailored to the purpose of each EC2 instance.

Description

Use a security group for each EC2 workload role. Changes to a shared default group affect every instance attached to it.

Potential impact

Any attached group can allow a connection, so a broad default-group rule can expose a port that the dedicated group restricts.

Remediation

Configure the dedicated group’s required inbound and outbound rules. Set vpc_security_group_ids to its ID, remove the unnecessary default-group association and test connectivity from the expected peers.

Examples

The examples change the instance’s group ID. Define the two referenced groups and their rules separately.

Before

hcl
resource "aws_instance" "example" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }

  vpc_security_group_ids = [aws_default_security_group.default.id]
}

After

hcl
resource "aws_instance" "example" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }

  vpc_security_group_ids = [aws_security_group.sg.id]
}

References