---
title: EC2 instance using default VPC
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Infrastructure as Code (IaC)
  Security > IaC Security Rules > EC2 instance using default VPC
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# EC2 instance using default VPC

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

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

**Provider:** AWS

**Platform:** Terraform

**Severity:** Low

**Category:** Networking and Firewall

#### Learn More{% #learn-more %}

- [Provider Reference](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#subnet_id)

### Description{% #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{% #compliant-code-examples %}

```terraform
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"
  }
}
```

```terraform
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"
  }
}
```

```terraform
# 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{% #non-compliant-code-examples %}

```terraform
# 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"
  }
}
```

```terraform
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"
  }
}
```
