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

This commit is contained in:
Julien Vey 2018-04-04 16:21:44 +02:00 committed by Ryan S. Brown
parent 1905a6e8fb
commit a6d5656dd8
2 changed files with 16 additions and 4 deletions

View file

@ -622,6 +622,7 @@ import re
import uuid import uuid
import string import string
import textwrap import textwrap
import time
from collections import namedtuple from collections import namedtuple
try: try:
@ -1378,7 +1379,7 @@ def ensure_present(existing_matches, changed, ec2, state):
) )
try: try:
instance_spec = build_run_instance_spec(module.params) 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'] instances = instance_response['Instances']
instance_ids = [i['InstanceId'] for i in 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") 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(): def main():
global module global module
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()

View file

@ -30,9 +30,6 @@
<<: *aws_connection_info <<: *aws_connection_info
register: iam_role_2 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 - name: Make instance with an instance_role
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-test-default-vpc" name: "{{ resource_prefix }}-test-default-vpc"