diff --git a/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml b/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml new file mode 100644 index 00000000000..293f89179b2 --- /dev/null +++ b/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - ec2_instance - ec2_instance module broken in Python 3.8 - dict keys modified during iteration (https://github.com/ansible-collections/amazon.aws/issues/709). diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index e7757ed825c..2bfde6f91f2 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1463,6 +1463,8 @@ def change_network_attachments(instance, params): def find_instances(ids=None, filters=None): paginator = client.get_paginator('describe_instances') + sanitized_filters = dict() + if ids: params = dict(InstanceIds=ids) elif filters is None: @@ -1470,13 +1472,16 @@ def find_instances(ids=None, filters=None): else: for key in list(filters.keys()): if not key.startswith("tag:"): - filters[key.replace("_", "-")] = filters.pop(key) - params = dict(Filters=ansible_dict_to_boto3_filter_list(filters)) + sanitized_filters[key.replace("_", "-")] = filters[key] + else: + sanitized_filters[key] = filters[key] + params = dict(Filters=ansible_dict_to_boto3_filter_list(sanitized_filters)) try: results = _describe_instances(**params) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json_aws(e, msg="Could not describe instances") + retval = list(results) return retval