

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

# 将 `AllocateHosts` 与 CLI 配合使用
<a name="example_ec2_AllocateHosts_section"></a>

以下代码示例演示如何使用 `AllocateHosts`。

------
#### [ CLI ]

**AWS CLI**  
**示例 1：分配专属主机**  
以下 `allocate-hosts` 示例在 `eu-west-1a` 可用区中分配一个专属主机，您可以在其上启动 `m5.large` 实例。默认情况下，专属主机仅接受目标实例启动，不支持主机恢复。  

```
aws ec2 allocate-hosts \
    --instance-type m5.large \
    --availability-zone eu-west-1a \
    --quantity 1
```
输出：  

```
{
    "HostIds": [
        "h-07879acf49EXAMPLE"
    ]
}
```
**示例 2：分配启用了自动置放和主机恢复的专属主机**  
以下 `allocate-hosts` 示例在启用了自动置放和主机恢复的 `eu-west-1a` 可用区中分配单个专属主机。  

```
aws ec2 allocate-hosts \
    --instance-type m5.large \
    --availability-zone eu-west-1a \
    --auto-placement on \
    --host-recovery on \
    --quantity 1
```
输出：  

```
{
     "HostIds": [
         "h-07879acf49EXAMPLE"
     ]
}
```
**示例 3：分配带有标签的专属主机**  
以下 `allocate-hosts` 示例分配单个专属主机，并应用具有名为 `purpose` 的键和值为 `production` 的标签。  

```
aws ec2 allocate-hosts \
    --instance-type m5.large \
    --availability-zone eu-west-1a \
    --quantity 1 \
    --tag-specifications 'ResourceType=dedicated-host,Tags={Key=purpose,Value=production}'
```
输出：  

```
{
    "HostIds": [
        "h-07879acf49EXAMPLE"
    ]
}
```
有关更多信息，请参阅《Amazon EC2 用户指南》**中的[分配专属主机](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/dedicated-hosts-allocating.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[AllocateHosts](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/allocate-hosts.html)*中的。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：此示例向您的账户分配给定实例类型和可用区的专属主机**  

```
New-EC2Host -AutoPlacement on -AvailabilityZone eu-west-1b -InstanceType m4.xlarge -Quantity 1
```
**输出**：  

```
h-01e23f4cd567890f3
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [AllocateHosts](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例向您的账户分配给定实例类型和可用区的专属主机**  

```
New-EC2Host -AutoPlacement on -AvailabilityZone eu-west-1b -InstanceType m4.xlarge -Quantity 1
```
**输出**：  

```
h-01e23f4cd567890f3
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [AllocateHosts](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------

有关 S AWS DK 开发者指南和代码示例的完整列表，请参阅[使用 AWS SDK 创建 Amazon EC2 资源](sdk-general-information-section.md)。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。