

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

# 手動選擇執行個體類型以建立混合執行個體群組
<a name="create-mixed-instances-group-manual-instance-type-selection"></a>

本主題示範如何透過手動選擇執行個體類型在單一 Auto Scaling 群組中啟動多個執行個體類型。

若您偏好以執行個體屬性作為選取執行個體類型的條件，請參閱 [使用屬性型執行個體類型選取項目建立混合執行個體群組](create-mixed-instances-group-attribute-based-instance-type-selection.md)。

**Topics**
+ [先決條件](#manual-instance-type-selection-prerequisites)
+ [建立混合執行個體群組 (主控台)](#manual-instance-type-selection-console)
+ [建立混合執行個體群組 (AWS CLI)](#manual-instance-type-selection-aws-cli)
+ [範例組態](#manual-instance-type-selection-example-configurations)

## 先決條件
<a name="manual-instance-type-selection-prerequisites"></a>
+ 建立啟動範本。如需詳細資訊，請參閱[為 Auto Scaling 群組建立啟動範本](create-launch-template.md)。
+ 驗證啟動範本是否尚未請求 Spot 執行個體。

## 建立混合執行個體群組 (主控台)
<a name="manual-instance-type-selection-console"></a>

請使用下列程序，透過手動選擇您的群組可以啟動的執行個體類型來建立混合執行個體群組。為了協助您有效率地完成這些步驟，我們會略過某些選用的區段。

若要檢閱混合執行個體群組的最佳實務，請參閱 [建立混合執行個體群組的設定概觀](mixed-instances-groups-set-up-overview.md)。

**建立混合執行個體群組**

1. 前往網址 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台，然後從導覽窗格中選擇 **Auto Scaling 群組**。

1. 在畫面上方的導覽列中，選擇您用來建立啟動範本的相同 AWS 區域 。

1. 選擇**建立 Auto Scaling 群組**。

1. 在**選擇啟動範本或組態**頁面上，針對**Auto Scaling 群組名稱**，請輸入 Auto Scaling 群組的名稱。

1. 若要選擇您的啟動範本，請執行以下操作：

   1. 針對**啟動範本**，選擇現有的啟動範本。

   1. 針對**啟動範本版本**，選擇 Auto Scaling 群組擴增時使用啟動範本的預設、最新或特定版本。

   1. 確認您的啟動範本支援您打算使用的所有選項，然後選擇**下一步**。

1. 在**選擇執行個體啟動選項**頁面中，執行以下操作：

   1. 對於 **Instance type requirements** (執行個體類型需求)，選擇 **Override launch template** (覆寫啟動範本)，然後選擇 **Manually add instance types** (手動新增執行個體類型)。

   1. 選擇您的執行個體類型。您可以使用我們的建議作為起點。依預設選取**系列和層代靈活**。
      + 若要變更執行個體類型的順序，請使用箭頭。如果您選擇支援優先順序的分配策略，則執行個體類型順序會設定其啟動優先順序。
      + 若要移除執行個體類型，請選擇 **X**。
      + (選用) 對於**權重**欄中的方塊，為每個執行個體類型指定相對權重。若要這麼做，請輸入該類型的執行個體計入群組所需容量的單位數。如果執行個體類型提供不同的 vCPU、記憶體、儲存或網路頻寬功能，則這麼做可能會很實用。如需詳細資訊，請參閱[設定 Auto Scaling 群組以使用執行個體權重](ec2-auto-scaling-mixed-instances-groups-instance-weighting.md)。

        注意，如果您選擇使用**大小靈活**建議，則屬於本部分的所有執行個體類型都會自動具有權重值。如果您不想指定任何權重，則請清除所有執行個體類型的 **Weight** (權重) 欄中的方塊。

   1. 在 **Instance purchase options** (執行個體購買選項) 下，針對**Instances distribution** (執行個體分佈)，分別指定群組要以隨需執行個體和 Spot 執行個體啟動的百分比。如果您的應用程式無狀態、可容錯並且可以處理被中斷的執行個體，則您可以指定較高的 Spot 執行個體百分比。

   1. (選用) 當您指定 Spot 執行個體的百分比時，您可以勾選**包含隨需基本容量**旁的核取方塊，然後指定 Auto Scaling 群組初始容量中應以隨需執行個體履行的數量下限。超過基本容量的項目會使用**執行個體分佈**設定來決定啟動多少隨需執行個體和 Spot 執行個體。

   1. 在 **Allocation strategies** (分配策略) 下，對於 **On-Demand allocation strategy** (隨需分配策略)，請選擇分配策略。手動選擇執行個體類型時，依預設會選取 **Prioritized** (按優先順序排序)。

   1. 對於 **Spot allocation strategy** (Spot 分配策略)，選擇分配策略。依預設會選取**最佳化的價格容量**。

      如果您選擇**容量最佳化**，則可以選擇性地勾選**排定執行個體類型優先順序**方塊，讓 Amazon EC2 Auto Scaling 根據執行個體類型的列出順序，選擇要先啟動的執行個體類型。

   1. 對於**容量重新平衡**，請選擇啟用或停用容量重新平衡。當 Spot 執行個體因 Spot 中斷而即將終止時，可使用容量重新平衡來自動回應。如需詳細資訊，請參閱[Auto Scaling 中的容量重新平衡，以取代具有風險的 Spot 執行個體](ec2-auto-scaling-capacity-rebalancing.md)。

   1. 在 **Network** (網路) 下，對於 **VPC**，選擇 VPC。Auto Scaling 群組必須與您在啟動範本中指定的安全群組建立在同一個 VPC 中。

   1. 對於**可用區域和子網路**，請選擇指定 VPC 中的一個或多個子網路。在多個可用區域中，請使用子網路，以獲得高可用性。如需詳細資訊，請參閱[選擇 VPC 子網路時的注意事項](asg-in-vpc.md#as-vpc-considerations)。

   1. 選擇**下一步**、**下一步**。

1. 針對**設定群組大小和擴展政策**步驟，請執行以下操作：

   1. 對於**所需容量**，在**群組大小**下輸入要啟動之執行個體的初始數量。

      依預設，所需容量表示為執行個體數量。如果您為執行個體類型指派權重，則必須將該值轉換為與指派權重相同的測量單位，例如 vCPU 數量。

   1. 在**擴展**區段的**擴展限制**下，如果**所需容量**的新值大於**所需容量下限**和**所需容量上限**，則**所需容量上限**會自動增加到新的所需容量值。您可視需要變更這些限制。如需詳細資訊，請參閱[設定 Auto Scaling 群組的擴展限制](asg-capacity-limits.md)。

1. 選擇 **Skip to review** (跳過以進行檢閱)。

1. 在 **Review** (檢閱) 頁面上，選擇 **Create Auto Scaling group** (建立 Auto Scaling 群組)。

## 建立混合執行個體群組 (AWS CLI)
<a name="manual-instance-type-selection-aws-cli"></a>

**使用命令列建立混合執行個體群組**  
請使用以下其中一個命令：
+ [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) (AWS CLI)
+ [New-ASAutoScalingGroup](https://docs.aws.amazon.com/powershell/latest/reference/items/New-ASAutoScalingGroup.html) (AWS Tools for Windows PowerShell)

## 範例組態
<a name="manual-instance-type-selection-example-configurations"></a>

下列範例組態示範如何使用不同 Spot 分配策略建立混合執行個體群組。

**注意**  
這些範例展示如何使用 JSON 或 YAML 格式組態檔案。如果您使用 AWS CLI 版本 1，則必須指定 JSON 格式的組態檔案。如果您使用 AWS CLI 版本 2，您可以指定 YAML 或 JSON 格式的組態檔案。

**Topics**
+ [範例 1：使用 `capacity-optimized` 配置策略啟動 Spot 執行個體](#capacity-optimized-aws-cli)
+ [範例 2：使用 `capacity-optimized-prioritized` 配置策略啟動 Spot 執行個體](#capacity-optimized-prioritized-aws-cli)
+ [範例 3：使用分散在兩個集區的 `lowest-price` 配置策略啟動 Spot 執行個體](#lowest-price-aws-cli)
+ [範例 4：使用 `price-capacity-optimized` 分配策略啟動 競價型執行個體](#price-capacity-optimized-aws-cli)

### 範例 1：使用 `capacity-optimized` 配置策略啟動 Spot 執行個體
<a name="capacity-optimized-aws-cli"></a>

下列 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令會建立 Auto Scaling 群組，其會指定下列項目：
+ 要啟動多少百分比的群組來做為隨需執行個體 (`0`)，以及隨需執行個體要開始的基本數量 (`1`)。
+ 按照優先順序啟動的執行個體類型 (`c5.large`、`c5a.large`、`m5.large`、`m5a.large`、`c4.large`、`m4.large`、`c3.large`、`m3.large`)。
+ 在其中啟動執行個體的子網路 (`subnet-5ea0c127`、`subnet-6194ea3b`、`subnet-c934b782`)。每個對應於不同的可用區域。
+ 啟動範本 (`my-launch-template`) 和啟動範本的版本 (`$Default`)。

Amazon EC2 Auto Scaling 在嘗試滿足您的隨需容量時，會先啟動 `c5.large` 執行個體類型。Spot 執行個體根據 Spot 執行個體容量，來自於每個可用區域的最佳 Spot 集區。

#### JSON
<a name="capacity-optimized-aws-cli-json"></a>

```
aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json
```

`config.json` 檔案包含下列內容。

```
{
    "AutoScalingGroupName": "my-asg",
    "MixedInstancesPolicy": {
        "LaunchTemplate": {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "$Default"
            },
            "Overrides": [
                {
                    "InstanceType": "c5.large"
                },
                {
                    "InstanceType": "c5a.large"
                },
                {
                    "InstanceType": "m5.large"
                },
                {
                    "InstanceType": "m5a.large"
                },
                {
                    "InstanceType": "c4.large"
                },
                {
                    "InstanceType": "m4.large"
                },
                {
                    "InstanceType": "c3.large"
                },
                {
                    "InstanceType": "m3.large"
                }
            ]
        },
        "InstancesDistribution": {
            "OnDemandBaseCapacity": 1,
            "OnDemandPercentageAboveBaseCapacity": 0,
            "SpotAllocationStrategy": "capacity-optimized"
        }
    },
    "MinSize": 1,
    "MaxSize": 5,
    "DesiredCapacity": 3,
    "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
}
```

#### YAML
<a name="capacity-optimized-aws-cli-yaml"></a>

或者，您可使用以下 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令建立 Auto Scaling 群組。這將參考 YAML 檔案作為 Auto Scaling 群組的唯一參數。

```
aws autoscaling create-auto-scaling-group --cli-input-yaml file://~/config.yaml
```

`config.yaml` 檔案包含下列內容。

```
---
AutoScalingGroupName: my-asg
MixedInstancesPolicy:
  LaunchTemplate:
    LaunchTemplateSpecification:
      LaunchTemplateName: my-launch-template
      Version: $Default
    Overrides:
    - InstanceType: c5.large
    - InstanceType: c5a.large
    - InstanceType: m5.large
    - InstanceType: m5a.large
    - InstanceType: c4.large
    - InstanceType: m4.large
    - InstanceType: c3.large
    - InstanceType: m3.large
  InstancesDistribution:
    OnDemandBaseCapacity: 1
    OnDemandPercentageAboveBaseCapacity: 0
    SpotAllocationStrategy: capacity-optimized
MinSize: 1
MaxSize: 5
DesiredCapacity: 3
VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
```

### 範例 2：使用 `capacity-optimized-prioritized` 配置策略啟動 Spot 執行個體
<a name="capacity-optimized-prioritized-aws-cli"></a>

下列 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令會建立 Auto Scaling 群組，其會指定下列項目：
+ 要啟動多少百分比的群組來做為隨需執行個體 (`0`)，以及隨需執行個體要開始的基本數量 (`1`)。
+ 按照優先順序啟動的執行個體類型 (`c5.large`、`c5a.large`、`m5.large`、`m5a.large`、`c4.large`、`m4.large`、`c3.large`、`m3.large`)。
+ 在其中啟動執行個體的子網路 (`subnet-5ea0c127`、`subnet-6194ea3b`、`subnet-c934b782`)。每個對應於不同的可用區域。
+ 啟動範本 (`my-launch-template`) 和啟動範本的版本 (`$Latest`)。

Amazon EC2 Auto Scaling 在嘗試滿足您的隨需容量時，會先啟動 `c5.large` 執行個體類型。Amazon EC2 Auto Scaling 在嘗試滿足 Spot 容量時，會竭盡全力遵循執行個體類型優先順序。不過，它會先針對容量進行最佳化。

#### JSON
<a name="capacity-optimized-prioritized-aws-cli-json"></a>

```
aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json
```

`config.json` 檔案包含下列內容。

```
{
    "AutoScalingGroupName": "my-asg",
    "MixedInstancesPolicy": {
        "LaunchTemplate": {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "$Latest"
            },
            "Overrides": [
                {
                    "InstanceType": "c5.large"
                },
                {
                    "InstanceType": "c5a.large"
                },
                {
                    "InstanceType": "m5.large"
                },
                {
                    "InstanceType": "m5a.large"
                },
                {
                    "InstanceType": "c4.large"
                },
                {
                    "InstanceType": "m4.large"
                },
                {
                    "InstanceType": "c3.large"
                },
                {
                    "InstanceType": "m3.large"
                }
            ]
        },
        "InstancesDistribution": {
            "OnDemandBaseCapacity": 1,
            "OnDemandPercentageAboveBaseCapacity": 0,
            "SpotAllocationStrategy": "capacity-optimized-prioritized"
        }
    },
    "MinSize": 1,
    "MaxSize": 5,
    "DesiredCapacity": 3,
    "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
}
```

#### YAML
<a name="capacity-optimized-prioritized-aws-cli-yaml"></a>

或者，您可使用以下 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令建立 Auto Scaling 群組。這將參考 YAML 檔案作為 Auto Scaling 群組的唯一參數。

```
aws autoscaling create-auto-scaling-group --cli-input-yaml file://~/config.yaml
```

`config.yaml` 檔案包含下列內容。

```
---
AutoScalingGroupName: my-asg
MixedInstancesPolicy:
  LaunchTemplate:
    LaunchTemplateSpecification:
      LaunchTemplateName: my-launch-template
      Version: $Default
    Overrides:
    - InstanceType: c5.large
    - InstanceType: c5a.large
    - InstanceType: m5.large
    - InstanceType: m5a.large
    - InstanceType: c4.large
    - InstanceType: m4.large
    - InstanceType: c3.large
    - InstanceType: m3.large
  InstancesDistribution:
    OnDemandBaseCapacity: 1
    OnDemandPercentageAboveBaseCapacity: 0
    SpotAllocationStrategy: capacity-optimized-prioritized
MinSize: 1
MaxSize: 5
DesiredCapacity: 3
VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
```

### 範例 3：使用分散在兩個集區的 `lowest-price` 配置策略啟動 Spot 執行個體
<a name="lowest-price-aws-cli"></a>

下列 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令會建立 Auto Scaling 群組，其會指定下列項目：
+ 要以隨需執行個體形式啟動的群組百分比 (`50`)。(這不會指定開始的隨需執行個體的基本數目。)
+ 按照優先順序啟動的執行個體類型 (`c5.large`、`c5a.large`、`m5.large`、`m5a.large`、`c4.large`、`m4.large`、`c3.large`、`m3.large`)。
+ 在其中啟動執行個體的子網路 (`subnet-5ea0c127`、`subnet-6194ea3b`、`subnet-c934b782`)。每個對應於不同的可用區域。
+ 啟動範本 (`my-launch-template`) 和啟動範本的版本 (`$Latest`)。

Amazon EC2 Auto Scaling 在嘗試滿足您的隨需容量時，會先啟動 `c5.large` 執行個體類型。就 Spot 容量而言，Amazon EC2 Auto Scaling 嘗試在各可用區域的兩個最低價格的集區中平均啟動 Spot 執行個體。

#### JSON
<a name="lowest-price-aws-cli-json"></a>

```
aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json
```

`config.json` 檔案包含下列內容。

```
{
    "AutoScalingGroupName": "my-asg",
    "MixedInstancesPolicy": {
        "LaunchTemplate": {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "$Latest"
            },
            "Overrides": [
                {
                    "InstanceType": "c5.large"
                },
                {
                    "InstanceType": "c5a.large"
                },
                {
                    "InstanceType": "m5.large"
                },
                {
                    "InstanceType": "m5a.large"
                },
                {
                    "InstanceType": "c4.large"
                },
                {
                    "InstanceType": "m4.large"
                },
                {
                    "InstanceType": "c3.large"
                },
                {
                    "InstanceType": "m3.large"
                }
            ]
        },
        "InstancesDistribution": {
            "OnDemandPercentageAboveBaseCapacity": 50,
            "SpotAllocationStrategy": "lowest-price",
            "SpotInstancePools": 2
        }
    },
    "MinSize": 1,
    "MaxSize": 5,
    "DesiredCapacity": 3,
    "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
}
```

#### YAML
<a name="lowest-price-aws-cli-yaml"></a>

或者，您可使用以下 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令建立 Auto Scaling 群組。這將參考 YAML 檔案作為 Auto Scaling 群組的唯一參數。

```
aws autoscaling create-auto-scaling-group --cli-input-yaml file://~/config.yaml
```

`config.yaml` 檔案包含下列內容。

```
---
AutoScalingGroupName: my-asg
MixedInstancesPolicy:
  LaunchTemplate:
    LaunchTemplateSpecification:
      LaunchTemplateName: my-launch-template
      Version: $Default
    Overrides:
    - InstanceType: c5.large
    - InstanceType: c5a.large
    - InstanceType: m5.large
    - InstanceType: m5a.large
    - InstanceType: c4.large
    - InstanceType: m4.large
    - InstanceType: c3.large
    - InstanceType: m3.large
  InstancesDistribution:
    OnDemandPercentageAboveBaseCapacity: 50
    SpotAllocationStrategy: lowest-price
    SpotInstancePools: 2
MinSize: 1
MaxSize: 5
DesiredCapacity: 3
VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
```

### 範例 4：使用 `price-capacity-optimized` 分配策略啟動 競價型執行個體
<a name="price-capacity-optimized-aws-cli"></a>

下列 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令會建立 Auto Scaling 群組，其會指定下列項目：
+ 要以隨需執行個體形式啟動的群組百分比 (`30`)。(這不會指定開始的隨需執行個體的基本數目。)
+ 按照優先順序啟動的執行個體類型 (`c5.large`、`c5a.large`、`m5.large`、`m5a.large`、`c4.large`、`m4.large`、`c3.large`、`m3.large`)。
+ 在其中啟動執行個體的子網路 (`subnet-5ea0c127`、`subnet-6194ea3b`、`subnet-c934b782`)。每個對應於不同的可用區域。
+ 啟動範本 (`my-launch-template`) 和啟動範本的版本 (`$Latest`)。

Amazon EC2 Auto Scaling 在嘗試滿足您的隨需容量時，會先啟動 `c5.large` 執行個體類型。對於 Spot 容量，Amazon EC2 Auto Scaling 嘗試從 Spot 執行個體集區以可能的最低價格啟動 Spot 執行個體，但同時也具有正啟動的執行個體數量的最佳容量。

#### JSON
<a name="price-capacity-optimized-aws-cli-json"></a>

```
aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json
```

`config.json` 檔案包含下列內容。

```
{
    "AutoScalingGroupName": "my-asg",
    "MixedInstancesPolicy": {
        "LaunchTemplate": {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "my-launch-template",
                "Version": "$Latest"
            },
            "Overrides": [
                {
                    "InstanceType": "c5.large"
                },
                {
                    "InstanceType": "c5a.large"
                },
                {
                    "InstanceType": "m5.large"
                },
                {
                    "InstanceType": "m5a.large"
                },
                {
                    "InstanceType": "c4.large"
                },
                {
                    "InstanceType": "m4.large"
                },
                {
                    "InstanceType": "c3.large"
                },
                {
                    "InstanceType": "m3.large"
                }
            ]
        },
        "InstancesDistribution": {
            "OnDemandPercentageAboveBaseCapacity": 30,
            "SpotAllocationStrategy": "price-capacity-optimized"
        }
    },
    "MinSize": 1,
    "MaxSize": 5,
    "DesiredCapacity": 3,
    "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
}
```

#### YAML
<a name="price-capacity-optimized-aws-cli-yaml"></a>

或者，您可使用以下 [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) 命令建立 Auto Scaling 群組。這將參考 YAML 檔案作為 Auto Scaling 群組的唯一參數。

```
aws autoscaling create-auto-scaling-group --cli-input-yaml file://~/config.yaml
```

`config.yaml` 檔案包含下列內容。

```
---
AutoScalingGroupName: my-asg
MixedInstancesPolicy:
  LaunchTemplate:
    LaunchTemplateSpecification:
      LaunchTemplateName: my-launch-template
      Version: $Default
    Overrides:
    - InstanceType: c5.large
    - InstanceType: c5a.large
    - InstanceType: m5.large
    - InstanceType: m5a.large
    - InstanceType: c4.large
    - InstanceType: m4.large
    - InstanceType: c3.large
    - InstanceType: m3.large
  InstancesDistribution:
    OnDemandPercentageAboveBaseCapacity: 30
    SpotAllocationStrategy: price-capacity-optimized
MinSize: 1
MaxSize: 5
DesiredCapacity: 3
VPCZoneIdentifier: subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
```