使用 AWS CLI 設定 IAM Identity Center 身分驗證 - AWS Command Line Interface

使用 AWS CLI 設定 IAM Identity Center 身分驗證

本主題提供如何使用 AWS IAM Identity Center (IAM Identity Center) 設定 AWS CLI 以擷取憑證來執行 AWS CLI 命令的指示。使用 IAM Identity Center 驗證使用者身分以取得憑證,藉此透過 config 檔案執行 AWS CLI 命令的方式主要有兩種:

  • (建議) SSO 權杖提供者組態。

  • 舊版不可重新整理的配置。

如需使用不需帳戶 ID 和角色之承載驗證的相關資訊,請參閱 Amazon CodeCatalyst 使用者指南中的設定以將 AWS CLI 搭配 CodeCatalyst 使用

注意

如需搭配使用 IAM Identity Center 與 AWS CLI 命令的引導式程序,請參閱 教學課程:使用 IAM Identity Center 在 AWS CLI 中執行 Amazon S3 命令

主題

先決條件

  • 安裝AWS CLI。如需詳細資訊,請參閱 安裝或更新至 AWS CLI 的最新版本

  • 您必須先有辦法存取 IAM Identity Center 中的 SSO 身分驗證。請從下列方式擇一,存取您的 AWS 憑證。

請遵循 AWS IAM Identity Center 使用者指南入門說明。此程序可以啟動 IAM Identity Center、建立管理使用者,並新增適當的最低權限許可集。

注意

建立套用最低權限許可的許可集。建議您使用預先定義的 PowerUserAccess 許可集,除非您的雇主已為此目的建立了自訂許可集。

退出入口網站並再次登入,以查看 AWS 帳戶、以程式設計方式存取詳細資料,及 AdministratorPowerUserAccess 的選項。在使用 SDK 時選取 PowerUserAccess

透過身分提供者的入口網站登入 AWS。若雲端管理員已授予您 PowerUserAccess (開發人員) 權限,您會看到您有權存取的 AWS 帳戶 和許可集。您會在許可集名稱旁,看到使用該許可集手動或以程式設計方式存取帳戶的選項。

若您自訂實作,可能會產生不同體驗,例如不同的許可集名稱。若您不確定要使用哪個許可集,請聯絡您的 IT 團隊尋求協助。

透過您的 AWS 存取入口網站來登入 AWS。若雲端管理員已授予您 PowerUserAccess (開發人員) 權限,您會看到您有權存取的 AWS 帳戶 和許可集。您會在許可集名稱旁,看到使用該許可集手動或以程式設計方式存取帳戶的選項。

請聯絡您的 IT 團隊尋求協助。

取得 IAM Identity Center 的存取權後,請執行下列動作來收集您的 IAM Identity Center 資訊:

  1. 收集您需要執行 aws configure ssoSSO Start URLSSO Region

    1. 在您的 AWS 存取入口網站中,選取您用於開發的許可集合,然後選取存取金鑰連結。

    2. 取得憑證對話方塊中,選擇符合您作業系統的索引標籤。

    3. 選擇 IAM Identity Center 憑證方法,以取得 SSO Start URLSSO Region 值。

  2. 或者,從 2.22.0 版起,您可以使用發行者 URL,而不是啟動 URL。發行者 URL 位於下列其中一個位置的 AWS IAM Identity Center 主控台中:

    • 儀表板頁面上,發行者 URL 位於設定摘要中。

    • 設定頁面上,發行者 URL 位於身分來源設定中。

  3. 如需要在哪些範圍註冊值的詳細資訊,請參閱《IAM Identity Center 使用者指南》中的 OAuth 2.0 存取範圍

使用 aws configure sso 精靈設定您的設定檔

