我代入了 AWS Identity and Access Management (IAM) 角色,我的 API 调用返回了以下错误: “An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials.”
简短描述
默认情况下,AWS 不激活新的 AWS 区域,新区域仅接受版本 2 令牌。当版本 1 令牌向 AWS 未激活的区域中的服务端点发出请求时,会出现以下错误:
“An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials.”
最佳做法是使用 AWS Security Token Service (AWS STS) 区域端点,因为来自 STS 区域端点的会话令牌是版本 2 令牌,在所有区域都有效。或者,您可以从全球端点更改会话令牌的区域兼容性设置。
解决方法
从区域端点获取令牌
**重要事项:**对于 AWS 默认激活的区域,无论哪个账户发出请求,您都必须在生成临时凭证的 AWS 账户中激活 STS 区域端点。对于手动激活的区域,请在发出请求的账户和生成凭证的账户中同时激活该区域。如果您没有在两个账户中激活该区域,则会收到“InvalidClientTokenId”错误。
**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI。
您可以使用 AWS SDK 或 AWS CLI 来获取令牌。
例如,您可以运行以下适用于 Python 的 AWS SDK (Boto3) 命令:
# Replace existing code to create STS client with the following:
sts_client = boto3.client('sts', region_name='your-region', endpoint_url='https://sts.your-region.amazonaws.com')
**注意:**将 your-region 替换为您的区域,将 endpoint_url 替换为您的端点。
或者,运行以下 assume-role 命令:
aws sts assume-role --role-arn arn:aws:iam::444455556666:role/RoleName --role-session-name RoleName --region your-region --endpoint-url https://sts.your-region.amazonaws.com
**注意:**将 your-region 替换为您的区域,将 role-arn 替换为您的 IAM Amazon 资源名称 (ARN),将 endpoint_url 替换为您的端点。
从全球端点更改会话令牌的区域兼容性设置
默认情况下,您对全局端点进行的 STS 调用会颁发版本 1 令牌。要在所有区域使用会话令牌,请配置 STS 全球端点以颁发版本 2 令牌。
**重要事项:**版本 2 令牌比版本 1 包含更多的字符。令牌大小的增加可能会影响临时存储会话令牌的现有系统。确保存储令牌的系统能够管理增加的令牌大小。