

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 CLI 組態 Spot 機群
<a name="spot-fleet-examples"></a>

您可以在 JSON 檔案中定義 Spot 機群組態，然後使用 [request-spot-fleet](https://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-fleet.html) AWS CLI 命令參考該檔案來建立機群，如下所示：

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

下列範例說明各種 Spot 機群使用案例的啟動組態。如需組態參數的詳細資訊，請參閱 [request-spot-fleet](https://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-fleet.html)。如需有關建立 Spot 機群的詳細資訊，請參閱 [建立 Spot 機群](create-spot-fleet.md)。

**注意**  
對於 Spot 機群，您無法在啟動範本或啟動規格中指定網路介面 ID。確保省略啟動範本或啟動規格中的 `NetworkInterfaceID` 參數。

**Topics**
+ [範例 1：使用區域中最低價格的可用區域來啟動 Spot 執行個體](#fleet-config1)
+ [範例 2：使用清單中最低價格的可用區域來啟動 Spot 執行個體](#fleet-config2)
+ [範例 3：使用清單中最低價格的執行個體類型來啟動 Spot 執行個體](#fleet-config3)
+ [範例 4。覆寫請求的價格](#fleet-config4)
+ [範例 5：使用多樣化分配策略來啟動 Spot 機群](#fleet-config5)
+ [範例 6：使用執行個體權重來啟動 Spot 機群](#fleet-config6)
+ [範例 7：使用隨需容量來啟動 Spot 機群](#fleet-config7)
+ [範例 8：設定容量重新平衡，以啟動取代 Spot 執行個體](#fleet-config8)
+ [範例 9：在容量最佳化機群中啟動 Spot 執行個體](#fleet-config9)
+ [範例 10：使用優先順序在容量最佳化機群中啟動 Spot 執行個體](#fleet-config10)
+ [範例 11：在 priceCapacityOptimized 機群中啟動 Spot 執行個體](#fleet-config11)
+ [範例 12：設定屬性型執行個體類型選取](#fleet-config12)

## 範例 1：使用區域中最低價格的可用區域來啟動 Spot 執行個體
<a name="fleet-config1"></a>

下列的範例指定了單一啟動規格，其中未包含可用區域或子網路。Spot 機群會在具有預設子網路的最低價格可用區域中，啟動執行個體。您所支付的價格不會超過隨需價格。

```
{
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "KeyName": "my-key-pair",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "m3.medium",
          "IamInstanceProfile": {
              "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role"
          }
      }
  ]
}
```

## 範例 2：使用清單中最低價格的可用區域來啟動 Spot 執行個體
<a name="fleet-config2"></a>

下列的範例指定了兩種啟動規格，各包含不同的可用區域或子網路，但具有相同的執行個體類型和 AMI。

**可用區域**

Spot 機群會在您所指定最低價格可用區域的預設子網路中，啟動執行個體。

```
{
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-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"
          }
      }
  ]
}
```

**子網路**

您可以指定預設子網路或非預設子網路，而非預設子網路可以是來自預設 VPC 或非預設 VPC。Spot 服務會在最低價格可用區域的任何可用子網路中，啟動執行個體。

您不能在 Spot 機群請求中指定來自同一個可用區域的不同子網路。

```
{
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "KeyName": "my-key-pair",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "m3.medium",
          "SubnetId": "subnet-a61dafcf, subnet-65ea5f08",
          "IamInstanceProfile": {
              "Arn": "arn:aws:iam::123456789012:instance-profile/my-iam-role"
          }
      }
  ]
}
```

如果執行個體是在預設的 VPC 中啟動，則其預設會接收公有 IPv4 位址。如果執行個體是在非預設的 VPC 中啟動，則其預設不會接收公有 IPv4 位址。在預設規格中啟動網路介面，來指派公有 IPv4 位址給在非預設 VPC 中啟動的執行個體。指定網路介面時，必須加入使用網路介面的子網路 ID 和安全群組 ID。

```
  ...       
      {
          "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::123456789012:instance-profile/my-iam-role"
          }
      }
  ...
```

## 範例 3：使用清單中最低價格的執行個體類型來啟動 Spot 執行個體
<a name="fleet-config3"></a>

下列的範例指定了兩種啟動組態，各具有不同的執行個體類型，但包含相同的 AMI 和可用區域或子網路。Spot 機群會使用具有最低價格的指定執行個體類型，來啟動執行個體。

**可用區域**

```
{
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "c5.4xlarge",
          "Placement": {
            "AvailabilityZone": "us-west-2b"
          }
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "r3.8xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      }
  ]
}
```

**子網路**

```
{
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "c5.4xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "SecurityGroups": [
              {
                  "GroupId": "sg-1a2b3c4d"
              }
          ],
          "InstanceType": "r3.8xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      }
  ]
}
```

## 範例 4。覆寫請求的價格
<a name="fleet-config4"></a>

我們建議您使用預設的最高價格，也就是隨需價格。如果想要的話，您可以指定機群請求的最高價格，和個別啟動規格的最高價格。

下列的範例指定了機群請求的最高價格，和三種啟動規格中其中兩種的最高價格。對於未指定最高價格的任何啟動規格，則會使用機群請求的最高價格。Spot 機群會使用具有最低價格的執行個體類型，來啟動執行個體。

**可用區域**

```
{
  "SpotPrice": "1.00",
  "TargetCapacity": 30,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          },
          "SpotPrice": "0.10"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.4xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          },
          "SpotPrice": "0.20"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.8xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      }
    ]
}
```

**子網路**

```
{
  "SpotPrice": "1.00",
  "TargetCapacity": 30,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.2xlarge",
          "SubnetId": "subnet-1a2b3c4d",
          "SpotPrice": "0.10"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.4xlarge",
          "SubnetId": "subnet-1a2b3c4d",
          "SpotPrice": "0.20"
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.8xlarge",
          "SubnetId": "subnet-1a2b3c4d"
      }
  ]
}
```

## 範例 5：使用多樣化分配策略來啟動 Spot 機群
<a name="fleet-config5"></a>

下列範例使用 `diversified` 分配策略。這些啟動規格各具有不同的執行個體類型，但包含相同的 AMI 和可用區域或子網路。Spot 機群會將 30 個執行個體分配給 3 種啟動規格，如此每種規格就有 10 個執行個體。如需詳細資訊，請參閱[使用配置策略來判斷 EC2 機群或 Spot 機群如何達成 Spot 和隨需容量](ec2-fleet-allocation-strategy.md)。

**可用區域**

```
{
  "SpotPrice": "0.70", 
  "TargetCapacity": 30,
  "AllocationStrategy": "diversified",
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c4.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "m3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "r3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      }
  ]
}
```

**子網路**

```
{
    "SpotPrice": "0.70", 
    "TargetCapacity": 30,
    "AllocationStrategy": "diversified",
    "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-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"
        }
    ]
}
```

最佳實務為增加 EC2 容量在其中一個可用區域停機時可以履行 Spot 請求的機會。此最佳做法可以讓區域之間多元化。針對此案例，請將可供您使用的各可用區域納入啟動規格之中。不要每次都使用相同的子網路，而是使用三個唯一的子網路 (各個都映射至不同的區域)。

**可用區域**

```
{
  "SpotPrice": "0.70", 
  "TargetCapacity": 30,
  "AllocationStrategy": "diversified",
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c4.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2a"
          }
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "m3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          }
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "r3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2c"
          }
      }
  ]
}
```

**子網路**

```
{
    "SpotPrice": "0.70", 
    "TargetCapacity": 30,
    "AllocationStrategy": "diversified",
    "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
    "LaunchSpecifications": [
        {
            "ImageId": "ami-1a2b3c4d",
            "InstanceType": "c4.2xlarge",
            "SubnetId": "subnet-1a2b3c4d"
        },
        {
            "ImageId": "ami-1a2b3c4d",
            "InstanceType": "m3.2xlarge",
            "SubnetId": "subnet-2a2b3c4d"
        },
        {
            "ImageId": "ami-1a2b3c4d",
            "InstanceType": "r3.2xlarge",
            "SubnetId": "subnet-3a2b3c4d"
        }
    ]
}
```

## 範例 6：使用執行個體權重來啟動 Spot 機群
<a name="fleet-config6"></a>

下列範例使用執行個體權重，這表示價格為每單位小時而非每執行個體小時的價格。每個啟動組態都列出了不同的執行個體類型和不同的權重。Spot 機群會使用具有最低每單位小時價格的執行個體類型。Spot 機群會將目標容量除以執行個體的權重，來計算出要啟動的 Spot 機群數量。如果結果不是整數，則 Spot 機群會將其捨入到下一個整數，以便您的機群大小不低於其目標容量。

如果 `r3.2xlarge` 的請求成功，Spot 會佈建 4 個這類執行個體。將 20 除以 6，會得出總共 3.33 個執行個體，接著再四捨五入為 4 個執行個體。

如果 `c3.xlarge` 的請求成功，Spot 會佈建 7 個這類執行個體。將 20 除以 3，會得出總共 6.66 個執行個體，接著再四捨五入為 7 個執行個體。

如需詳細資訊，請參閱[使用執行個體權重來管理 EC2 機群或 Spot 機群的成本和效能](ec2-fleet-instance-weighting.md)。

**可用區域**

```
{
  "SpotPrice": "0.70",
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "r3.2xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          },
          "WeightedCapacity": 6
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.xlarge",
          "Placement": {
              "AvailabilityZone": "us-west-2b"
          },
          "WeightedCapacity": 3
      }
    ]
}
```

**子網路**

```
{
  "SpotPrice": "0.70",
  "TargetCapacity": 20,
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "LaunchSpecifications": [
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "r3.2xlarge",
          "SubnetId": "subnet-1a2b3c4d",
          "WeightedCapacity": 6
      },
      {
          "ImageId": "ami-1a2b3c4d",
          "InstanceType": "c3.xlarge",
          "SubnetId": "subnet-1a2b3c4d",
          "WeightedCapacity": 3
      }
  ]
}
```

## 範例 7：使用隨需容量來啟動 Spot 機群
<a name="fleet-config7"></a>

為確保永遠有執行個體容量，您可以在 Spot 機群請求中加入對隨需容量的請求。如果有容量，則隨需請求一律已履行。如有容量和可用性，則目標容量的餘額已履行做為 Spot 使用。

下列範例將所要的目標容量指定為 10，其中 5 個必須為隨需容量。未指定 Spot 容量；它暗含在目標容量減去隨需容量的餘額中。如果有可用的 Amazon EC2 容量和可用性，Amazon EC2 會啟動 5 個容量單位作為隨需容量，5 個容量單位 (10-5=5) 作為 Spot 容量。

```
{
  "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
  "AllocationStrategy": "lowestPrice",
  "TargetCapacity": 10,
  "SpotPrice": null,
  "ValidFrom": "2018-04-04T15:58:13Z",
  "ValidUntil": "2019-04-04T15:58:13Z",
  "TerminateInstancesWithExpiration": true,
  "LaunchSpecifications": [],
  "Type": "maintain",
  "OnDemandTargetCapacity": 5,
  "LaunchTemplateConfigs": [
    {
      "LaunchTemplateSpecification": {
        "LaunchTemplateId": "lt-0dbb04d4a6cca5ad1",
        "Version": "2"
      },
      "Overrides": [
        {
          "InstanceType": "t2.medium",
          "WeightedCapacity": 1,
          "SubnetId": "subnet-d0dc51fb"
        }
      ]
    }
  ]
}
```

## 範例 8：設定容量重新平衡，以啟動取代 Spot 執行個體
<a name="fleet-config8"></a>

下列範例會設定 Spot 機群，以便 Amazon EC2 在機群中對 Spot 執行個體發出重新平衡建議時啟動替代 Spot 執行個體。若要為 `ReplacementStrategy` 設定 Spot 執行個體的自動替代，請指定 `launch-before-terminate`。若要設定從啟動新替代 Spot 執行個體到自動刪除舊 Spot 執行個體的時間延遲，請針對 `termination-delay`，指定一值 (以秒為單位)。如需詳細資訊，請參閱[組態選項](ec2-fleet-capacity-rebalance.md#ec2-fleet-capacity-rebalance-config-options)。

**注意**  
建議僅在您可以預測執行個體關閉程序將需要多長時間才能完成時，才使用 `launch-before-terminate`。這可確保只在關機程序完成之後，才會終止舊執行個體。所有執行個體在執行時，您需要支付所有執行個體的費用。

容量重新平衡策略的有效性取決於 Spot 機群請求中指定的 Spot 容量集區數量。我們建議您使用多樣化的執行個體類型和可用區域的集合來設定機群，若要 `AllocationStrategy`，請指定 `capacityOptimized`。如需當設定 Spot 機群進行容量重新平衡時應考量哪些項目的詳細資訊，請參閱 [在 EC2 機群和 Spot 機群中使用容量重新平衡，來取代具有風險的 Spot 執行個體](ec2-fleet-capacity-rebalance.md)。

```
{
    "SpotFleetRequestConfig": {
        "AllocationStrategy": "capacityOptimized",
        "IamFleetRole": "arn:aws:iam::123456789012:role/aws-ec2-spot-fleet-tagging-role",
        "LaunchTemplateConfigs": [
            {
                "LaunchTemplateSpecification": {
                    "LaunchTemplateName": "LaunchTemplate",
                    "Version": "1"
                },
                 "Overrides": [
                       {
                           "InstanceType": "c3.large",
                           "WeightedCapacity": 1,
                           "Placement": {
                               "AvailabilityZone": "us-east-1a"
                           }
                       },
                       {
                           "InstanceType": "c4.large",
                           "WeightedCapacity": 1,
                           "Placement": {
                               "AvailabilityZone": "us-east-1a"
                           }
                       },
                       {
                           "InstanceType": "c5.large",
                           "WeightedCapacity": 1,
                           "Placement": {
                               "AvailabilityZone": "us-east-1a"
                           }
                       }
                ] 
          }
    ],
        "TargetCapacity": 5,
        "SpotMaintenanceStrategies": {
            "CapacityRebalance": {
                "ReplacementStrategy": "launch-before-terminate",
                "TerminationDelay": "720"
            }
        }
    }
}
```

## 範例 9：在容量最佳化機群中啟動 Spot 執行個體
<a name="fleet-config9"></a>

下列範例示範如何使用可最佳化容量的 Spot 分配策略來設定 Spot 機群。若要最佳化容量，您必須將 `AllocationStrategy` 設為 `capacityOptimized`。

在下列範例中，三個啟動規格指定三個 Spot 容量集區。目標容量為 50 個 Spot 執行個體。Spot 機群嘗試將 50 個 Spot 執行個體啟動到 Spot 容量集區中，且擁有啟動中執行個體數目之最佳容量。

```
{
    "TargetCapacity": "50",
    "SpotFleetRequestConfig": {
        "AllocationStrategy": "capacityOptimized",
    },
    "LaunchTemplateConfigs": [
        {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "1"
            },
            "Overrides": [
                {
                    "InstanceType": "r4.2xlarge",  
                    "AvailabilityZone": "us-west-2a"
                },
                {
                    "InstanceType": "m4.2xlarge",
                    "AvailabilityZone": "us-west-2b"
                }, 
                {
                    "InstanceType": "c5.2xlarge",
                    "AvailabilityZone": "us-west-2b"
                }
            ] 
        }
    ]
}
```

## 範例 10：使用優先順序在容量最佳化機群中啟動 Spot 執行個體
<a name="fleet-config10"></a>

下列範例示範在盡最大努力使用優先順序時，如何使用可最佳化容量的 Spot 分配策略來設定 Spot 機群。

當您使用 `capacityOptimizedPrioritized` 分配策略時，您可以使用 `Priority` 參數來指定 Spot 容量集區的優先順序，其中數字越小，優先順序越高。如果您對它們一視同仁，也可以為數個 Spot 容量集區設定相同的優先順序。如果您沒有設定集區的優先順序，集區將被視為最後一個優先順序。

若要排定 Spot 容量集區的優先順序，您必須將 `AllocationStrategy` 設為 `capacityOptimizedPrioritized`。Spot 機群會先針對容量進行最佳化，但會盡力遵守優先順序 (例如，如果遵守優先順序不會顯著影響 Spot 機群佈建最佳容量的能力)。對於必須將中斷可能性降至最低的工作負載來說，這是一個很好的選擇，而且某些執行個體類型的偏好也很重要。

在下列範例中，三個啟動規格指定三個 Spot 容量集區。每個集區都有優先順序，其中數字越小，優先順序越高。目標容量為 50 個 Spot 執行個體。Spot 機群嘗試以最高優先順序在 Spot 容量集區中啟動 50 個 Spot 執行個體，但首先針對容量進行最佳化。

```
{
    "TargetCapacity": "50",
    "SpotFleetRequestConfig": {
        "AllocationStrategy": "capacityOptimizedPrioritized"
    },
    "LaunchTemplateConfigs": [
        {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "1"
            },
            "Overrides": [
                {
                    "InstanceType": "r4.2xlarge",    
                   "Priority": 1,
                    "AvailabilityZone": "us-west-2a"
                },
                {
                           "InstanceType": "m4.2xlarge",
                           "Priority": 2,
                           "AvailabilityZone": "us-west-2b"
                }, 
                {
                           "InstanceType": "c5.2xlarge",
                           "Priority": 3,
                           "AvailabilityZone": "us-west-2b"
                }
            ] 
        }
    ]
}
```

## 範例 11：在 priceCapacityOptimized 機群中啟動 Spot 執行個體
<a name="fleet-config11"></a>

下列範例示範如何使用可最佳化容量且價格最低的 Spot 分配策略來設定 Spot 機群。若要在將價格納入考慮的同時最佳化容量，您必須將 Spot `AllocationStrategy` 設定為 `priceCapacityOptimized`。

在下列範例中，三個啟動規格指定三個 Spot 容量集區。目標容量為 50 個 Spot 執行個體。Spot 機群嘗試將 50 個 Spot 執行個體啟動到 Spot 容量集區中，且擁有啟動中執行個體數目之最佳容量，同時還會選擇價格最低的集區。

```
{
    "SpotFleetRequestConfig": {
        "AllocationStrategy": "priceCapacityOptimized",
        "OnDemandAllocationStrategy": "lowestPrice",
        "ExcessCapacityTerminationPolicy": "default",
        "IamFleetRole": "arn:aws:iam::111111111111:role/aws-ec2-spot-fleet-tagging-role",
        "LaunchTemplateConfigs": [
            {
                "LaunchTemplateSpecification": {
                    "LaunchTemplateId": "lt-0123456789example",
                    "Version": "1"
                },
                "Overrides": [
                     {
                           "InstanceType": "r4.2xlarge",  
                           "AvailabilityZone": "us-west-2a"
                      },
                      {
                           "InstanceType": "m4.2xlarge",
                           "AvailabilityZone": "us-west-2b"
                      }, 
                      {
                           "InstanceType": "c5.2xlarge",
                           "AvailabilityZone": "us-west-2b"
                      }
                ]
            }
        ],
        "TargetCapacity": 50,
        "Type": "request"
    }
}
```

## 範例 12：設定屬性型執行個體類型選取
<a name="fleet-config12"></a>

下列範例示範如何將 Spot 機群設定為使用屬性型執行個體類型選取來識別執行個體類型。若要指定必要的執行個體屬性，您可以在 `InstanceRequirements` 結構中指定屬性。

在下列範例中，指定了兩個執行個體屬性：
+ `VCpuCount` – 至少指定 2 個 vCPU。由於未指定最大值，因此沒有最大限制。
+ `MemoryMiB` – 至少指定 4 MiB 的記憶體。由於未指定最大值，因此沒有最大限制。

將會識別任何具有 2 個或更多 vCPU 和 4 MiB 或更多記憶體的執行個體類型。但是，當 [Spot 機群佈建機群](ec2-fleet-attribute-based-instance-type-selection.md#how-ef-uses-abs)時，價格保護和分配策略可能會排除某些執行個體類型。

如需您可指定之所有可能屬性的清單和說明，請參閱 *Amazon EC2 API 參考*中的[InstanceRequirements](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_InstanceRequirements.html)。

```
{
	"AllocationStrategy": "priceCapacityOptimized",
	"TargetCapacity": 20,
	"Type": "request",
	"LaunchTemplateConfigs": [{
		"LaunchTemplateSpecification": {
			"LaunchTemplateName": "my-launch-template",
			"Version": "1"
		},
		"Overrides": [{
			"InstanceRequirements": {
				"VCpuCount": {
					"Min": 2
				},
				"MemoryMiB": {
					"Min": 4
				}
			}
		}]
	}]
}
```