KangSheng Wong
KangSheng Wong

Reputation: 51

InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda (Python Boto3)

I am using boto3 API on python and I have encounter this problem.

An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: The role defined for the function cannot be assumed by Lambda.

Upvotes: 0

Views: 1162

Answers (1)

KangSheng Wong
KangSheng Wong

Reputation: 51

I found out that it need some time for AWS lambda to use the newly created roles.

My fix is like below:

  1. create a retry decorator for retrying the commands

     def retry(ExceptionToCheck=Exception, tries=4, delay=3, backoff=2):
         def deco_retry(func):
    
             @wraps(func)
             def wrapper(*args, **kwargs):
                 cnt, mdelay = tries, delay
                 while cnt > 1:
                     try:
                         return func(*args, **kwargs)
                     except ExceptionToCheck as e:
                         print(f'{str(e)}, Retrying in {mdelay} seconds...')
                         time.sleep(mdelay)
                         cnt -= 1
                         mdelay *= backoff
                 return func(*args, **kwargs)
    
             return wrapper
    
         return deco_retry
    
  2. wraps the function from decorator

     @retry()
     def create(input):
         response = lambda_client.create_function(**input)
         return response
    

Upvotes: 1

Related Questions