feat(ec2): Add Instance Monitoring attribute (#6678)

This commit is contained in:
Pepe Fagoaga 2023-08-16 23:55:24 +02:00 committed by GitHub
parent 45cffb6cd8
commit 0b4889ad37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -120,6 +120,7 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel):
in_ec2_classic = not bool(self.subnet_id)
self.key_name = kwargs.get("key_name")
self.ebs_optimized = kwargs.get("ebs_optimized", False)
self.monitoring_state = kwargs.get("monitoring_state", "disabled")
self.source_dest_check = "true"
self.launch_time = utc_date_and_time()
self.ami_launch_index = kwargs.get("ami_launch_index", 0)

View File

@ -84,6 +84,9 @@ class InstanceResponse(EC2BaseResponse):
or None,
"iam_instance_profile_arn": self._get_param("IamInstanceProfile.Arn")
or None,
"monitoring_state": "enabled"
if self._get_param("Monitoring.Enabled") == "true"
else "disabled",
}
if len(kwargs["nics"]) and kwargs["subnet_id"]:
raise InvalidParameterCombination(
@ -472,7 +475,7 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
<tenancy>default</tenancy>
</placement>
<monitoring>
<state>enabled</state>
<state> {{ instance.monitoring_state }} </state>
</monitoring>
{% if instance.subnet_id %}
<subnetId>{{ instance.subnet_id }}</subnetId>
@ -632,7 +635,7 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns="http://ec2.amazona
<platform>{{ instance.platform }}</platform>
{% endif %}
<monitoring>
<state>disabled</state>
<state>{{ instance.monitoring_state }}</state>
</monitoring>
{% if instance.subnet_id %}
<subnetId>{{ instance.subnet_id }}</subnetId>

View File

@ -2664,3 +2664,35 @@ def test_run_multiple_instances_with_single_nic_template():
instance_1_ip = enis[1]["PrivateIpAddresses"][0]["PrivateIpAddress"]
assert instance_0_ip != instance_1_ip
@mock_ec2
def test_describe_instance_without_enhanced_monitoring():
conn = boto3.client("ec2", region_name="us-west-1")
instance = conn.run_instances(
ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, Monitoring={"Enabled": True}
)
assert instance["Instances"][0]["Monitoring"] == {"State": "enabled"}
result = conn.describe_instances(
InstanceIds=[instance["Instances"][0]["InstanceId"]]
)["Reservations"][0]["Instances"]
assert result[0]["Monitoring"] == {"State": "enabled"}
@mock_ec2
def test_describe_instance_with_enhanced_monitoring():
conn = boto3.client("ec2", region_name="us-west-1")
instance = conn.run_instances(
ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1, Monitoring={"Enabled": False}
)
assert instance["Instances"][0]["Monitoring"] == {"State": "disabled"}
result = conn.describe_instances(
InstanceIds=[instance["Instances"][0]["InstanceId"]]
)["Reservations"][0]["Instances"]
assert result[0]["Monitoring"] == {"State": "disabled"}