本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
設定 AWS CLI
本主題說明如何快速設定 AWS Command Line Interface (AWS CLI) 用來與之互動的基本設定 AWS。其中包括您的安全憑據,默認輸出格式和默認 AWS 區域。
收集您的憑證資訊以進行程式設計存取
如果您想要與 AWS 之外的 AWS Management Console. 針對驗證和憑證指示,請從下列選項擇一:
身分驗證類型 |
用途 |
指示 |
IAM 身分中心人力使用者的短期登入
|
(建議使用) 針對 IAM 身分中心員工使用者使用短期登入資料。 安全性最佳做法是搭 AWS Organizations 配 IAM 身分中心使用。它結合了短期登入資料與使用者目錄,例如內建的 IAM 身分識別中心目錄或 Active Directory。 |
設定 AWS CLI 要使用的 AWS IAM Identity Center |
IAM 用戶短期登入資料 |
使用 IAM 使用者短期登入資料,這比長期登入資料更安全。如果您的憑證遭到入侵,則可在到期前使用有限的時間。 |
使用短期憑證進行驗證 |
Amazon EC2 執行個體上的 IAM 或身分識別中心使用者。 |
使用 Amazon EC2 執行個體中繼資料,使用指派給 Amazon EC2 執行個體的角色查詢臨時登入資料。 |
使用 Amazon EC2 執行個體中繼資料的憑證 |
假設權限的角色 |
配對另一個認證方法,並假定用於暫時存取 AWS 服務 您的使用者的角色可能無法存取。 |
在 AWS CLI 中使用 IAM 角色 |
IAM 用戶長期登入資料 |
(不建議使用) 使用沒有過期的長期認證。 |
使用 IAM 使用者憑證進行驗證 |
IAM 或 IAM 身分中心員工使用者的外部儲存 |
(不建議) 配對另一個認證方法,但將認證值儲存在 AWS CLI. 此方法僅與儲存認證的外部位置一樣安全。 |
透過外部程序取得憑證 |
設定新的組態和憑證
會將您的組態和認證資訊 AWS CLI 儲存在和檔案中的設定config
檔 (設定credentials
集合) 中。
下列範例會針對每個驗證方式使用範例值。使用您自己的值來取代範例值。
使用 AWS CLI
命令進行設定
一般來說,您偏好的終端中的 aws configure
或 aws configure sso
命令是設定您的 AWS CLI
安裝最快的方法。根據您偏好的認證方法, AWS CLI 系統會提示您輸入相關資訊。根據預設,當您執行未明確指定要使用的設定檔的 AWS CLI 命令時,會使用此設定檔中的資訊。
如需 credentials
和 config
檔案的詳細資訊,請參閱 組態與憑證檔案設定。
- IAM Identity Center
(SSO)
-
此範例適 AWS IAM Identity Center 用於使用aws configure sso
精靈。如需詳細資訊,請參閱 設定使用具有自動身份驗證重新整理的 IAM 身分中心令牌提供者憑據 AWS CLI。
$
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
Attempting to automatically open the SSO authorization page in your default browser.
There are 2 AWS accounts available to you.
> DeveloperAccount, developer-account-admin@example.com (111122223333
)
ProductionAccount, production-account-admin@example.com (444455556666
)
Using the account ID 111122223333
There are 2 roles available to you.
> ReadOnly
FullAccess
Using the role name "ReadOnly"
CLI default client Region [None]:
us-west-2
CLI default output format [None]:
json
CLI profile name [123456789011_ReadOnly]:
user1
- IAM Identity Center
(Legacy SSO)
-
此範例適用於使 AWS IAM Identity Center 用aws configure sso
精靈的舊式方法。若要使用舊版 SSO,請將工作階段名稱留白。如需詳細資訊,請參閱 AWS IAM Identity Center舊版不可重新整理的配置。
$
aws configure sso
SSO session name (Recommended):
SSO start URL [None]:
https://my-sso-portal.awsapps.com/start
SSO region [None]:
us-east-1
SSO authorization page has automatically been opened in your default browser.
Follow the instructions in the browser to complete this authorization request.
There are 2 AWS accounts available to you.
> DeveloperAccount, developer-account-admin@example.com (111122223333
)
ProductionAccount, production-account-admin@example.com (444455556666
)
Using the account ID 111122223333
There are 2 roles available to you.
> ReadOnly
FullAccess
Using the role name "ReadOnly"
CLI default client Region [None]:
us-west-2
CLI default output format [None]:
json
CLI profile name [123456789011_ReadOnly]:
user1
- Short-term credentials
-
此範例適用於來自 AWS Identity and Access Management的短期憑證。aws 配置精靈是用於設定初始值,而 aws configure set
命令會指派所需的最後值。如需詳細資訊,請參閱 使用短期憑證進行驗證。
$
aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
$
aws configure set aws_session_token fcZib3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
- IAM role
-
此範例適用於 IAM 角色之擔任。使用 IAM 角色的設定檔會從其他設定檔提取憑證,接著套用 IAM 角色許可。在下列範例中,default
是憑證的來源設定檔,且 user1
借用相同憑證來擔任新角色。此程序中沒有精靈,因此每個值皆使用 aws configure set
命令設定。如需詳細資訊,請參閱 在 AWS CLI 中使用 IAM 角色。
$
aws configure set role_arn arn:aws:iam::123456789012:role/defaultrole
$
aws configure set source_profile default
$
aws configure set role_session_name session_user1
$
aws configure set region us-west-2
$
aws configure set output json
- Amazon EC2 instance metadata credentials
-
此範例適用從託管 Amazon EC2 執行個體中繼資料取得的憑證。此程序中沒有精靈,因此每個值皆使用 aws configure
set
命令設定。如需詳細資訊,請參閱 使用 Amazon EC2 執行個體中繼資料的憑證。
$
aws configure set role_arn arn:aws:iam::123456789012:role/defaultrole
$
aws configure set credential_source Ec2InstanceMetadata
$
aws configure set region us-west-2
$
aws configure set output json
- Long-term credentials
-
此範例適用於來自 AWS Identity and Access Management的長期憑證。如需詳細資訊,請參閱 使用 IAM 使用者憑證進行驗證。
$
aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
如需深入了解驗證與憑證的詳細資訊,請參閱 驗證與存取憑證。
手動編輯憑證和設定檔
複製貼上資訊時,建議您手動編輯 config
和 credentials
檔案。根據您偏好的憑證方法,檔案會以不同方式設定。
檔案會儲存在 .aws
資料夾下的主目錄中。主目錄的位置根據作業系統而不同,但在 Windows 中是使用環境變數 %UserProfile%
來參考,而在 Unix 系統中是使用 $HOME
或 ~
(波狀符號) 來參考。如需這些設定儲存位置的詳細資訊,請參閱 組態設定存放在何處?。
以下範例使用範例值,且顯示 default
設定檔和名為 user1
的設定檔。使用您自己的值來取代範例值。如需 credentials
和 config
檔案的詳細資訊,請參閱 組態與憑證檔案設定。
- IAM Identity Center
(SSO)
-
此範例適用於 AWS IAM Identity Center. 如需詳細資訊,請參閱 設定使用具有自動身份驗證重新整理的 IAM 身分中心令牌提供者憑據 AWS CLI。
憑證檔案
此驗證方法不會使用 credentials
檔案。
組態檔案
[default]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = readOnly
region = us-west-2
output = text
[profile user1]
sso_session = my-sso
sso_account_id = 444455556666
sso_role_name = readOnly
region = us-east-1
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
- IAM Identity Center
(Legacy SSO)
-
此範例適用於的舊式方法 AWS IAM Identity Center。如需詳細資訊,請參閱 AWS IAM Identity Center舊版不可重新整理的配置。
憑證檔案
此驗證方法不會使用 credentials
檔案。
組態檔案
[default]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-east-1
sso_account_id = 111122223333
sso_role_name = readOnly
region = us-west-2
output = text
[profile user1]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-east-1
sso_account_id = 444455556666
sso_role_name = readOnly
region = us-east-1
output = json
- Short-term credentials
-
此範例適用於來自 AWS Identity and Access Management的短期憑證。如需詳細資訊,請參閱 使用短期憑證進行驗證。
憑證檔案
[default]
aws_access_key_id=ASIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
aws_session_token = IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
[user1]
aws_access_key_id=ASIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
aws_session_token = fcZib3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
組態檔案
[default]
region=us-west-2
output=json
[profile user1]
region=us-east-1
output=text
- IAM role
-
此範例適用於 IAM 角色之擔任。使用 IAM 角色的設定檔會從其他設定檔提取憑證,接著套用 IAM 角色許可。在下列範例中,default
是憑證的來源設定檔,且 user1
借用相同憑證來擔任新角色。如需詳細資訊,請參閱 在 AWS CLI 中使用 IAM 角色。
憑證檔案
credentials
檔案取決於您的來源設定檔使用哪種身分驗證。在下列範例中,來源設定檔是使用短期憑證。
[default]
aws_access_key_id=ASIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
aws_session_token = IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
組態檔案
[default]
region=us-west-2
output=json
[profile user1]
role_arn=arn:aws:iam::777788889999:role/user1role
source_profile=default
role_session_name=session_user1
region=us-east-1
output=text
- Amazon EC2 instance metadata credentials
-
此範例適用從託管 Amazon EC2 執行個體中繼資料取得的憑證。如需詳細資訊,請參閱 使用 Amazon EC2 執行個體中繼資料的憑證。
憑證檔案
此驗證方法不會使用 credentials
檔案。
組態檔案
[default]
role_arn=arn:aws:iam::123456789012:role/defaultrole
credential_source=Ec2InstanceMetadata
region=us-west-2
output=json
[profile user1]
role_arn=arn:aws:iam::777788889999:role/user1role
credential_source=Ec2InstanceMetadata
region=us-east-1
output=text
- Long-term credentials
-
此範例適用於來自 AWS Identity and Access Management的長期憑證。如需詳細資訊,請參閱 使用 IAM 使用者憑證進行驗證。
憑證檔案
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
組態檔案
[default]
region=us-west-2
output=json
[profile user1]
region=us-east-1
output=text
如需深入了解驗證與憑證的詳細資訊,請參閱 驗證與存取憑證。
使用現有組態和憑證檔案
如果您有現有的組態和憑證檔案,則這些檔案可用於 AWS CLI。
若要使用 config
和 credentials
檔案,將它們移動到主目錄中名為 .aws
的資料夾。主目錄的位置根據作業系統而不同,但在 Windows 中是使用環境變數 %UserProfile%
來參考,而在 Unix 系統中是使用 $HOME
或 ~
(波狀符號) 來參考。
您可以將 AWS_CONFIG_FILE
和 AWS_SHARED_CREDENTIALS_FILE
環境變數設為其他本機路徑,以指定 config
和 credentials
檔案的非預設位置。如需詳細資訊,請參閱 要設定的環境變數 AWS CLI。
如需關於組態和憑證檔案的詳細資訊,請參閱 組態與憑證檔案設定。