

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

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

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

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

**AWS CLI**  
**请求子网中价格最低的竞价型实例集**  
此示例命令创建一个竞价型实例集请求，其中包含两个仅因子网而异的启动规范。竞价型实例集启动指定子网中价格最低的实例。如果在默认 VPC 中启动实例，则实例在默认情况下会收到一个公有 IP 地址。如果在非默认 VPC 中启动实例，则默认情况下它们不会收到一个公有 IP 地址。  
请注意，您无法在竞价型实例集请求中指定来自相同可用区的不同子网。  
命令:  

```
aws ec2 request-spot-fleet --spot-fleet-request-config file://config.json
```
Config.json：  

```
{
  "SpotPrice": "0.04",
  "TargetCapacity": 2,
  "IamFleetRole": "arn:aws:iam::123456789012:role/my-spot-fleet-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "KeyName": "my-key-pair",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "m3.medium",
          "SubnetId": "subnet-1a2b3c4d, subnet-3c4d5e6f",
          "IamInstanceProfile": {
              "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role"
          }
      }
  ]
}
```
输出：  

```
{
  "SpotFleetRequestId": "sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLE"
}
```
**请求可用区中价格最低的竞价型实例集**  
此示例命令创建一个竞价型实例集请求，其中包含两个仅因可用区而异的启动规范。竞价型实例集启动指定可用区中价格最低的实例。如果您的账户仅支持 EC2-VPC，则 Amazon EC2 会在可用区的默认子网中启动竞价型实例。如果您的账户支持 EC2-Classic，则 Amazon EC2 会在可用区中的 EC2-Classic 中启动实例。  
命令:  

```
aws ec2 request-spot-fleet --spot-fleet-request-config file://config.json
```
Config.json：  

```
{
  "SpotPrice": "0.04",
  "TargetCapacity": 2,
  "IamFleetRole": "arn:aws:iam::123456789012:role/my-spot-fleet-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "KeyName": "my-key-pair",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "m3.medium",
          "Placement": {
              "AvailabilityZone": "us-west-2a, us-west-2b"
          },
          "IamInstanceProfile": {
              "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role"
          }
      }
  ]
}
```
**在子网中启动竞价型实例并为其分配公有 IP 地址**  
此示例命令将公有地址分配给在非默认 VPC 中启动的实例。请注意，指定网络接口时，您必须包括使用网络接口的子网 ID 和安全组 ID。  
命令:  

```
aws ec2 request-spot-fleet --spot-fleet-request-config file://config.json
```
Config.json：  

```
{
  "SpotPrice": "0.04",
  "TargetCapacity": 2,
  "IamFleetRole": "arn:aws:iam::123456789012:role/my-spot-fleet-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "KeyName": "my-key-pair",
          "InstanceType": "m3.medium",
          "NetworkInterfaces": [
              {
                  "DeviceIndex": 0,
                  "SubnetId": "subnet-1a2b3c4d",
                  "Groups": [ "sg-1a2b3c4d" ],
                  "AssociatePublicIpAddress": true
              }
          ],
          "IamInstanceProfile": {
              "Arn": "arn:aws:iam::880185128111:instance-profile/my-iam-role"
          }
      }
  ]
}
```
**使用多样化分配策略请求竞价型实例集**  
此示例命令创建一个竞价型实例集请求，该请求使用多样化分配策略启动 30 个实例。启动规范因实例类型而异。竞价型实例集在启动规范间分配实例，以便每种类型有 10 个实例。  
命令:  

```
aws ec2 request-spot-fleet --spot-fleet-request-config file://config.json
```
Config.json：  

```
{
  "SpotPrice": "0.70",
  "TargetCapacity": 30,
  "AllocationStrategy": "diversified",
  "IamFleetRole": "arn:aws:iam::123456789012:role/my-spot-fleet-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c4.2xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "m3.2xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "r3.2xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      }
  ]
}
```
有关更多信息，请参阅《Amazon Elastic Compute Cloud 用户指南》**中的“竞价型实例集请求”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[RequestSpotFleet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/request-spot-fleet.html)*中的。

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

**适用于 PowerShell V4 的工具**  
**示例 1：此示例在价格最低的可用区中为指定的实例类型创建竞价型实例集请求。如果您的账户仅支持 EC2-VPC，则竞价型实例集在具有默认子网的价格最低的可用区中启动实例。如果您的账户支持 EC2-Classic，则竞价型实例集在价格最低的可用区的 EC2-Classic 中启动实例。请注意，您支付的价格不会超过请求的指定竞价价格。**  

```
$sg = New-Object Amazon.EC2.Model.GroupIdentifier
$sg.GroupId = "sg-12345678"
$lc = New-Object Amazon.EC2.Model.SpotFleetLaunchSpecification
$lc.ImageId = "ami-12345678"
$lc.InstanceType = "m3.medium"
$lc.SecurityGroups.Add($sg) 
Request-EC2SpotFleet -SpotFleetRequestConfig_SpotPrice 0.04 `
-SpotFleetRequestConfig_TargetCapacity 2 `
-SpotFleetRequestConfig_IamFleetRole arn:aws:iam::123456789012:role/my-spot-fleet-role `
-SpotFleetRequestConfig_LaunchSpecification $lc
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [RequestSpotFleet](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例在价格最低的可用区中为指定的实例类型创建竞价型实例集请求。如果您的账户仅支持 EC2-VPC，则竞价型实例集在具有默认子网的价格最低的可用区中启动实例。如果您的账户支持 EC2-Classic，则竞价型实例集在价格最低的可用区的 EC2-Classic 中启动实例。请注意，您支付的价格不会超过请求的指定竞价价格。**  

```
$sg = New-Object Amazon.EC2.Model.GroupIdentifier
$sg.GroupId = "sg-12345678"
$lc = New-Object Amazon.EC2.Model.SpotFleetLaunchSpecification
$lc.ImageId = "ami-12345678"
$lc.InstanceType = "m3.medium"
$lc.SecurityGroups.Add($sg) 
Request-EC2SpotFleet -SpotFleetRequestConfig_SpotPrice 0.04 `
-SpotFleetRequestConfig_TargetCapacity 2 `
-SpotFleetRequestConfig_IamFleetRole arn:aws:iam::123456789012:role/my-spot-fleet-role `
-SpotFleetRequestConfig_LaunchSpecification $lc
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [RequestSpotFleet](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------

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