如何使用 AWS CLI 取得 IAM Identity Center 使用者的暫時憑證?

1 分的閱讀內容
0

我想要取得 AWS IAM Identity Center (AWS Single Sign-On 的後繼者) 使用者的暫時憑證。

簡短描述

將已命名設定檔設定為使用 IAM Identity Center 會在 $ cd ~/.aws/sso/cache 目錄中建立 JSON 檔案。JSON 檔案包含 JSON Web 權杖 (JWT),用於透過 get-role-credentials API 呼叫取得暫時安全憑證。存取權杖的有效期為 8 小時,如 JSON 檔案中的 expiresAt 時間戳記所述。過期的權杖必須使用 get-role-credentials API 呼叫重新驗證。

解決方法

您可以使用 AWS Command Line Interface (AWS CLI) 取得 IAM Identity Center 使用者的暫時憑證。

**注意:**如果您在執行 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"}

執行 AWS CLI 命令 get-role-credentials 以取得 IAM 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>

範例輸出:

{
    "roleCredentials": {
        "accessKeyId": "ASIA*************”,
        "secretAccessKey": “**********************************”,
        "sessionToken": “****************************************”,
        "expiration": 1592362463000
    }
}

然後,依照指示將憑證設定為環境變數


相關資訊

如何使用 IAM Identity Center 許可集?

AWS 官方
AWS 官方已更新 3 年前