From 140b10b631d2ee08044ed56693f9b217cae26f85 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 3 Dec 2020 20:21:13 +0100 Subject: [PATCH 1/2] Add AWSRetry to ec2_key --- plugins/modules/ec2_key.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugins/modules/ec2_key.py b/plugins/modules/ec2_key.py index b02673036d6..815130f9590 100644 --- a/plugins/modules/ec2_key.py +++ b/plugins/modules/ec2_key.py @@ -135,6 +135,7 @@ from ..module_utils.core import AnsibleAWSModule from ..module_utils.core import is_boto3_error_code +from ..module_utils.ec2 import AWSRetry def extract_key_data(key): @@ -170,7 +171,7 @@ def get_key_fingerprint(module, ec2_client, key_material): def find_key_pair(module, ec2_client, name): try: - key = ec2_client.describe_key_pairs(KeyNames=[name])['KeyPairs'][0] + key = ec2_client.describe_key_pairs(aws_retry=True, KeyNames=[name])['KeyPairs'][0] except is_boto3_error_code('InvalidKeyPair.NotFound'): return None except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as err: @@ -205,7 +206,7 @@ def create_key_pair(module, ec2_client, name, key_material, force): key = import_key_pair(module, ec2_client, name, key_material) else: try: - key = ec2_client.create_key_pair(KeyName=name) + key = ec2_client.create_key_pair(aws_retry=True, KeyName=name) except botocore.exceptions.ClientError as err: module.fail_json_aws(err, msg="error creating key") key_data = extract_key_data(key) @@ -215,7 +216,7 @@ def create_key_pair(module, ec2_client, name, key_material, force): def import_key_pair(module, ec2_client, name, key_material): try: - key = ec2_client.import_key_pair(KeyName=name, PublicKeyMaterial=to_bytes(key_material)) + key = ec2_client.import_key_pair(aws_retry=True, KeyName=name, PublicKeyMaterial=to_bytes(key_material)) except botocore.exceptions.ClientError as err: module.fail_json_aws(err, msg="error importing key") return key @@ -227,7 +228,7 @@ def delete_key_pair(module, ec2_client, name, finish_task=True): if key: if not module.check_mode: try: - ec2_client.delete_key_pair(KeyName=name) + ec2_client.delete_key_pair(aws_retry=True, KeyName=name) except botocore.exceptions.ClientError as err: module.fail_json_aws(err, msg="error deleting key") if not finish_task: @@ -249,7 +250,7 @@ def main(): module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - ec2_client = module.client('ec2') + ec2_client = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff()) name = module.params['name'] state = module.params.get('state') From 37c1ddbcd7468136a259a77b86a5cc7f5763f354 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 3 Dec 2020 20:27:22 +0100 Subject: [PATCH 2/2] changelog --- changelogs/fragments/213-ec2_key-retries.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/213-ec2_key-retries.yml diff --git a/changelogs/fragments/213-ec2_key-retries.yml b/changelogs/fragments/213-ec2_key-retries.yml new file mode 100644 index 00000000000..934a81c0a2c --- /dev/null +++ b/changelogs/fragments/213-ec2_key-retries.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_key - add AWSRetry decorator to automatically retry on common temporary failures (https://github.com/ansible-collections/amazon.aws/pull/213).