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 当作略早于其实际到期时间就过期了)。

如何修复

  1. 重新认证正确的 profile:

    aws sso login --profile my-profile
    aws sts get-caller-identity --profile my-profile   # confirm it worked
  2. 确保你的代码使用那同一个 profile——为与 DynamoDB 对话的进程设置 AWS_PROFILE=my-profile,并检查 ~/.aws/config 中是否有重复/遗留的 profile 定义。

  3. 登录后仍然"过期"?清除 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
  4. 对于长时间运行或无头的工作负载,不要使用 SSO profile——一个交互式浏览器登录无法无人值守地刷新。改用附加到计算资源的 IAM 角色(实例/任务/执行角色)或一个 CI OIDC 角色。

  5. 会话过期快得烦人? 会话时长是一个 Identity Center 设置——请你的管理员延长它。

桌面工具继承同样的修复:一旦 aws sso login 成功,DynoTable 桌面应用就会接收你刷新后的 SSO profile 并重新连接到你的表。在会话有效期间,DynamoDB Expression Builder 帮你把想查询的东西变成一个正确的请求。

相关错误

References

最后核实于 2026-07-13,依据上方链接的 AWS 官方文档。

无需控制台即可使用 DynamoDB

DynoTable 是面向 DynamoDB 的快速桌面客户端 —— 浏览表、运行 SQL 风格的查询,并在本地编辑项。