diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 30785bf1c..4bded43bb 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -47,6 +47,7 @@ class InstanceResponse(EC2BaseResponse): account_id=self.current_account, reservations=reservations_resp, next_token=next_token, + run_instances=False, ) .replace("True", "true") .replace("False", "false") @@ -128,7 +129,9 @@ class InstanceResponse(EC2BaseResponse): template = self.response_template(EC2_RUN_INSTANCES) return template.render( - account_id=self.current_account, reservation=new_reservation + account_id=self.current_account, + reservation=new_reservation, + run_instances=True, ) def terminate_instances(self) -> str: @@ -443,25 +446,20 @@ BLOCK_DEVICE_MAPPING_TEMPLATE = { }, } -EC2_RUN_INSTANCES = """ - 59dbff89-35bd-4eac-99ed-be587EXAMPLE - {{ reservation.id }} - {{ account_id }} - - - sg-245f6a01 - default - - - - {% for instance in reservation.instances %} - +INSTANCE_TEMPLATE = """ {{ instance.id }} {{ instance.image_id }} + {% if run_instances %} 0 pending + {% else %} + + {{ instance._state.code }} + {{ instance._state.name }} + + {% endif %} {{ instance.private_dns }} {{ instance.public_dns }} {{ instance.public_dns }} @@ -509,8 +507,12 @@ EC2_RUN_INSTANCES = """ {% endif %} + {% if instance.get_tags() %} {% for tag in instance.get_tags() %} + {{ tag.resource_id }} + {{ tag.resource_type }} {{ tag.key }} {{ tag.value }} {% endfor %} + {% endif %} {% for nic in instance.nics.values() %} @@ -553,8 +578,12 @@ EC2_RUN_INSTANCES = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + {{ reservation.id }} + {{ account_id }} + + + sg-245f6a01 + default + + + + {% for instance in reservation.instances %} + """ + + INSTANCE_TEMPLATE + + """ {% endfor %} """ +) -EC2_DESCRIBE_INSTANCES = """ +EC2_DESCRIBE_INSTANCES = ( + """ fdcdcab1-ae5c-489e-9c33-4637c5dda355 {% for reservation in reservations %} @@ -612,165 +660,9 @@ EC2_DESCRIBE_INSTANCES = """ @@ -780,6 +672,7 @@ EC2_DESCRIBE_INSTANCES = """ diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index d57fcfef5..e9e4fe469 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -1192,6 +1192,7 @@ def test_run_instance_with_security_group_id(): @mock_ec2 @pytest.mark.parametrize("hibernate", [True, False]) def test_run_instance_with_additional_args(hibernate): + client = boto3.client("ec2", region_name="us-east-1") ec2 = boto3.resource("ec2", region_name="us-east-1") instance = ec2.create_instances( ImageId=EXAMPLE_AMI_ID, @@ -1206,6 +1207,10 @@ def test_run_instance_with_additional_args(hibernate): assert instance.placement["AvailabilityZone"] == "us-east-1b" assert instance.hibernation_options == {"Configured": hibernate} + reservations = client.describe_instances(InstanceIds=[instance.id])["Reservations"] + instance = reservations[0]["Instances"][0] + assert instance["HibernationOptions"] == {"Configured": hibernate} + @mock_ec2 def test_run_instance_with_default_placement():