【问题标题】:An error occurred (UnrecognizedClientException) when calling the GetSecretValue operation: The security token included in the request is invalid调用 GetSecretValue 操作时发生错误(UnrecognizedClientException):请求中包含的安全令牌无效
【发布时间】:2020-12-09 10:58:00
【问题描述】:

请检查以下有问题的屏幕截图。当我们在 boto3 中单独使用但在 SAM lambda 函数中触发它时,AWS 凭据配置正确并且工作正常。enter image description here

尝试了所有解决方案,例如检查“aws configure”和取消设置AWS_SECURITY_TOKEN 以及其他来源中提到的其他解决方案也尝试过但没有奏效。

并且在代码中我正在尝试做

session = boto3.session.Session()
secretsmanager = session.client('secretsmanager')

try:
        get_secret_value_response = secretsmanager.get_secret_value(
            SecretId=secret_name
        )
        secret = json.loads(get_secret_value_response['SecretString'])
    except ClientError as e:
        print(e)
        # print(sys.exc_info(),traceback.print_exc(file=sys.stdout))
    except Exception as e:
        print(sys.exc_info(),traceback.print_exc(file=sys.stdout))
        print(e)

【问题讨论】:

  • 再次检查用于您的程序的 IAM 凭证。你确定它们仍然有效吗?

标签: python boto3 aws-sam


【解决方案1】:

也许问题有点不同,但我在本地遇到了完全相同的错误,因为我在获取会话之前设置了默认配置文件。

所以,如果我运行脚本:

boto3.setup_default_session(profile_name='myprofile')
session = boto3.session.Session()
secretsmanager = session.client('secretsmanager')

我得到了与问题相同的错误,可能是因为它们一起使用时效果不佳。

要解决,您可以删除会话部分:

boto3.setup_default_session(profile_name='myprofile')
secretsmanager = boto3.client('secretsmanager')

【讨论】:

    【解决方案2】:

    通过删除此文件 ~/.aws/credentials 来移除 AWS 凭证。然后重新运行aws configure 并传递有效的安全凭证。这应该可以解决您遇到的问题。

    如果您配置了多个配置文件,则编辑 ~/.aws/credentials 并删除与此代码一起使用的配置文件。例如,如果您在配置凭据时使用了 user1,那么您的文件将包含类似于以下内容:

    [default]
    aws_access_key_id=AKIAIOSFODNN7EXAMPLE
    aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
    
    [user1]
    aws_access_key_id=AKIAI44QH8DHBEXAMPLE
    aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
    

    要解决此问题,只需从 ~/.aws/credentials 中删除 [user1] 部分,然后重新运行 aws configure

    【讨论】:

    • 嗨。如果文件中有多个配置文件怎么办?删除文件将删除导致更多问题的所有内容。
    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 2019-11-12
    • 2016-12-12
    • 2020-05-05
    • 2021-12-12
    • 2014-01-30
    • 2021-08-26
    • 2015-02-20
    相关资源
    最近更新 更多