ec2_instance: add a retry to run_instance to help with ec2 consistency

pull/35159/head
Julien Vey 6 years ago committed by Ryan S. Brown
parent 1905a6e8fb
commit a6d5656dd8

@ -622,6 +622,7 @@ import re
import uuid
import string
import textwrap
import time
from collections import namedtuple
try:
@ -1378,7 +1379,7 @@ def ensure_present(existing_matches, changed, ec2, state):
)
try:
instance_spec = build_run_instance_spec(module.params)
instance_response = AWSRetry.jittered_backoff()(ec2.run_instances)(**instance_spec)
instance_response = run_instances(ec2, **instance_spec)
instances = instance_response['Instances']
instance_ids = [i['InstanceId'] for i in instances]
@ -1405,6 +1406,20 @@ def ensure_present(existing_matches, changed, ec2, state):
module.fail_json_aws(e, msg="Failed to create new EC2 instance")
@AWSRetry.jittered_backoff()
def run_instances(ec2, **instance_spec):
try:
return ec2.run_instances(**instance_spec)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidParameterValue' and "Invalid IAM Instance Profile ARN" in e.response['Error']['Message']:
# 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 ec2.run_instances(**instance_spec)
else:
raise e
def main():
global module
argument_spec = ec2_argument_spec()

@ -30,9 +30,6 @@
<<: *aws_connection_info
register: iam_role_2
- name: Wait for IAM role to be available, otherwise the next step will fail (Invalid IAM Instance Profile name)
command: sleep 10
- name: Make instance with an instance_role
ec2_instance:
name: "{{ resource_prefix }}-test-default-vpc"

Loading…
Cancel
Save