Skip to content

Commit

Permalink
ec2_instance_info - Add AWS Retries (ansible-collections#521)
Browse files Browse the repository at this point in the history
* Add retries to ec2_instance_info

* changelog
  • Loading branch information
tremble authored and danielcotton committed Nov 23, 2021
1 parent 1ee6eb3 commit 62a5216
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/521-ec2_instance_info-retries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance_info - add retries on common AWS failures (https://github.com/ansible-collections/community.aws/pull/521).
13 changes: 9 additions & 4 deletions plugins/modules/ec2_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,27 +511,32 @@

try:
import botocore
from botocore.exceptions import ClientError
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict


@AWSRetry.jittered_backoff()
def _describe_instances(connection, **params):
paginator = connection.get_paginator('describe_instances')
return paginator.paginate(**params).build_full_result()


def list_ec2_instances(connection, module):

instance_ids = module.params.get("instance_ids")
uptime = module.params.get('minimum_uptime')
filters = ansible_dict_to_boto3_filter_list(module.params.get("filters"))

try:
reservations_paginator = connection.get_paginator('describe_instances')
reservations = reservations_paginator.paginate(InstanceIds=instance_ids, Filters=filters).build_full_result()
except ClientError as e:
reservations = _describe_instances(connection, InstanceIds=instance_ids, Filters=filters)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to list ec2 instances")

instances = []
Expand Down

0 comments on commit 62a5216

Please sign in to comment.