Repository: getmoto/moto

Issue:
ec2 created without keypair has KeyName = 'None'
When creating an instance without a keypair, moto describes the instance as having a keypair called`None`:

```python
import boto3
from moto import mock_ec2

mock = mock_ec2()
mock.start()

ec2_client = boto3.client("ec2", region_name="us-east-1")
ec2_client.run_instances(
    MaxCount=1,
    MinCount=1,
)

response = ec2_client.describe_instances()

# the following is true
assert response['Reservations'][0]['Instances'][0]['KeyName'] == 'None'
```

However the EC2 describe instances API will return a response without the KeyName field for EC2 instances without a keypair.
 
 moto 3.1.16


Discussion:

Resolving patch:
diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py
--- a/moto/ec2/responses/instances.py
+++ b/moto/ec2/responses/instances.py
@@ -406,7 +406,9 @@ def _convert_to_bool(bool_str):
           <publicDnsName>{{ instance.public_dns }}</publicDnsName>
           <dnsName>{{ instance.public_dns }}</dnsName>
           <reason/>
-          <keyName>{{ instance.key_name }}</keyName>
+          {% if instance.key_name is not none %}
+             <keyName>{{ instance.key_name }}</keyName>
+          {% endif %}
           <ebsOptimized>{{ instance.ebs_optimized }}</ebsOptimized>
           <amiLaunchIndex>{{ instance.ami_launch_index }}</amiLaunchIndex>
           <instanceType>{{ instance.instance_type }}</instanceType>
@@ -559,7 +561,9 @@ def _convert_to_bool(bool_str):
                     <publicDnsName>{{ instance.public_dns }}</publicDnsName>
                     <dnsName>{{ instance.public_dns }}</dnsName>
                     <reason>{{ instance._reason }}</reason>
-                    <keyName>{{ instance.key_name }}</keyName>
+                    {% if instance.key_name is not none %}
+                        <keyName>{{ instance.key_name }}</keyName>
+                    {% endif %}
                     <ebsOptimized>{{ instance.ebs_optimized }}</ebsOptimized>
                     <amiLaunchIndex>{{ instance.ami_launch_index }}</amiLaunchIndex>
                     <productCodes/>


Test patch:
diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py
--- a/tests/test_ec2/test_instances.py
+++ b/tests/test_ec2/test_instances.py
@@ -1990,6 +1990,22 @@ def test_modify_delete_on_termination():
     instance.block_device_mappings[0]["Ebs"]["DeleteOnTermination"].should.be(False)
 
 
+@mock_ec2
+def test_create_instance_with_default_options():
+    client = boto3.client("ec2", region_name="eu-west-1")
+
+    def assert_instance(instance):
+        # TODO: Add additional asserts for default instance response
+        assert instance["ImageId"] == EXAMPLE_AMI_ID
+        assert "KeyName" not in instance
+
+    resp = client.run_instances(ImageId=EXAMPLE_AMI_ID, MaxCount=1, MinCount=1)
+    assert_instance(resp["Instances"][0])
+
+    resp = client.describe_instances(InstanceIds=[resp["Instances"][0]["InstanceId"]])
+    assert_instance(resp["Reservations"][0]["Instances"][0])
+
+
 @mock_ec2
 def test_create_instance_ebs_optimized():
     ec2_resource = boto3.resource("ec2", region_name="eu-west-1")