跳至内容

为什么我无法在 Amazon EC2 实例上运行 AWS CLI 命令?

2 分钟阅读
0

我在 Amazon Elastic Compute Cloud (Amazon EC2) 实例上运行 AWS 命令行界面 (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 官方已更新 5 个月前