跳至內容

為什麼我無法在 Amazon EC2 執行個體上執行 AWS CLI 命令?

2 分的閱讀內容
0

我在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤。

簡短說明

在 EC2 執行個體上執行 AWS CLI 命令時,您可能會收到類似以下範例的錯誤訊息:

  • 「Unable to locate credentials.You can configure credentials by running 'aws configure'」
  • 「An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation」
    注意: 請利用錯誤訊息找出失敗的 API 呼叫。在上述範例中,DescribeInstances API 呼叫發生錯誤。
  • 「An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials」
  • 「An error occurred (ExpiredToken) when calling the GetCallerIdentity operation: The security token included in the request is expired」

若要疑難排解這些錯誤,請根據收到的錯誤採取以下措施。

注意: 如果您透過 AWS PrivateLink 連線至執行個體,請確認您能連線至該服務的虛擬私有雲端 (VPC) 端點。如需私有子網路中執行個體的相關資訊,請參閱如何建立 Amazon Virtual Private Cloud (Amazon VPC) 端點,以便透過 AWS Systems Manager 管理無法存取網際網路的私有 Amazon EC2 執行個體?

解決方法

注意: 如果執行 AWS CLI 命令時收到錯誤,請參閱疑難排解 AWS CLI 錯誤。此外,請確認您使用的是最新版本的 AWS CLI。

疑難排解「unable to locate credentials」錯誤

執行以下 list 命令,確認 AWS CLI 能否擷取憑證:

aws configure list

如果輸出未包含您的憑證,請設定 AWS CLI 的憑證。

如果您使用 AWS Identity and Access Management (IAM) 執行個體設定檔,請確認 IAM 角色關聯已完成。若要確認該角色可見,請執行以下 get-caller-identity 命令:

aws sts get-caller-identity

如果輸出未包含執行個體設定檔角色,請將執行個體角色重新連接至執行個體設定檔。

疑難排解「UnauthorizedOperation」和「AuthFailure」錯誤

請採取以下措施:

疑難排解「ExpiredToken」錯誤

如果您使用暫時性憑證,這些憑證會在建立時所指定的時間到期。IAM 角色憑證到期後,若要取得新的 STS 權杖,請擔任新的 IAM 角色。

相關資訊

如何疑難排解 IAM 政策的存取遭拒或未授權作業錯誤?

AWS 官方已更新 9 個月前