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-ec2-instance-using-default-vpc.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

Id: terraform-aws-ec2-instance-using-default-vpc

Provider: AWS

Platform: Terraform

Severity: Low

Category: Networking and Firewall

Learn More

Description

This check ensures that Amazon EC2 instances are not deployed within the default VPC (aws_vpc.default) in AWS environments. Default VPCs are automatically created by AWS and often have broader, pre-configured network permissions and less restrictive security controls, increasing the attack surface and risk of unauthorized access. By explicitly defining and using custom VPCs (for example, aws_vpc.main), organizations can enforce tailored network segmentation and security group rules, reducing the likelihood of exploitation due to overly permissive defaults.

Compliant Code Examples

resource "aws_instance" "negative1" {
  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 = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}
resource "aws_instance" "negative3" {
  ami = "ami-003634241a8fcdec0"

  instance_type = "t2.micro"

  subnet_id = aws_subnet.my_subnet5.id

}

resource "aws_vpc" "default2" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "my_subnet5" {
  vpc_id     = aws_vpc.default2.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}
# Regression fixture for the cross-stack join bug: this instance's own
# subnet references a real, non-default aws_vpc named "main" (see
# other_stack/negative6.tf, an unrelated stack in its own subdirectory
# whose aws_default_vpc happens to reuse the exact same label "main").
# Without scoping the join to this module (see module_scope in
# query.rego), the unrelated aws_default_vpc.main in
# other_stack/negative6.tf would incorrectly satisfy this lookup and
# flag this instance.
resource "aws_instance" "negative5" {
  ami = "ami-003634241a8fcdec0"

  instance_type = "t2.micro"

  subnet_id = aws_subnet.my_subnet6.id
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "my_subnet6" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}

Non-Compliant Code Examples

# Note for false_negative_unresolved_subnet_reference1.json, the plan-JSON
# sibling exercising this same configuration: on a real Terraform create
# plan, subnet_id is set via a reference to a not-yet-created subnet, so
# it is unresolved at plan time and simply absent from planned_values
# (Terraform records it under resource_changes[].change.after_unknown
# instead, which this repo's plan-JSON parser does not surface into Rego).
# The resolved-ID plan-JSON rule body has nothing to correlate
# instance.subnet_id against in that fixture, so it stays silent -- a
# false negative.
resource "aws_instance" "positive1" {
  ami = "ami-003634241a8fcdec0"

  instance_type = "t2.micro"

  subnet_id   = aws_subnet.my_subnet.id

}

resource "aws_default_vpc" "default" {
}

resource "aws_subnet" "my_subnet" {
  vpc_id     = aws_default_vpc.default.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}
resource "aws_instance" "positive2" {
  ami = "ami-003634241a8fcdec0"

  instance_type = "t2.micro"

  subnet_id = aws_subnet.my_subnet3.id

}

resource "aws_default_vpc" "default" {
}

resource "aws_subnet" "my_subnet3" {
  vpc_id     = aws_default_vpc.default.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}