The SSO session associated with this profile has expired or is otherwise invalid
TL;DR — 你的 IAM Identity Center(AWS SSO)访问 token 失效了,因此该 profile 无法为 DynamoDB 调用铸造凭证。运行 aws sso login --profile <profile> 重新认证。如果登录后错误仍然存在,那是你登录的 profile/会话与代码使用的不同,或者 ~/.aws/sso/cache 下缓存的 token 陈旧了——清除它并重新登录。
含义
UnauthorizedSSOTokenError: The SSO session associated with this profile has
expired or is otherwise invalid. To refresh this SSO session run aws sso login
with the corresponding profile.基于 SSO 的 profile 不存储长期密钥。aws sso login 在本地缓存一个访问 token,而 CLI/SDK 按需把它换成短期角色凭证。两层都会过期:角色凭证在数分钟到数小时后,而 SSO token 在你组织配置的会话长度后。当 token 过期(或刷新失败)时,每个签名调用——包括 DynamoDB 读写——都会在到达 AWS 之前以这个错误失败。
为什么会发生
- SSO 会话干脆超时了——会话时长由你的 Identity Center 管理员设置;隔夜过期很典型。
- 你登录了一个不同的 profile——
aws sso login针对一个 profile 运行,而你的应用/工具解析的是另一个(AWS_PROFILE不匹配,或两个 profile 指向不同的sso_session配置)。 - 陈旧或损坏的 token 缓存——
~/.aws/sso/cache下的 JSON 文件不再与 profile 的 SSO 配置匹配(区域/起始 URL 改变了),因此即使登录后工具也一直看到一个"无效"的会话。 - 第三方工具自行解析凭证——直接读取 SSO 缓存的 SDK 和工具可能在有效性上与 CLI 意见不一(有些把 token 当作略早于其实际到期时间就过期了)。
如何修复
重新认证正确的 profile:
aws sso login --profile my-profile aws sts get-caller-identity --profile my-profile # confirm it worked确保你的代码使用那同一个 profile——为与 DynamoDB 对话的进程设置
AWS_PROFILE=my-profile,并检查~/.aws/config中是否有重复/遗留的 profile 定义。登录后仍然"过期"?清除 token 缓存并重新登录——
aws sso logout是删除缓存凭证的文档化方式(它们位于~/.aws/sso/cache):aws sso logout rm -rf ~/.aws/sso/cache # only if a stale cache file still lingers aws sso login --profile my-profile对于长时间运行或无头的工作负载,不要使用 SSO profile——一个交互式浏览器登录无法无人值守地刷新。改用附加到计算资源的 IAM 角色(实例/任务/执行角色)或一个 CI OIDC 角色。
会话过期快得烦人? 会话时长是一个 Identity Center 设置——请你的管理员延长它。
桌面工具继承同样的修复:一旦 aws sso login 成功,DynoTable 桌面应用就会接收你刷新后的 SSO profile 并重新连接到你的表。在会话有效期间,DynamoDB Expression Builder 帮你把想查询的东西变成一个正确的请求。
相关错误
- The security token included in the request is expired——过期的_角色_凭证,而非 SSO 会话。
- The security token included in the request is invalid
- Unable to locate credentials (boto3)
References
- Configuring IAM Identity Center authentication with the AWS CLI — AWS CLI User Guide
- sso login — AWS CLI Command Reference
- Set session duration for AWS accounts — IAM Identity Center User Guide
最后核实于 2026-07-13,依据上方链接的 AWS 官方文档。