Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWSRetry to ec2_key #213

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/213-ec2_key-retries.yml
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 6 additions & 5 deletions plugins/modules/ec2_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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')
Expand Down