解决 AWS CLI 错误 - AWS Command Line Interface

本文档 AWS CLI 仅适用于版本 1。有关版本 2 的文档 AWS CLI,请参阅版本 2 用户指南

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

解决 AWS CLI 错误

本节介绍常见错误和解决您的问题的故障排除步骤。我们建议首先进行一般故障排除

首先尝试的一般故障排除

如果您收到错误或遇到问题 AWS CLI,我们建议您使用以下一般提示来帮助您进行故障排除。

回到顶部

检查您的 AWS CLI 命令格式

如果您收到一个错误,表明某个命令不存在,或者它无法识别文档指明可用的参数 (Parameter validation failed),则您的命令可能格式不正确。我们建议您检查以下内容:

有关如何构造特定命令的更多信息,请参阅AWS CLI 参考指南

回到顶部

检查 AWS 区域 你的 AWS CLI 命令正在使用什么

注意

使用 AWS 区域 时,必须明确指定或通过设置默认区域来指定。 AWS CLI有关您可以指定的所有内容的列表 AWS 区域 ,请参阅中的AWS 区域和终端节点Amazon Web Services 一般参考。使用的 AWS 区域 标号与您在 AWS Management Console 网址和服务端点中看到的名称相同。 AWS CLI

如果您指定的资源 AWS 服务 不可用,或者您的资源位于其他位置,则可能会出现错误 AWS 区域 或意外结果 AWS 区域。按优先顺序排列,按以下方式设置: AWS 区域

确认您的资源使用的是正确 AWS 区域 的。

回到顶部

确认您运行的是 AWS CLI的最新版本

如果您收到错误消息,表明某个命令不存在,或者它无法识别参考指南参考指南所说的可用参数,请首先确认您的命令格式是否正确。如果格式正确,我们建议您升级到 AWS CLI的最新版本。的更新版本几乎每个工作日都会发布。 AWS CLI 这些新版本中引入了新的 AWS 服务、功能和参数 AWS CLI。获取这些新服务、功能或参数的唯一方式是升级到首次引入该元素后发布的版本。

如何更新版本 AWS CLI 取决于您最初的安装方式,如中所述安装、更新和卸载 AWS CLI

如果您使用了某个捆绑安装程序,则可能需要先删除现有安装,然后为您的操作系统下载并安装最新版本。

回到顶部

使用 --debug 选项

当 AWS CLI 报告一个你无法立即理解的错误,或者产生了你意想不到的结果时,你可以通过使用--debug选项再次运行该命令来获得有关错误的更多详细信息。使用此选项, AWS CLI 会输出有关它处理命令所执行的每一步的详细信息。输出中的详细信息可以帮助您确定错误发生的时间,并提供错误从何处开始的线索。

您可以将输出发送到文本文件以供日后查看,或者按要求发送给 AWS Support 。

当您包含 --debug 选项时,一些详细信息包括:

  • 查找凭证

  • 解析提供的参数

  • 构造发送到 AWS 服务器的请求

  • 发送到的请求的内容 AWS

  • 原始响应的内容

  • 带格式的输出

以下是使用和不使用 --debug 选项运行命令的示例。

