我想获得 AWS IAM Identity Center 用户的临时凭证。
简短描述
要生成临时凭证,必须运行 get-role-credentials 命令。当您配置一个命名配置文件以使用 IAM Identity Center 时,AWS 命令行界面 (AWS CLI) 会在 cd ~/.aws/sso/cache 目录中创建一个 JSON 文件。该 JSON 文件包含一个 JSON Web 令牌 (JWT),此令牌用于通过 get-role-credentials 命令获取临时安全凭证。该访问令牌的有效期为 8 小时。JSON 文件中的 expiresAt 时间戳显示到期时间。
解决方法
**注意:**如果您在运行 AWS CLI 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI。
获取临时凭证
完成以下步骤:
-
打开以下 JSON 文件,然后找到访问令牌:
cat 535a8450b05870c9045c8a7b95870.json
{"startUrl": "https://my-sso-portal.awsapps.com/start", "region": "us-east-1", "accessToken": "eyJlbmMiOiJBM….", "expiresAt": "2020-06-17T10:02:08UTC"}
-
复制 accessToken 值。
-
运行以下 get-role-credentials 命令,以获取 Identity Center 用户的凭证:
aws sso get-role-credentials --account-id 123456789012 --role-name permission-set-name --access-token eyJlbmMiOiJBM…. --region enter_the_same_sso_region_same_in_the_JSON_file
**注意:**请将 enter_the_same_sso_region_same_in_the_JSON_file 替换为 JSON 文件中的 AWS 区域,将 permission-set-name 替换为您的角色名称。如果您使用了错误的角色名称,则会收到错误“An error occurred (ForbiddenException) when calling the GetRoleCredentials operation: No access.” 如果会话令牌或区域与 JSON 文件输出不匹配,则您会收到错误“An error occurred (UnauthorizedException) when calling the GetRoleCredentials operation: Session token not found or invalid.”
输出示例:
{ "roleCredentials": {
"accessKeyId": "ASIA*************",
"secretAccessKey": "**********************************",
"sessionToken": "****************************************",
"expiration": 1592362463000
}
}
-
将凭证配置为环境变量。
相关信息
如何使用 IAM Identity Center 权限集?