首页 > 解决方案 > Boto3 InvalidParameterException 在执行 lambda 函数时

问题描述

我在运行 lambda 函数时收到 Boto3 InvalidParameterException。我试图找到一种方法来处理这个异常。

我遇到了以下解决方案:

from boto.exception import BotoServerError

class InvalidParameterException(BotoServerError):
    pass

我正在使用 python3 并了解 boto 现在已弃用并被 boto3 取代。但我在 boto3 中找不到等效的解决方案。

谁能帮我解决这个问题?

标签: python-3.xamazon-web-servicesboto3botobotocore

解决方案


正如boto已弃用的那样modeled,客户端上的所有异常都可用。您也可以在 API 文档中查找相同的内容,基本上代码boto3是直接从 API 生成的。早期的方法boto是硬编码的东西并为此编写代码。

正如你在这里看到的

例如

import boto3
from botocore.exceptions import ClientError


def get_secret():
    secret_name = "MySecretName"
    region_name = "us-west-2"

    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name,
    )

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'ResourceNotFoundException':
            print("The requested secret " + secret_name + " was not found")
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            print("The request was invalid due to:", e)
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            print("The request had invalid params:", e)
        elif e.response['Error']['Code'] == 'DecryptionFailure':
            print("The requested secret can't be decrypted using the provided KMS key:", e)
        elif e.response['Error']['Code'] == 'InternalServiceError':
            print("An error occurred on service side:", e)

来自文档的 AWS Secrets Manager 示例

如何使用 boto3 处理错误


推荐阅读