diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 91f16e4c9..162338196 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -46,6 +46,7 @@ class Instance(BotoInstance): self.security_groups = security_groups self.instance_type = kwargs.get("instance_type", "m1.small") self.subnet_id = kwargs.get("subnet_id") + self.key_name = kwargs.get("key_name") @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json): @@ -60,7 +61,8 @@ class Instance(BotoInstance): count=1, security_group_names=group_names, instance_type=properties.get("InstanceType", "m1.small"), - subnet_id=properties.get("SubnetId") + subnet_id=properties.get("SubnetId"), + key_name=properties.get("KeyName"), ) return reservation.instances[0] diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index a153713c7..d3c2136fd 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -35,9 +35,11 @@ class InstanceResponse(BaseResponse): security_group_names = self._get_multi_param('SecurityGroup') instance_type = self.querystring.get("InstanceType", ["m1.small"])[0] subnet_id = self.querystring.get("SubnetId", [None])[0] + key_name = self.querystring.get("KeyName", [None])[0] new_reservation = ec2_backend.add_instances( image_id, min_count, user_data, security_group_names, - instance_type=instance_type, subnet_id=subnet_id) + instance_type=instance_type, subnet_id=subnet_id, + key_name=key_name) template = Template(EC2_RUN_INSTANCES) return template.render(reservation=new_reservation) @@ -114,6 +116,7 @@ EC2_RUN_INSTANCES = """