From 1b6dcca86b3057a432d94315205324cb23f36c36 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 9 Apr 2021 14:48:31 +0200 Subject: [PATCH] ec2_instance - fetch status of instance before attempting to set additional parameters (#533) --- .../fragments/532-ec2_instance-wait-status.yml | 2 ++ plugins/modules/ec2_instance.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 changelogs/fragments/532-ec2_instance-wait-status.yml diff --git a/changelogs/fragments/532-ec2_instance-wait-status.yml b/changelogs/fragments/532-ec2_instance-wait-status.yml new file mode 100644 index 00000000000..aa0164d2d05 --- /dev/null +++ b/changelogs/fragments/532-ec2_instance-wait-status.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_instance - wait for new instances to return a status before attempting to set additional parameters (https://github.com/ansible-collections/community.aws/pull/533). diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 22db3c88f79..5138fd7647a 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1647,6 +1647,18 @@ def ensure_present(existing_matches, changed, ec2, state): instance_ids = [i['InstanceId'] for i in instances] for ins in instances: + # Wait for instances to exist (don't check state) + try: + AWSRetry.jittered_backoff( + catch_extra_error_codes=['InvalidInstanceID.NotFound'], + )( + ec2.describe_instance_status + )( + InstanceIds=[ins['InstanceId']], + IncludeAllInstances=True, + ) + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: + module.fail_json_aws(e, msg="Failed to fetch status of new EC2 instance") changes = diff_instance_and_params(ins, module.params, ec2, skip=['UserData', 'EbsOptimized']) for c in changes: try: