排查 Amazon CLI 错误 - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

排查 Amazon CLI 错误

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

首先尝试的一般故障排除

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

回到顶部

检查您的 Amazon CLI 命令格式

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

有关如何构造特定命令的更多信息,请参阅Amazon CLI《 版本 2 参考指南》

回到顶部

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

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

如何更新您的 Amazon CLI 版本取决于您最初安装它的方式,如安装或更新到最新版本的 Amazon CLI中所述。

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

回到顶部

使用 --debug 选项

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

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

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

  • 查找凭证

  • 解析提供的参数

  • 构建发送到 Amazon 服务器的请求

  • 发送到的请求的内容Amazon

  • 原始响应的内容

  • 带格式的输出

以下是使用和不使用 --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" } ] }

回到顶部

启用并审查 Amazon CLI 命令历史记录日志

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

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

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

  • 针对 botocore 的 API 调用

  • 状态代码

  • HTTP 响应

  • 标头

  • 返回代码

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

回到顶部

确认已配置 Amazon CLI

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

回到顶部

找不到命令错误

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

可能的原因:您正在尝试使用的 Amazon 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 ....

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

回到顶部

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

错误示例文本:

$ aws --version command not found: aws

如果第一次安装或更新 Amazon CLI 后找不到 aws 命令,您可能需要重新启动终端以便它识别任何 PATH 更新。

回到顶部

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

错误示例文本:

$ aws --version command not found: aws

如果第一次安装或更新 Amazon CLI 后找不到 aws 命令,它可能尚未完全安装。按照安装或更新到最新版本的 Amazon CLI中适用于您的平台的步骤尝试重新安装。

回到顶部

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

如果第一次在 Linux 安装或更新 aws 后找不到 Amazon CLI 命令,可能是没有安装文件夹的 execute 权限。使用 PATH 对您的 Amazon CLI 安装运行以下命令,以将 chmod 权限提供给 Amazon CLI:

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

回到顶部

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

错误示例文本:

$ aws --version command not found: aws

您可能需要将 aws 可执行文件添加到操作系统的 PATH 环境变量中。要将 Amazon 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 ...

    默认情况下,Amazon 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”命令返回的版本与您安装的版本不同

您的终端可能为 Amazon CLI 返回的 PATH 与您预期的不同。

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

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

回到顶部

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

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

回到顶部

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

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

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

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

注意

如果您在预安装了 Amazon CLI 版本 1 的情况下安装了 Amazon CLI 版本 2 之后发生这种情况,请遵循中的迁移说明操作。Amazon CLI 版本 2 迁移说明

回到顶部

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

当系统中的某个位置仍安装了 Amazon CLI 时,通常会发生这种情况。

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

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

回到顶部

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

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

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

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

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

    $ aws --version command not found: aws

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

    注意

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

回到顶部

Amazon CLI 处理了参数名称不完整的命令

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

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

以下 create-change-set 命令示例更改 CloudFormation 堆栈名称。参数 --change-set-n 被识别为 --change-set-name 的缩写,然后 Amazon 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)的缩写。因此,Amazon 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

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

回到顶部

访问被拒绝错误

可能的原因:Amazon 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.

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

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

如果您确定用户或角色具有策略所分配的适当权限,确保您的 Amazon CLI 命令使用的是您预期的凭证。请参阅下一节中有关凭证的内容以验证 Amazon 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.
可能的原因:Amazon CLI 从意外位置读取了不正确的凭证

Amazon CLI 读取凭证的位置可能与您的预期不同,或者密钥对信息不正确。您可以运行 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.

当 Amazon CLI 运行命令时,它会向 Amazon 服务器发送加密请求以执行适当的 Amazon 服务操作。您的凭证(访问密钥和私有密钥)参与了加密过程,使 Amazon 能够对发出请求的人员进行身份验证。有多种因素可能会干扰此过程的正常执行,如下所示。

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

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

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

$ date

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

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

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

回到顶部

可能的原因:操作系统错误地处理了包含某些特殊字符的 Amazon 密钥

如果 Amazon 密钥包含某些特殊字符(例如 -+/%),则某些操作系统变体会不正确地处理该字符串,并导致对该密钥字符串的解释不正确。

如果使用其他工具或脚本(例如,在创建新实例期间在其上构建凭证文件的工具)处理密钥,则这些工具和脚本可能有自己对特殊字符的处理,这会导致将这些字符转换为 Amazon 不再识别的内容。

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

回到顶部

SSL 证书错误

可能的原因:Amazon CLI 不信任您的代理的证书

错误示例文本:

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

当您使用 Amazon CLI 命令时,您收到 [SSL: CERTIFICATE_ VERIFY_FAILED] certificate verify failed 错误消息。这是因为 Amazon CLI 不信任代理的证书,原因包括代理的证书是自签名的,而您的公司却设置为证书颁发机构 (CA)。这会致使 Amazon CLI 无法在本地 CA 注册机构中找到您的公司 CA 根证书。

要解决这个问题,请指示 Amazon CLI 使用 ca_bundle 配置文件设置、--ca-bundle 命令行选项或 AWS_CA_BUNDLE 环境变量在何处查找公司 .pem 文件。

回到顶部

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

错误示例文本:

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

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

回到顶部

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}

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

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

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

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

回到顶部

可能的原因:您的终端的引号规则阻止将有效的 JSON 发送到 Amazon CLI

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

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

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

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

要进行更深入的故障排除,请使用 --debug 参数来查看调试日志,因为它们将确切地显示传递给 Amazon 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 输入在发送到 Amazon CLI 时出现的任何问题。有关引号规则的更多信息,请参阅Amazon CLI 中的引号及字符串

注意

如果您在将有效的 JSON 发送到 Amazon CLI 时遇到问题,我们建议您使用 BLOB 将 JSON 数据直接发送到 Amazon CLI,从而绕过终端对 JSON 数据输入的引号规则。有关 Blob 的更多信息,请参阅Blob

回到顶部

其他资源

有关 Amazon CLI 问题的其他帮助,请访问 GitHub 上的 Amazon CLI 社区,或者访问 Amazon Web Services re:Post 社区

回到顶部