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.
# 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.idcidr_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.idcidr_block="10.0.1.0/24"tags={Name="Main"}}