$ aws iam list-groups --profile MyTestProfile { "Groups": [ { "Path": "/", "GroupName": "MyTestGroup", "GroupId": "AGPA0123456789EXAMPLE", "Arn": "arn:aws:iam::123456789012:group/MyTestGroup", "CreateDate": "2019-08-12T19:34:04Z" } ] }
$ aws iam list-groups --profile MyTestProfile --debug 2019-08-12 12:36:18,305 - MainThread - awscli.clidriver - DEBUG - CLI version: aws-cli/1.16.215 Python/3.7.3 Linux/4.14.133-113.105.amzn2.x86_64 botocore/1.12.205 2019-08-12 12:36:18,305 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['iam', 'list-groups', '--debug'] 2019-08-12 12:36:18,305 - MainThread - botocore.hooks - DEBUG - Event session-initialized: calling handler <function add_scalar_parsers at 0x7fdf173161e0> 2019-08-12 12:36:18,305 - MainThread - botocore.hooks - DEBUG - Event session-initialized: calling handler <function register_uri_param_handler at 0x7fdf17dec400> 2019-08-12 12:36:18,305 - MainThread - botocore.hooks - DEBUG - Event session-initialized: calling handler <function inject_assume_role_provider_cache at 0x7fdf17da9378> 2019-08-12 12:36:18,307 - MainThread - botocore.credentials - DEBUG - Skipping environment variable credential check because profile name was explicitly set. 2019-08-12 12:36:18,307 - MainThread - botocore.hooks - DEBUG - Event session-initialized: calling handler <function attach_history_handler at 0x7fdf173ed9d8> 2019-08-12 12:36:18,308 - MainThread - botocore.loaders - DEBUG - Loading JSON file: /home/ec2-user/venv/lib/python3.7/site-packages/botocore/data/iam/2010-05-08/service-2.json 2019-08-12 12:36:18,317 - MainThread - botocore.hooks - DEBUG - Event building-command-table.iam: calling handler <function add_waiters at 0x7fdf1731a840> 2019-08-12 12:36:18,320 - MainThread - botocore.loaders - DEBUG - Loading JSON file: /home/ec2-user/venv/lib/python3.7/site-packages/botocore/data/iam/2010-05-08/waiters-2.json 2019-08-12 12:36:18,321 - MainThread - awscli.clidriver - DEBUG - OrderedDict([('path-prefix', <awscli.arguments.CLIArgument object at 0x7fdf171ac780>), ('marker', <awscli.arguments.CLIArgument object at 0x7fdf171b09e8>), ('max-items', <awscli.arguments.CLIArgument object at 0x7fdf171b09b0>)]) 2019-08-12 12:36:18,322 - MainThread - botocore.hooks - DEBUG - Event building-argument-table.iam.list-groups: calling handler <function add_streaming_output_arg at 0x7fdf17316510> 2019-08-12 12:36:18,322 - MainThread - botocore.hooks - DEBUG - Event building-argument-table.iam.list-groups: calling handler <function add_cli_input_json at 0x7fdf17da9d90> 2019-08-12 12:36:18,322 - MainThread - botocore.hooks - DEBUG - Event building-argument-table.iam.list-groups: calling handler <function unify_paging_params at 0x7fdf17328048> 2019-08-12 12:36:18,326 - MainThread - botocore.loaders - DEBUG - Loading JSON file: /home/ec2-user/venv/lib/python3.7/site-packages/botocore/data/iam/2010-05-08/paginators-1.json 2019-08-12 12:36:18,326 - MainThread - awscli.customizations.paginate - DEBUG - Modifying paging parameters for operation: ListGroups 2019-08-12 12:36:18,326 - MainThread - botocore.hooks - DEBUG - Event building-argument-table.iam.list-groups: calling handler <function add_generate_skeleton at 0x7fdf1737eae8> 2019-08-12 12:36:18,326 - MainThread - botocore.hooks - DEBUG - Event before-building-argument-table-parser.iam.list-groups: calling handler <bound method OverrideRequiredArgsArgument.override_required_args of <awscli.customizations.cliinputjson.CliInputJSONArgument object at 0x7fdf171b0a58>> 2019-08-12 12:36:18,327 - MainThread - botocore.hooks - DEBUG - Event before-building-argument-table-parser.iam.list-groups: calling handler <bound method GenerateCliSkeletonArgument.override_required_args of <awscli.customizations.generatecliskeleton.GenerateCliSkeletonArgument object at 0x7fdf171c5978>> 2019-08-12 12:36:18,327 - MainThread - botocore.hooks - DEBUG - Event operation-args-parsed.iam.list-groups: calling handler functools.partial(<function check_should_enable_pagination at 0x7fdf17328158>, ['marker', 'max-items'], {'max-items': <awscli.arguments.CLIArgument object at 0x7fdf171b09b0>}, OrderedDict([('path-prefix', <awscli.arguments.CLIArgument object at 0x7fdf171ac780>), ('marker', <awscli.arguments.CLIArgument object at 0x7fdf171b09e8>), ('max-items', <awscli.customizations.paginate.PageArgument object at 0x7fdf171c58d0>), ('cli-input-json', <awscli.customizations.cliinputjson.CliInputJSONArgument object at 0x7fdf171b0a58>), ('starting-token', <awscli.customizations.paginate.PageArgument object at 0x7fdf171b0a20>), ('page-size', <awscli.customizations.paginate.PageArgument object at 0x7fdf171c5828>), ('generate-cli-skeleton', <awscli.customizations.generatecliskeleton.GenerateCliSkeletonArgument object at 0x7fdf171c5978>)])) 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.path-prefix: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.marker: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.max-items: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.cli-input-json: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.starting-token: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.page-size: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,328 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.iam.list-groups.generate-cli-skeleton: calling handler <awscli.paramfile.URIArgumentHandler object at 0x7fdf1725c978> 2019-08-12 12:36:18,329 - MainThread - botocore.hooks - DEBUG - Event calling-command.iam.list-groups: calling handler <bound method CliInputJSONArgument.add_to_call_parameters of <awscli.customizations.cliinputjson.CliInputJSONArgument object at 0x7fdf171b0a58>> 2019-08-12 12:36:18,329 - MainThread - botocore.hooks - DEBUG - Event calling-command.iam.list-groups: calling handler <bound method GenerateCliSkeletonArgument.generate_json_skeleton of <awscli.customizations.generatecliskeleton.GenerateCliSkeletonArgument object at 0x7fdf171c5978>> 2019-08-12 12:36:18,329 - MainThread - botocore.credentials - DEBUG - Looking for credentials via: assume-role 2019-08-12 12:36:18,329 - MainThread - botocore.credentials - DEBUG - Looking for credentials via: assume-role-with-web-identity 2019-08-12 12:36:18,329 - MainThread - botocore.credentials - DEBUG - Looking for credentials via: shared-credentials-file 2019-08-12 12:36:18,329 - MainThread - botocore.credentials - INFO - Found credentials in shared credentials file: ~/.aws/credentials 2019-08-12 12:36:18,330 - MainThread - botocore.loaders - DEBUG - Loading JSON file: /home/ec2-user/venv/lib/python3.7/site-packages/botocore/data/endpoints.json 2019-08-12 12:36:18,334 - MainThread - botocore.hooks - DEBUG - Event choose-service-name: calling handler <function handle_service_name_alias at 0x7fdf1898eb70> 2019-08-12 12:36:18,337 - MainThread - botocore.hooks - DEBUG - Event creating-client-class.iam: calling handler <function add_generate_presigned_url at 0x7fdf18a028c8> 2019-08-12 12:36:18,337 - MainThread - botocore.regions - DEBUG - Using partition endpoint for iam, us-west-2: aws-global 2019-08-12 12:36:18,337 - MainThread - botocore.args - DEBUG - The s3 config key is not a dictionary type, ignoring its value of: None 2019-08-12 12:36:18,340 - MainThread - botocore.endpoint - DEBUG - Setting iam timeout as (60, 60) 2019-08-12 12:36:18,341 - MainThread - botocore.loaders - DEBUG - Loading JSON file: /home/ec2-user/venv/lib/python3.7/site-packages/botocore/data/_retry.json 2019-08-12 12:36:18,341 - MainThread - botocore.client - DEBUG - Registering retry handlers for service: iam 2019-08-12 12:36:18,342 - MainThread - botocore.hooks - DEBUG - Event before-parameter-build.iam.ListGroups: calling handler <function generate_idempotent_uuid at 0x7fdf189b10d0> 2019-08-12 12:36:18,342 - MainThread - botocore.hooks - DEBUG - Event before-call.iam.ListGroups: calling handler <function inject_api_version_header_if_needed at 0x7fdf189b2a60> 2019-08-12 12:36:18,343 - MainThread - botocore.endpoint - DEBUG - Making request for OperationModel(name=ListGroups) with params: {'url_path': '/', 'query_string': '', 'method': 'POST', 'headers': {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent': 'aws-cli/1.16.215 Python/3.7.3 Linux/4.14.133-113.105.amzn2.x86_64 botocore/1.12.205'}, 'body': {'Action': 'ListGroups', 'Version': '2010-05-08'}, 'url': 'https://iam.amazonaws.com/', 'context': {'client_region': 'aws-global', 'client_config': <botocore.config.Config object at 0x7fdf16e9a4a8>, 'has_streaming_input': False, 'auth_type': None}} 2019-08-12 12:36:18,343 - MainThread - botocore.hooks - DEBUG - Event request-created.iam.ListGroups: calling handler <bound method RequestSigner.handler of <botocore.signers.RequestSigner object at 0x7fdf16e9a470>> 2019-08-12 12:36:18,343 - MainThread - botocore.hooks - DEBUG - Event choose-signer.iam.ListGroups: calling handler <function set_operation_specific_signer at 0x7fdf18996f28> 2019-08-12 12:36:18,343 - MainThread - botocore.auth - DEBUG - Calculating signature using v4 auth. 2019-08-12 12:36:18,343 - MainThread - botocore.auth - DEBUG - CanonicalRequest: POST / content-type:application/x-www-form-urlencoded; charset=utf-8 host:iam.amazonaws.com x-amz-date:20190812T193618Z content-type;host;x-amz-date 5f776d91EXAMPLE9b8cb5eb5d6d4a787a33ae41c8cd6eEXAMPLEca69080e1e1f 2019-08-12 12:36:18,344 - MainThread - botocore.auth - DEBUG - StringToSign: AWS4-HMAC-SHA256 20190812T193618Z 20190812/us-east-1/iam/aws4_request ab7e367eEXAMPLE2769f178ea509978cf8bfa054874b3EXAMPLE8d043fab6cc9 2019-08-12 12:36:18,344 - MainThread - botocore.auth - DEBUG - Signature: d85a0EXAMPLEb40164f2f539cdc76d4f294fe822EXAMPLE18ad1ddf58a1a3ce7 2019-08-12 12:36:18,344 - MainThread - botocore.endpoint - DEBUG - Sending http request: <AWSPreparedRequest stream_output=False, method=POST, url=https://iam.amazonaws.com/, headers={'Content-Type': b'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent': b'aws-cli/1.16.215 Python/3.7.3 Linux/4.14.133-113.105.amzn2.x86_64 botocore/1.12.205', 'X-Amz-Date': b'20190812T193618Z', 'Authorization': b'AWS4-HMAC-SHA256 Credential=AKIA01234567890EXAMPLE-east-1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=d85a07692aceb401EXAMPLEa1b18ad1ddf58a1a3ce7EXAMPLE', 'Content-Length': '36'}> 2019-08-12 12:36:18,344 - MainThread - urllib3.util.retry - DEBUG - Converted retries value: False -> Retry(total=False, connect=None, read=None, redirect=0, status=None) 2019-08-12 12:36:18,344 - MainThread - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): iam.amazonaws.com:443 2019-08-12 12:36:18,664 - MainThread - urllib3.connectionpool - DEBUG - https://iam.amazonaws.com:443 "POST / HTTP/1.1" 200 570 2019-08-12 12:36:18,664 - MainThread - botocore.parsers - DEBUG - Response headers: {'x-amzn-RequestId': '74c11606-bd38-11e9-9c82-559da0adb349', 'Content-Type': 'text/xml', 'Content-Length': '570', 'Date': 'Mon, 12 Aug 2019 19:36:18 GMT'} 2019-08-12 12:36:18,664 - MainThread - botocore.parsers - DEBUG - Response body: b'<ListGroupsResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">\n <ListGroupsResult>\n <IsTruncated>false</IsTruncated>\n <Groups>\n <member>\n <Path>/</Path>\n <GroupName>MyTestGroup</GroupName>\n <Arn>arn:aws:iam::123456789012:group/MyTestGroup</Arn>\n <GroupId>AGPA1234567890EXAMPLE</GroupId>\n <CreateDate>2019-08-12T19:34:04Z</CreateDate>\n </member>\n </Groups>\n </ListGroupsResult>\n <ResponseMetadata>\n <RequestId>74c11606-bd38-11e9-9c82-559da0adb349</RequestId>\n </ResponseMetadata>\n</ListGroupsResponse>\n' 2019-08-12 12:36:18,665 - MainThread - botocore.hooks - DEBUG - Event needs-retry.iam.ListGroups: calling handler <botocore.retryhandler.RetryHandler object at 0x7fdf16e9a780> 2019-08-12 12:36:18,665 - MainThread - botocore.retryhandler - DEBUG - No retry needed. 2019-08-12 12:36:18,665 - MainThread - botocore.hooks - DEBUG - Event after-call.iam.ListGroups: calling handler <function json_decode_policies at 0x7fdf189b1d90> { "Groups": [ { "Path": "/", "GroupName": "MyTestGroup", "GroupId": "AGPA123456789012EXAMPLE", "Arn": "arn:aws:iam::123456789012:group/MyTestGroup", "CreateDate": "2019-08-12T19:34:04Z" } ] }

回到顶部

启用并查看 AWS CLI 命令历史记录日志

您可以使用cli_history文件设置启用 AWS CLI 命令历史记录日志。启用此设置后,会 AWS CLI 记录aws命令的历史记录。

您可以使用 aws history list 命令列出您的历史记录,然后使用 aws history show 命令中生成的 command_id 获取详细信息。有关更多信息,请参阅《AWS CLI 参考指南》中的 aws history

当您包含 --debug 选项时,一些详细信息包括:

  • 针对 botocore 的 API 调用

  • 状态代码

  • HTTP 响应

  • 标头

  • 返回代码

您可以使用此信息来确认参数数据和 API 调用是否按预期方式运行,然后可以推断出您的命令在哪个步骤失败。

回到顶部

确认您的配置 AWS CLI 已完成

如果您的 configcredentials 文件或您的 IAM 用户或角色未得以正确配置,可能发生各种错误。有关解决 configcredentials 文件或您的 IAM 用户或角色的错误的更多信息,请参阅访问被拒绝错误凭证无效和密钥错误

回到顶部

找不到命令错误

此错误意味着操作系统找不到该 AWS CLI 命令。安装可能不完整或需要更新。

可能的原因:您正在尝试使用比已安装版本更新的 AWS CLI 功能,或者格式不正确

错误示例文本:

$ aws s3 copy usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters] To see help text, you can run: aws help aws <command> help aws <command> <subcommand> help aws: error: argument subcommand: Invalid choice, valid choices are: ls | website cp | mv ....

如果命令格式不正确,或者您使用的是发布此功能之前的早期版本,可能会发生各种错误。有关解决围绕这两个问题的错误的更多信息,请参阅检查您的 AWS CLI 命令格式确认您运行的是 AWS CLI的最新版本

回到顶部

可能的原因:安装后需要重新启动终端

错误示例文本:

$ aws --version command not found: aws

如果在首次安装或更新后找不到该aws命令 AWS CLI,则可能需要重新启动终端才能识别任何PATH更新。

回到顶部

可能的原因: AWS CLI 未完全安装

错误示例文本

$ aws --version command not found: aws

如果在首次安装或更新后找不到该aws命令 AWS CLI,则该命令可能尚未完全安装。按照安装、更新和卸载 AWS CLI中适用于您的平台的步骤尝试重新安装。

回到顶部

可能的原因: AWS CLI 没有权限 (Linux)

如果在 Linux AWS CLI 上首次安装或更新后找不到该aws命令,则该命令可能没有execute权限访问其安装的文件夹。在 AWS CLI 安装时运行以下命令,为chmod用户提供权限 AWS CLI:PATH

$ sudo chmod -R 755 /usr/local/aws-cli/

回到顶部

可能的原因:安装期间未更新操作系统 PATH

错误示例文本:

$ aws --version command not found: aws

您可能需要将 aws 可执行文件添加到操作系统的 PATH 环境变量中。要将 AWS CLI 添加到您的PATH,请按照以下适用于您的操作系统的说明进行操作。

Linux and macOS
  1. 在您的用户目录中查找 Shell 的配置文件脚本。如果您不能确定所使用的 Shell,请运行 echo $SHELL

    $ ls -a ~ . .. .bash_logout .bash_profile .bashrc Desktop Documents Downloads
    • Bash.bash_profile.profile.bash_login

    • Zsh.zshrc

    • Tcsh.tcshrc.cshrc.login

  2. 向配置文件脚本中添加导出命令。以下命令将您的本地 bin 添加到当前 PATH 变量。

    export PATH=/usr/local/bin:$PATH
  3. 将更新的配置文件重新加载到当前会话中。

    $ source ~/.bash_profile
Windows
  1. 在 Windows 命令提示符下,使用带 /R path 参数的 where 命令来查找 aws 文件位置。结果将返回所有包含 aws 的文件夹。

    C:\> where /R c:\ aws c:\Program Files\Amazon\AWSCLIV2\aws.exe ...

    默认情况下, AWS CLI 版本 2 位于:

    c:\Program Files\Amazon\AWSCLIV2\aws.exe
  2. 按 Windows 键并输入 environment variables

  3. 从建议列表中,选择 Edit environment variables for your account(编辑您账户的环境变量)。

  4. 选择 PATH,然后选择 Edit(编辑)。

  5. 将在第一步中找到的路径添加到 Variable value(变量值)字段中,例如 C:\Program Files\Amazon\AWSCLIV2\aws.exe

  6. 选择 OK(确定)两次以应用新设置。

  7. 关闭任何运行的命令提示符并重新打开命令提示符窗口。

回到顶部

aws --version”命令返回的版本与您安装的版本不同

您的终端返回的结果可能与您预期PATH的 AWS CLI 不同。

可能的原因:安装后需要重新启动终端

如果 aws 命令显示错误的版本,您可能需要重新启动终端以便它识别任何 PATH 更新。需要关闭所有打开的终端,而不仅仅是活动的终端。

回到顶部

可能的原因:安装后需要重新启动系统

如果 aws 命令显示了错误的版本,并且重新启动终端不起作用,您可能需要重新启动系统,系统才能识别您的 PATH 更新。

回到顶部

可能的原因:您有多个版本的 AWS CLI

如果您更新了 AWS CLI 并使用了与先前安装不同的安装方法,则可能会导致安装多个版本。例如,如果您在 Linux 或 macOS 上使用 pip 进行了当前安装,但试图使用 .pkg 安装文件进行更新,这可能会导致一些冲突,尤其是当 PATH 指向旧版本时。

要解决这个问题,请卸载 AWS CLI的所有版本,然后执行净安装。

卸载所有版本后,请按照适用于您的操作系统的说明安装所需版本:AWS CLI 版本 1AWS CLI 版本 2

注意

如果在安装 AWS CLI 版本 2 并预先安装了版本 1 之后发生这种情况,则在从 AWS CLIAWS CLI 版本 1 迁移时,请按照安装说明中的迁移说明进行操作。

回到顶部

卸载后,aws --version“” 命令会返回一个版本 AWS CLI

这种情况通常发生在您的系统上还有 AWS CLI 安装的软件时。

可能的原因:卸载后需要重新启动终端

如果 aws --version 命令仍然有效,您可能需要重新启动终端以便它识别任何终端更新。

回到顶部

可能的原因:您的系统 AWS CLI 上有多个版本的,或者使用的卸载方法与最初安装时使用的卸载方法不同 AWS CLI

如果您 AWS CLI 使用与安装方法不同的方法卸载,或者安装了多个版本,则 AWS CLI 可能无法正确卸载。例如,如果您使用 pip 执行了当前安装,则必须使用 pip 卸载它。要解决此问题,请 AWS CLI 使用与安装方法相同的方法进行卸载。

  1. 按照适用于您的操作系统的说明和原始安装方法卸载 AWS CLI 版本 1AWS CLI 版本 2

  2. 关闭所有打开的终端。

  3. 打开首选终端,输入以下命令并确认没有返回任何版本。

    $ aws --version command not found: aws

    如果输出中仍列出了一个版本,则 AWS CLI 很可能是使用其他方法安装的,或者有多个版本。如果您不知道安装的是哪种方法 AWS CLI,请按照适用于您的操作系统的AWS CLI 版本 1 和AWS CLI 版本 2 的每种卸载方法的说明进行操作,直到没有收到任何版本输出。

    注意

    如果您是使用软件包管理器(pipaptbrew 等)安装 AWS CLI 的,则必须使用同一个软件包管理器来卸载它。请务必按照软件包管理器提供的有关如何卸载软件包的所有版本的说明进行操作。

回到顶部

AWS CLI 处理了一个参数名称不完整的命令

可能的原因:您使用了普遍接受的 AWS CLI 参数缩写

由于 AWS CLI 是使用 Python 构建的,因此 AWS CLI 使用 Python argparse 库,包括allow_abbrev参数。参数的缩写由识别 AWS CLI 并处理。

以下create-change-set命令示例更改了 CloudFormation 堆栈名称。该参数--change-set-n被识别为的缩写--change-set-name,然后 AWS CLI 处理该命令。

$ aws cloudformation create-change-set --stack-name my-stack --change-set-n my-change-set

当您的缩写可能代表多个命令时,参数将不会被识别为缩写。

以下create-change-set命令示例更改了 CloudFormation 堆栈名称。参数 --change-set- 无法识别为缩写,因为它可以是多个参数(例如 --change-set-name--change-set-type)的缩写。因此, AWS CLI 处理该命令。

$ aws cloudformation create-change-set --stack-name my-stack --change-set- my-change-set
警告

不要刻意使用参数缩写。缩写不可靠且不向后兼容。如果在命令中添加了任何会使缩写产生混淆的新参数,则会中断您的命令。

此外,如果参数是单值参数,则会导致命令出现意外行为。如果传递了单值参数的多个实例,则只有最后一个实例会运行。在以下示例中,参数 --filters 是单值参数。指定了参数 --filters--filter--filter 参数是 --filters 的缩写。这会导致应用 --filters 的两个实例,只有最后一个 --filter 参数会应用。

$ aws ec2 describe-vpc-peering-connections \ --filters Name=tag:TagName,Values=VpcPeeringConnection \ --filter Name=status-code,Values=active

在运行命令之前,请确认您使用的是有效参数,以防止出现意外行为。

回到顶部

访问被拒绝错误

可能的原因: AWS CLI 程序文件没有 “运行” 权限

在 Linux 或 macOS 上,确保 aws 程序具有发出调用的用户的运行权限。通常,权限设置为 755

要添加用户的运行权限,请运行以下命令,并将 ~/.local/bin/aws 替换为您计算机上指向此程序的路径。

$ chmod +x ~/.local/bin/aws

回到顶部

可能的原因:您的 IAM 身份没有执行此操作的权限

错误示例文本:

$ aws s3 ls An error occurred (AcessDenied) when calling the ListBuckets operation: Access denied.

当您运行 AWS CLI 命令时,将使用将您与 IAM 账户或角色关联的凭证代表您执行 AWS 操作。附加的策略必须向您授予权限,才能调用与通过 AWS CLI运行的命令相对应的 API 操作。

大多数命令会通过一个与命令名称匹配的名称来调用单个操作。但是,像 aws s3 sync 这样的自定义命令会调用多个 API。您可以查看命令通过使用 --debug 选项调用哪些 API。

如果您确定用户或角色具有策略分配的适当权限,请确保您的 AWS CLI 命令使用的是您期望的证书。请参阅下一节中有关凭据的内容,以验证他们使用的凭证 AWS CLI 是否符合您的期望。

有关分配 IAM 权限的信息,请参阅《IAM 用户指南》中的访问管理概述:权限和策略

回到顶部

凭证无效和密钥错误

错误示例文本:

$ aws s3 ls An error occurred (InvalidAccessKeyId) when calling the ListBuckets operation: The AWS Access Key Id you provided does not exist in our records.
$ aws s3 ls An error occurred (InvalidClientTokenId) when calling the ListBuckets operation: The security token included in the request is invalid.
可能的原因: AWS CLI 正在读取错误的凭据或来自意外的位置

AWS CLI 可能正在从与您预期不同的位置读取证书,或者您的密钥对(key pair)信息不正确。您可以运行 aws configure list 以确认使用哪些凭证。

以下示例说明如何检查用于默认配置文件的凭证。

$ aws configure list Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************XYVA shared-credentials-file secret_key ****************ZAGY shared-credentials-file region us-west-2 config-file ~/.aws/config

以下示例说明如何检查命名配置文件的凭证。

$ aws configure list --profile saanvi Name Value Type Location ---- ----- ---- -------- profile saanvi manual --profile access_key **************** shared-credentials-file secret_key **************** shared-credentials-file region us-west-2 config-file ~/.aws/config

要确认密钥对详细信息,请检查 configcredentials 文件。有关 configcredentials 文件的更多信息,请参阅配置和凭证文件设置。有关凭证和身份验证(包括凭证优先顺序)的更多信息,请参阅身份验证和访问凭证

回到顶部

可能的原因:您计算机的时钟不同步

如果您使用的凭证是有效的,则可能是您的时钟不同步。在 Linux 或 macOS 上,运行 date 以检查时间。

$ date

如果您的系统时钟在几分钟内不正确,则使用 ntpd 进行同步。

$ sudo service ntpd stop $ sudo ntpdate time.nist.gov $ sudo service ntpd start $ ntpstat

在 Windows 上,使用控制面板中的日期和时间选项来配置系统时钟。

回到顶部

签名与错误不匹配

错误示例文本:

$ aws s3 ls An error occurred (SignatureDoesNotMatch) when calling the ListBuckets operation: The request signature we calculated does not match the signature you provided. Check your key and signing method.

AWS CLI 运行命令时,它会向 AWS 服务器发送加密请求以执行相应的 AWS 服务操作。您的证书(访问密钥和私有密钥)参与加密,并 AWS 允许对提出请求的人进行身份验证。有多种因素可能会干扰此过程的正常执行,如下所示。

可能的原因:您的时钟与 AWS 服务器不同步

为了帮助防范重播攻击,在加密/解密过程中可能会使用当前时间。如果客户端和服务器的时间不一致超出允许的时间量,该过程可能会失败,并且请求会被拒绝。当您在其时钟与主机时钟不同步的虚拟机中运行命令时,也可能发生此错误。一个可能的原因是,当虚拟机休眠时,唤醒后需要一些时间才能将时钟与主机同步。

在 Linux 或 macOS 上,运行 date 以检查时间。

$ date

如果您的系统时钟在几分钟内不正确,则使用 ntpd 进行同步。

$ sudo service ntpd stop $ sudo ntpdate time.nist.gov $ sudo service ntpd start $ ntpstat

在 Windows 上,使用控制面板中的日期和时间选项来配置系统时钟。

回到顶部

可能的原因:您的操作系统对包含某些特殊字符的 AWS 密钥处理不当

如果您的 AWS 密钥包含某些特殊字符,例如、-+/%、或,则某些操作系统变体会不正确地处理字符串,从而导致密钥字符串解释不正确。

如果您使用其他工具或脚本处理密钥,例如在新实例上构建证书文件作为其创建过程的一部分的工具,则这些工具和脚本可能对特殊字符有自己的处理方式,从而导致它们被转换为 AWS 无法识别的东西。

我们建议重新生成私有密钥,以使获得的私有密钥不包含会导致问题的特殊字符。

回到顶部

找不到 Windows 控制台错误

错误示例文本:

$ aws s3 ls No Windows console found. Are you running cmd.exe?

当你使用 AWS CLI 命令时,你会收到 “未找到 Windows 控制台”。您是否正在运行 cmd.exe?” 错误消息。如果您安装的 Python 已过时,则在 AWS CLI 版本 1 中通常prompt_toolkit会出现错误。要解决此问题,请安装 Python 网站上最新版本的 prompt_toolkit

回到顶部

SSL 证书错误

可能的原因: AWS CLI 不信任您的代理服务器的证书

错误示例文本

$ aws s3 ls [SSL: CERTIFICATE_ VERIFY_FAILED] certificate verify failed

使用 AWS CLI 命令时,您会收到一条[SSL: CERTIFICATE_ VERIFY_FAILED] certificate verify failed错误消息。这是由于代理人的证书是自签名的,您的公司被设置为证书颁发机构 (CA),导致您 AWS CLI 不信任代理人的证书。这可以防止在本地 CA 注册表中找到您公司的 CA 根证书。 AWS CLI

要解决此问题,请使用ca_bundle配置.pem文件设置、--ca-bundle命令行选项或AWS_CA_BUNDLE环境变量指示在 AWS CLI 哪里可以找到您的公司文件。

回到顶部

可能的原因:您的配置未指向正确的 CA 根证书位置

错误示例文本:

$ aws s3 ls SSL validation failed for regionname [Errno 2] No such file or directory

这是由于您的证书颁发机构 (CA)捆绑包文件位置在 AWS CLI中配置不正确所致。要解决此问题,请确认您的公司 .pem 文件所在的位置,并使用 ca_bundle 配置文件设置、--ca-bundle 命令行选项或 AWS_CA_BUNDLE 环境变量更新 AWS CLI 配置。

回到顶部

可能的原因:您的配置使用不正确 AWS 区域

错误示例文本

$ aws s3 ls [SSL: CERTIFICATE_ VERIFY_FAILED] certificate verify failed

如果您指定的资源 AWS 服务 不可用,或者您的资源位于其他位置,则可能会出现错误 AWS 区域 或意外结果 AWS 区域。有关问题排查步骤,请参阅检查 AWS 区域 你的 AWS CLI 命令正在使用什么

回到顶部

可能的原因:你的 TLS 版本需要更新

错误示例文本

$ aws s3 ls [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled

使用 AWS 服务 的 TLS 版本与您设备的 TLS 版本不兼容。要解决此问题,请更新到支持的 TLS 版本。有关更多信息,请参阅 强制使用最低版本的 TLS

回到顶部

JSON 无效错误

错误示例文本

$ aws dynamodb update-table \ --provisioned-throughput '{"ReadCapacityUnits":15,WriteCapacityUnits":10}' \ --table-name MyDDBTable Error parsing parameter '--provisioned-throughput': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 25 (char 24) JSON received: {"ReadCapacityUnits":15,WriteCapacityUnits":10}

使用 AWS CLI 命令时,您会收到 “Invalid JSON” 错误消息。当您输入具有预期的 JSON 格式的命令并且 AWS CLI 无法正确读取您的 JSON 时,通常会出现错误。

可能的原因:您没有输入有效的 JSON AWS CLI 供使用

确认您为命令输入了有效的 JSON。我们建议您对格式设置遇到问题的 JSON 使用 JSON 验证器。

要在命令行中使用更高级的 JSON,请考虑使用 jq 等命令行 JSON 处理器来创建 JSON 字符串。有关的更多信息jq,请参阅上的 jq 存储库GitHub

回到顶部

可能的原因:您的终端的报价规则导致无法将有效的 JSON 发送到 AWS CLI

在从命令 AWS CLI 接收任何内容之前,您的终端会使用自己的引用和转义规则处理该命令。由于终端的格式设置规则,您的某些 JSON 内容可能会在命令传递给 AWS CLI之前剥离掉。在构建命令时,请务必使用终端的引号规则

要进行故障排除,请使用 echo 命令来查看 Shell 如何处理您的参数:

$ echo {"ReadCapacityUnits":15,"WriteCapacityUnits":10} ReadCapacityUnits:15 WriteCapacityUnits:10
$ echo '{"ReadCapacityUnits":15,"WriteCapacityUnits":10}' {"ReadCapacityUnits":15,"WriteCapacityUnits":10}

修改您的命令,直到返回有效的 JSON。

要进行更深入的故障排除,请使用 --debug 参数来查看调试日志,因为它们将确切地显示传递给 AWS CLI的内容:

$ aws dynamodb update-table \ --provisioned-throughput '{"ReadCapacityUnits":15,WriteCapacityUnits":10}' \ --table-name MyDDBTable \ --debug 2022-07-19 22:25:07,741 - MainThread - awscli.clidriver - DEBUG - CLI version: aws-cli/1.18.147 Python/2.7.18 Linux/5.4.196-119.356.amzn2int.x86_64 botocore/1.18.6 2022-07-19 22:25:07,741 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['dynamodb', 'update-table', '--provisioned-throughput', '{"ReadCapacityUnits":15,WriteCapacityUnits":10}', '--table-name', 'MyDDBTable', '--debug']

使用终端的引号规则来修复 JSON 输入在发送到 AWS CLI时出现的任何问题。有关引号规则的更多信息,请参阅AWS CLI 中的引号及字符串

注意

如果您在获取有效 JSON 时遇到问题 AWS CLI,我们建议您使用 Blobs 将您的 JSON 数据直接传递给,从而绕过终端对 JSON 数据输入的报价规则。 AWS CLI有关 Blob 的更多信息,请参阅Blob

回到顶部

其他 资源

要获得 AWS CLI 有关问题的更多帮助,请访问AWS CLI 社区GitHubAWS re:Post 社区

回到顶部