For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/cloudformation-aws-ec2-instance-using-default-vpc.md.
A documentation index is available at /llms.txt.
EC2 instances placed in a default VPC are more likely to be publicly reachable and lack explicit network segmentation, increasing the risk of unintended exposure and lateral movement. This rule checks AWS::EC2::Instance resources and ensures Properties.SubnetId does not reference a subnet that is associated with a default VPC. Instances are flagged when their SubnetId references an AWS::EC2::Subnet whose Properties.VpcId resolves to a value or Ref name containing default. Use an explicit non-default VPC and private subnets. For example, reference a subnet in your own VPC:
Resources:DefaultVPC:Type:AWS::EC2::InstanceProperties:ImageId:"ami-79fd7eee"KeyName:"testkey"SubnetId:!Ref PublicSubnetA22PublicSubnetA22:Type:AWS::EC2::SubnetProperties:VpcId:!Ref VPCCidrBlock:10.1.10.0/24AvailabilityZone:!Select [ 0, !GetAZs ] # Obtenha o primeiro AZ na listaTags:- Key:NameValue:!Sub ${AWS::StackName}-Public-A
Resources:DefaultVPC:Type:AWS::EC2::InstanceProperties:ImageId:"ami-79fd7eee"KeyName:"testkey"SubnetId:!Ref PublicSubnetA2PublicSubnetA2:Type:AWS::EC2::SubnetProperties:VpcId:!Ref DefaultVPCCidrBlock:10.1.10.0/24AvailabilityZone:!Select [ 0, !GetAZs ] # Obtenha o primeiro AZ na listaTags:- Key:NameValue:!Sub ${AWS::StackName}-Public-A