【问题标题】:InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda (Python Boto3)InvalidParameterValueException:为函数定义的角色不能由 Lambda 承担(Python Boto3)
【发布时间】:2021-08-30 23:47:28
【问题描述】:

我在 python 上使用 boto3 API,我遇到了这个问题。

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

【问题讨论】:

  • this 是否回答您的问题或提供任何帮助?
  • @Avijeet 这不是 IAM 中设置错误的问题,而是在角色创建和附加它以与 lambda 一起使用之间需要一些延迟时间。我使用下面的答案来解决它。

标签: python aws-lambda boto3


【解决方案1】:

我发现 AWS lambda 需要一些时间才能使用新创建的角色。

我的修复如下:

  1. 为重试命令创建一个重试装饰器

     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. 从装饰器包装函数

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

【讨论】:

    猜你喜欢
    • 2016-09-26
    • 2021-10-24
    • 2016-07-24
    • 2023-04-06
    • 1970-01-01
    • 2017-05-12
    • 2019-12-17
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多