本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
設定 AWS CLI
本主題說明如何快速設定 AWS Command Line Interface (AWS CLI) 用來與之互動的基本設定 AWS。其中包括您的安全登入資料、預設輸出格式和預設 AWS 區域。
收集您的憑證資訊以進行程式設計存取
如果您想要在 AWS 外部與 互動,則需要程式設計存取 AWS 管理主控台。針對驗證和憑證指示,請從下列選項擇一:
設定新的組態和憑證
會將您的組態和登入資料資訊 AWS CLI 儲存在 credentials和 config檔案中的設定檔 (設定集合) 中。
下列範例會針對每個驗證方式使用範例值。使用您自己的值來取代範例值。
使用 AWS CLI
命令進行設定
一般來說,您偏好的終端中的 aws configure 或 aws configure sso 命令是設定您的 AWS CLI
安裝最快的方法。會根據您偏好的登入資料方法, AWS CLI 提示您提供相關資訊。根據預設,當您執行未明確指定要使用之設定檔的 AWS CLI 命令時,會使用此設定檔中的資訊。
如需 credentials 和 config 檔案的詳細資訊,請參閱 AWS CLI中的組態與憑證檔案設定。
- Console sessions as short-term credentials
-
此範例可讓您將現有的主控台登入資料與 aws login命令搭配使用。此登入方法可與在初始帳戶設定期間建立的根登入資料、IAM 使用者或來自身分提供者的聯合身分搭配使用。如需詳細資訊,請參閱使用主控台登入資料進行 AWS 本機開發的登入。
$ aws login
No AWS region has been configured. The AWS region is the geographic location of your AWS resources.
If you've used AWS before and already have resources in your account, tell us which region they were
created in. If you haven't created resources in your account before, you can pick the region closest to you:
https://docs.aws.amazon.com/global-infrastructure/latest/regions/aws-regions.html. You are able to change
the region in the CLI at any time with the command `aws configure set region NEW_REGION`.
AWS Region [us-east-1]: us-east-1
Attempting to open the login page for `us-east-1` in your default browser. If the browser does not open,
use the following URL to complete your login:
https://us-east-1.signin.aws.amazon.com/v1/authorize<abbreviated>
If you cannot connect to this URL, make sure that you have specified a valid region.
Waiting for login...
Logged in with role `arn:aws:sts::012345678910:user/iam-user`, and configured profile
`default` to use `us-east-1`. This session will expire on October 14, 2025 at 2:04 PST. After this time, you
can renew your session with `aws login`.
- IAM Identity Center
(SSO)
-
此範例適用於 AWS IAM Identity Center 使用aws configure sso精靈。如需詳細資訊,請參閱使用 AWS CLI 設定 IAM Identity Center 身分驗證。
$ 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 CLI 設定 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 CLI 的短期憑證進行驗證。
$ 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 命令設定。如需詳細資訊,請參閱在 中使用 IAM 角色 AWS CLI。
$ 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 命令設定。如需詳細資訊,請參閱在 AWS CLI 中使用 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 CLI。
$ 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 CLI的驗證與存取憑證。
手動編輯憑證和設定檔
複製貼上資訊時,建議您手動編輯 config 和 credentials 檔案。根據您偏好的憑證方法,檔案會以不同方式設定。
檔案會儲存在 .aws 資料夾下的主目錄中。主目錄的位置根據作業系統而不同,但在 Windows 中是使用環境變數 %UserProfile% 來參考,而在 Unix 系統中是使用 $HOME 或 ~ (波狀符號) 來參考。如需這些設定儲存位置的詳細資訊,請參閱 組態設定存放在何處?。
以下範例使用範例值,且顯示 default 設定檔和名為 user1 的設定檔。使用您自己的值來取代範例值。如需 credentials 和 config 檔案的詳細資訊,請參閱 AWS CLI中的組態與憑證檔案設定。
- IAM Identity Center
(SSO)
-
此範例適用於 AWS IAM Identity Center。如需詳細資訊,請參閱使用 AWS CLI 設定 IAM Identity Center 身分驗證。
憑證檔案
此驗證方法不會使用 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 CLI 設定 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的短期憑證。如需詳細資訊,請參閱使用 AWS CLI 的短期憑證進行驗證。
憑證檔案
[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 借用相同憑證來擔任新角色。如需詳細資訊,請參閱在 中使用 IAM 角色 AWS CLI。
憑證檔案
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 執行個體中繼資料取得的憑證。如需詳細資訊,請參閱在 AWS CLI 中使用 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 使用者登入資料進行驗證 AWS CLI。
憑證檔案
[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的驗證與存取憑證。
使用現有組態和憑證檔案
如果您有現有的組態和憑證檔案,則這些檔案可用於 AWS CLI。
若要使用 config 和 credentials 檔案,將它們移動到主目錄中名為 .aws 的資料夾。主目錄的位置根據作業系統而不同,但在 Windows 中是使用環境變數 %UserProfile% 來參考,而在 Unix 系統中是使用 $HOME 或 ~ (波狀符號) 來參考。
您可以將 AWS_CONFIG_FILE 和 AWS_SHARED_CREDENTIALS_FILE 環境變數設為其他本機路徑,以指定 config 和 credentials 檔案的非預設位置。如需詳細資訊,請參閱 設定 AWS CLI的環境變數。
如需關於組態和憑證檔案的詳細資訊,請參閱 AWS CLI中的組態與憑證檔案設定。