

# AWS CLI 中的命令结构
<a name="cli-usage-commandstructure"></a>

本主题介绍如何构建 AWS Command Line Interface（AWS CLI）命令，以及如何使用 wait 命令。

**Topics**
+ [命令结构](#cli-usage-commandstructure-structure.title)
+ [Wait 命令](#cli-usage-commandstructure-wait)

## 命令结构
<a name="cli-usage-commandstructure-structure.title"></a>

AWS CLI 在命令行上使用多部分结构，各部分必须按如下顺序指定：

1. 对 `aws` 计划的基本调用。

1. 顶级*命令*，这通常对应于 AWS CLI 支持的AWS服务。

1. 用于指定要执行的操作的*子命令*。

1. 常规 AWS CLI 选项或操作所需的参数。您可以按任意顺序指定这些项，只要它们跟在前三个部分之后。如果多次指定某个排他参数，则仅应用*最后一个值*。

```
$ aws <command> <subcommand> [options and parameters]
```

参数可采用各种类型的输入值，如数字、字符串、列表、映射和 JSON 结构。支持的内容取决于您指定的命令和子命令。

### 示例
<a name="cli-usage-commandstructure-structure-example"></a>

**Amazon S3**

以下示例列出您的所有 Amazon S3 存储桶。

```
$ aws s3 ls
2018-12-11 17:08:50 amzn-s3-demo-bucket1
2018-12-14 14:55:44 amzn-s3-demo-bucket2
```

有关 Amazon S3 命令的更多信息，请参阅 *AWS CLI 命令参考* 中的 [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)。

**AWS CloudFormation**

以下 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-change-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-change-set.html) 命令示例将 cloudformation 堆栈名称更改为 *my-change-set*。

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

有关 AWS CloudFormation 命令的更多信息，请参阅 *AWS CLI 命令参考* 中的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/index.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/index.html)。

## Wait 命令
<a name="cli-usage-commandstructure-wait"></a>

一些 AWS 服务提供 `wait` 命令。使用 `aws wait` 的任何命令通常都会等到命令完成后再进入下一步。这对于多部分命令或脚本编写来说特别有用，因为当命令失败时，您可以使用 wait 命令阻止进入后续步骤。

AWS CLI 在命令行上对 `wait` 命令使用多部分结构，各部分必须按如下顺序指定：

1. 对 `aws` 计划的基本调用。

1. 顶级*命令*，这通常对应于 AWS CLI 支持的AWS服务。

1. `wait` 命令。

1. 用于指定要执行的操作的*子命令*。

1. 常规 CLI 选项或操作所需的参数。您可以按任意顺序指定这些项，只要它们跟在前三个部分之后。如果多次指定某个排他参数，则仅应用*最后一个值*。

```
$ aws <command> wait <subcommand> [options and parameters]
```

参数可采用各种类型的输入值，如数字、字符串、列表、映射和 JSON 结构。支持的内容取决于您指定的命令和子命令。

**注意**  
并不是所有 AWS 服务都支持 `wait` 命令。请参阅 [AWS CLI 版本 2 参考指南](https://docs.aws.amazon.com/cli/latest/reference/index.html)，了解您的服务是否支持 `wait` 命令。

### 示例
<a name="cli-usage-commandstructure-wait-example"></a>

**AWS CloudFormation**

在下面的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/change-set-create-complete.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/change-set-create-complete.html) 命令示例中，命令将暂停运行，并且仅在可确认 *my-stack* 堆栈中的 *my-change-set* 更改集已准备好运行后才恢复运行。

```
$ aws cloudformation wait change-set-create-complete --stack-name my-stack --change-set-name my-change-set
```

有关 AWS CloudFormation `wait` 命令的更多信息，请参阅**《AWS CLI 命令参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/index.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/index.html)。

**AWS CodeDeploy**

在下面的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/change-set-create-complete.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/change-set-create-complete.html) 命令示例中，命令将暂停运行，直到 *d-A1B2C3111* 部署成功完成。

```
$ aws deploy wait deployment-successful --deployment-id d-A1B2C3111
```

有关 AWS CodeDeploy `wait` 命令的更多信息，请参阅**《AWS CLI 命令参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/index.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/index.html)。