若要為您的 AWS CLI 設定 IAM Identity Center 設定檔:
  1. 在您偏好的終端機中執行 aws configure sso 命令。

    (Recommended) IAM Identity Center

    建立工作階段名稱、提供您的 IAM Identity Center 啟動 URL 或發行者 URL、託管 IAM Identity Center 目錄的 AWS 區域,以及註冊範圍。

    $ aws configure sso SSO session name (Recommended): my-sso SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

    代碼交換證明金鑰 (PKCE) 授權自 2.22.0 版起為 AWS CLI 的預設值,且必須在具有瀏覽器的裝置上使用。若要繼續使用裝置授權,請附加 --use-device-code 選項。

    $ aws configure sso --use-device-code
    Legacy IAM Identity Center

    略過工作階段名稱,並提供您的 IAM Identity Center 啟動 URL,以及託管 Identity Center 目錄的 AWS 區域。

    $ aws configure sso SSO session name (Recommended): SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]:us-east-1
  2. AWS CLI 會嘗試開啟您的預設瀏覽器,以進行 IAM Identity Center 帳戶的登入程序。此程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的開發套件之上,因此許可訊息可能會包含 botocore 名稱的變體。

    • 如果 AWS CLI 無法開啟瀏覽器,系統會根據您使用的授權類型,顯示手動啟動登入程序的指示。

      PKCE authorization

      代碼交換證明金鑰 (PKCE) 授權自 2.22.0 版起為 AWS CLI 的預設值。顯示的 URL 為唯一 URL,開頭為:https://oidc.us-east-1.amazonaws.com/authorize

      PKCE 授權 URL 必須在您登入的同一個裝置上開啟,且必須用於具有瀏覽器的裝置。

      Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize the request, open the following URL: https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
      Device authorization

      2.22.0 版之前的 AWS CLI 使用 OAuth 2.0 裝置授權。您可以使用 --use-device-code 選項在較新版本上啟用此方法。

      裝置授權 URL 不需要在您登入的同一個裝置上開啟,且可用於有或無瀏覽器的裝置。

      If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-west-2.amazonaws.com/ Then enter the code: QCFK-N451
  3. 從顯示的清單中選擇要使用的 AWS 帳戶。如果授權您只能使用一個帳戶,則 AWS CLI 會自動選取該帳戶並略過提示。

    There are 2 AWS accounts available to you. > DeveloperAccount, developer-account-admin@example.com (123456789011) ProductionAccount, production-account-admin@example.com (123456789022)
  4. 從顯示的清單中選擇要使用的 IAM 角色。如果只有一個角色可供使用,則 AWS CLI 會自動選取該角色並略過提示。

    Using the account ID 123456789011 There are 2 roles available to you. > ReadOnly FullAccess
  5. 指定預設輸出格式、要將命令傳往的預設 AWS 區域,以及設定檔名稱。如果您指定 default 做為設定檔名稱,則此設定檔會成為使用的預設設定檔。在下列範例中,使用者會輸入預設區域、預設輸出格式和設定檔的名稱。

    Default client Region [None]: us-west-2<ENTER> CLI default output format (json if not specified) [None]: json<ENTER> Profile name [123456789011_ReadOnly]: my-dev-profile<ENTER>
  6. 最後一則訊息說明完成的設定檔組態。您現在可以使用此設定檔來請求憑證。使用 aws sso login 命令來請求和擷取執行命令所需的憑證。如需說明,請參閱登入 IAM Identity Center 工作階段

這些步驟會造成在 config 檔案中建立 sso-session 區段及具名設定檔,如下所示:

IAM Identity Center
[profile my-dev-profile] sso_session = my-sso sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start sso_registration_scopes = sso:account:access
Legacy IAM Identity Center
[profile my-dev-profile] sso_start_url = https://my-sso-portal.awsapps.com/start sso_region = us-east-1 sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json

使用 aws configure sso-session 精靈只設定 sso-session 區段

注意

此組態與舊版 IAM Identity Center 不相容。

aws configure sso-session 命令會更新 ~/.aws/config 檔案中的 sso-session 區段。執行 aws configure sso-session 命令,並提供 IAM Identity Center 啟動 URL 或發行者 URL,以及託管 Identity Center 目錄的 AWS 區域。

$ aws configure sso-session SSO session name: my-sso SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

使用 config 檔案手動設定

IAM Identity Center 組態資訊會存放在 config 檔案中,且可以使用文字編輯器進行編輯。若要手動將 IAM Identity Center 支援新增至具名設定檔,您必須將索引鍵和值新增至 config 檔案。

config 檔案的 sso-session 區段用於將配置變數分組以取得 SSO 存取字符,然後用於取得 AWS 憑證。使用下列的設定:

您可以定義 sso-session 區段,並將其與設定檔建立關聯。sso_regionsso_start_url 設定必須在 sso-session 區段中設定。一般而言,sso_account_idsso_role_name 必須在 profile 區段中設定,以讓開發套件請求 SSO 憑證。

下列範例會將開發套件設定為請求 SSO憑認證,並支援自動字符重新整理:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start

