0

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.
  • Does [this](https://stackoverflow.com/questions/36419442/the-role-defined-for-the-function-cannot-be-assumed-by-lambda) answer your question, or provide any help? – BoredRyuzaki Jun 15 '21 at 06:19
  • @Avijeet It is not the problem of wrong setting in the IAM, but it turn out to be the some delay time needed between role creation and attached it to be used with lambda. i solve it by using the below answer. – KangSheng Wong Jun 21 '21 at 01:18

1 Answers1

1

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