Repository: getmoto/moto

Issue:
Security Groups: Default security group should be created for VPC
AWS creates a default security group when a VPC is created. Moto should reflect the same.

(Note: AWS also creates a default route table, which we do already model in moto. Just to make sure, I tested AWS to see whether the default security group is triggered by VPC creation or chained by the route table creation -- confirmed, the VPC creation triggers the default security group, and no new security group via route table creation.)

Minimal demonstration:

```
In [1]: import boto, boto.vpc

In [2]: conn = boto.vpc.connect_to_region('us-west-1')

In [3]: len(conn.get_all_security_groups())
Out[3]: 15

In [4]: vpc = conn.create_vpc("11.0.0.0/16");

In [5]: len(conn.get_all_security_groups())
Out[5]: 16

In [6]: route_table = conn.create_route_table(vpc.id)

In [7]: len(conn.get_all_security_groups())
Out[7]: 16
```



Discussion:

Resolving patch:
diff --git a/moto/ec2/models.py b/moto/ec2/models.py
--- a/moto/ec2/models.py
+++ b/moto/ec2/models.py
@@ -857,7 +857,7 @@ def get_security_group_from_name(self, name, vpc_id=None):
 
         if name == 'default':
             # If the request is for the default group and it does not exist, create it
-            default_group = ec2_backend.create_security_group("default", "The default security group", force=True)
+            default_group = ec2_backend.create_security_group("default", "The default security group", vpc_id=vpc_id, force=True)
             return default_group
 
     def authorize_security_group_ingress(self,
@@ -1116,9 +1116,13 @@ def create_vpc(self, cidr_block):
         vpc = VPC(vpc_id, cidr_block)
         self.vpcs[vpc_id] = vpc
 
-        # AWS creates a default main route table.
+        # AWS creates a default main route table and security group.
         main_route_table = self.create_route_table(vpc_id, main=True)
 
+        default = ec2_backend.get_security_group_from_name('default', vpc_id=vpc_id)
+        if not default:
+            ec2_backend.create_security_group('default', 'default VPC security group', vpc_id=vpc_id)
+
         return vpc
 
     def get_vpc(self, vpc_id):
@@ -1140,6 +1144,11 @@ def delete_vpc(self, vpc_id):
         for route_table in route_tables:
             ec2_backend.delete_route_table(route_table.id)
 
+        # Delete default security group if exists.
+        default = ec2_backend.get_security_group_from_name('default', vpc_id=vpc_id)
+        if default:
+            ec2_backend.delete_security_group(group_id=default.id)
+
         # Now delete VPC.
         vpc = self.vpcs.pop(vpc_id, None)
         if not vpc:


Test patch:
diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py
--- a/tests/test_ec2/test_vpcs.py
+++ b/tests/test_ec2/test_vpcs.py
@@ -31,6 +31,22 @@ def test_vpcs():
     cm.exception.request_id.should_not.be.none
 
 
+@mock_ec2
+def test_vpc_defaults():
+    conn = boto.connect_vpc('the_key', 'the_secret')
+    vpc = conn.create_vpc("10.0.0.0/16")
+
+    conn.get_all_vpcs().should.have.length_of(1)
+    conn.get_all_route_tables().should.have.length_of(1)
+    conn.get_all_security_groups().should.have.length_of(1)
+
+    vpc.delete()
+
+    conn.get_all_vpcs().should.have.length_of(0)
+    conn.get_all_route_tables().should.have.length_of(0)
+    conn.get_all_security_groups().should.have.length_of(0)
+
+
 @mock_ec2
 def test_vpc_tagging():
     conn = boto.connect_vpc()