Skip to content

Commit

Permalink
ec2_instance - expand use of AWSRetry (#928)
Browse files Browse the repository at this point in the history
ec2_instance - expand use of AWSRetry

SUMMARY
fixes: #927
A few code paths weren't using AWSRetry when the should have, probably a leftover from when retries included the NotFound exceptions.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_instance
ADDITIONAL INFORMATION

Reviewed-by: Brian Scholer <None>
Reviewed-by: Alina Buzachis <None>
(cherry picked from commit 524129f)
  • Loading branch information
tremble authored and patchback[bot] committed Jul 15, 2022
1 parent cc9d8dc commit f16fa65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/927-ec2_instance-retries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance - expanded the use of the automatic retries on temporary failures (https://github.com/ansible-collections/amazon.aws/issues/927).
10 changes: 6 additions & 4 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,9 +1800,11 @@ def enforce_count(existing_matches, module, desired_module_state):
module.exit_json(changed=True, msg='Would have terminated following instances if not in check mode {0}'.format(terminate_ids))
# terminate instances
try:
result = client.terminate_instances(InstanceIds=terminate_ids)
result = client.terminate_instances(aws_retry=True, InstanceIds=terminate_ids)
await_instances(terminate_ids, desired_module_state='terminated', force_wait=True)
except botocore.exceptions.ClientError as e:
except is_boto3_error_code('InvalidInstanceID.NotFound'):
pass
except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except
module.fail_json(e, msg='Unable to terminate instances')
module.exit_json(
changed=True,
Expand Down Expand Up @@ -1878,12 +1880,12 @@ def ensure_present(existing_matches, desired_module_state):

def run_instances(**instance_spec):
try:
return client.run_instances(**instance_spec)
return client.run_instances(aws_retry=True, **instance_spec)
except is_boto3_error_message('Invalid IAM Instance Profile ARN'):
# If the instance profile has just been created, it takes some time to be visible by ec2
# So we wait 10 second and retry the run_instances
time.sleep(10)
return client.run_instances(**instance_spec)
return client.run_instances(aws_retry=True, **instance_spec)


def build_filters():
Expand Down

0 comments on commit f16fa65

Please sign in to comment.