這也讓 sso-session 組態能在多個設定檔中重複使用:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [profile prod] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole2 [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start

然而,SSO 字符組態並非所有情況都需要 sso_account_idsso_role_name。如果您的應用程式只使用支援承載身分驗證的 AWS 服務,則不需要傳統的 AWS 憑證。承載身分驗證是一種 HTTP 身分驗證結構描述,使用稱為承載字符的安全字符。在這種情況下,sso_account_idsso_role_name 並非必要資訊。請參閱您 AWS 服務的個別指南,以確定其是否支援承載字符授權。

此外,註冊範圍也可以設定為 sso-session 的一部分。範圍是 OAuth 2.0 中的一種機制,用於限制應用程式對使用者帳戶的存取。應用程式可以請求一個或多個範圍,則合法給應用程式的存取字符將僅限於授予的範圍。這些範圍定義為已註冊 OIDC 用戶端和用戶端擷取的存取字符進行授權所需請求的許可。下列範例設定 sso_registration_scopes 以提供列出帳戶/角色的存取權限:

[sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start sso_registration_scopes = sso:account:access

身分驗證字符會以基於工作階段名稱的檔案名稱快取至 sso/cache 目錄下的磁碟。

注意

使用舊版不可重新整理的組態,不支援自動字符重新整理。我們建議使用 SSO 字符組態。

若要手動將 IAM Identity Center 支援新增至具名設定檔,您必須將下列索引鍵和值新增至 config 檔案中的設定檔定義。

您可以在 .aws/config 檔案中包含有效的任何其他索引鍵和值。以下範例是 IAM Identity Center 設定檔:

[profile my-sso-profile] sso_start_url = https://my-sso-portal.awsapps.com/start sso_region = us-west-2 sso_account_id = 111122223333 sso_role_name = SSOReadOnlyRole region = us-west-2 output = json

若要執行命令,您必須先進行 登入 IAM Identity Center 工作階段 來請求和擷取暫時憑證。

如需 configcredentials 檔案的詳細資訊,請參閱 AWS CLI 中的組態與憑證檔案設定

登入 IAM Identity Center 工作階段

注意

登入程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的開發套件之上,因此許可訊息可能會包含 botocore 名稱的變體。

若要擷取和快取 IAM Identity Center 憑證集,請為 AWS CLI 執行下列命令,以開啟預設瀏覽器並驗證您的 IAM Identity Center 登入。

$ aws sso login --profile my-dev-profile SSO authorization page has automatically been opened in your default browser. Follow the instructions in the browser to complete this authorization request. Successfully logged into Start URL: https://my-sso-portal.awsapps.com/start

系統會快取您的 IAM Identity Center工作階段憑證,且 AWS CLI 會將其用於安全地擷取設定檔中所指定 IAM 角色的 AWS 憑證。

如果 AWS CLI 無法自動開啟瀏覽器,系統會根據您使用的授權類型,顯示手動啟動登入程序的指示。

PKCE authorization

代碼交換證明金鑰 (PKCE) 授權自 2.22.0 版起為 AWS CLI 的預設值。顯示的 URL 為唯一 URL,開頭為:https://oidc.us-east-1.amazonaws.com/authorize

PKCE 授權 URL 必須在您登入的同一個裝置上開啟,且必須用於具有瀏覽器的裝置。

Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize the request, open the following URL: https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
Device authorization

2.22.0 版之前的 AWS CLI 使用 OAuth 2.0 裝置授權。您可以使用 --use-device-code 選項在較新版本上啟用此方法。

裝置授權 URL 不需要在您登入的同一個裝置上開啟,且可用於有或無瀏覽器的裝置。

If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-west-2.amazonaws.com/ Then enter the code: QCFK-N451

您也可以指定使用 aws sso login 指令的 --sso-session 參數登入時要使用的 sso-session 設定檔。sso-session 選項不適用於舊版 IAM Identity Center。

$ aws sso login --sso-session my-dev-session

自 2.22.0 版起,PKCE 授權為預設值。若要使用裝置授權登入,請新增 --use-device-code 選項。

$ aws sso login --profile my-dev-profile --use-device-code

身分驗證權杖會以基於 sso_start_url 的檔案名稱快取至 ~/.aws/sso/cache 目錄下的磁碟。

使用您的 IAM Identity Center 設定檔執行命令

登入時,您可以使用您的憑證來以關聯的具名設定檔調用 AWS CLI 命令。下列範例使用設定檔顯示命令:

$ aws sts get-caller-identity --profile my-dev-profile

只要您已登入 IAM Identity Center,而且那些快取的憑證尚未過期,AWS CLI 就會在需要時自動更新過期的 AWS 憑證。但是,如果您的 IAM Identity Center 憑證過期,必須重新登入您的 IAM Identity Center 帳戶,以明確更新它們。

登出您的 IAM Identity Center 工作階段

完成使用 IAM Identity Center 設定檔後,您可以讓憑證過期,或執行下列命令來刪除快取的憑證。

$ aws sso logout Successfully signed out of all SSO profiles.

故障診斷

如果您遭遇有關使用 AWS CLI 的問題,請參閱 故障診斷 AWS CLI 的錯誤 以瞭解故障診斷步驟。

相關資源

其他資源如下所示。