Review an EC2 instance’s use of the default VPC

Check that default VPC use fits the workload’s network design.

Description

Follow aws_instance.subnet_id to the subnet and check its vpc_id.

Potential impact

Default subnet routes, public IP assignment, or shared security groups can allow more access than the workload needs.

Remediation

Review routes and security group rules. If the workload needs a separate network, create a subnet in a dedicated VPC and point the instance to it. Changing an existing instance’s subnet replaces the instance. Review the Terraform plan and preserve any data you need before applying it.

Examples

Supply an unused subnet CIDR within the selected VPC and an AMI valid in the target Region.

Before

hcl
data "aws_vpc" "default" {
  default = true
}

resource "aws_instance" "example" {
  ami           = "ami-003634241a8fcdec0"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.my_subnet.id
}

resource "aws_subnet" "my_subnet" {
  vpc_id     = data.aws_vpc.default.id
  cidr_block = var.available_subnet_cidr
}

After

hcl
resource "aws_instance" "example" {
  ami           = "ami-003634241a8fcdec0"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.my_subnet2.id
}

resource "aws_subnet" "my_subnet2" {
  vpc_id     = aws_vpc.main.id
  cidr_block = var.available_subnet_cidr
}

References