

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

# 教學課程：設定您的 EC2 機群，在可中斷的容量保留中啟動執行個體
<a name="ec2-fleet-launch-instances-interruptible-cr-walkthrough"></a>

本教學課程會逐步解說您必須執行的步驟，以便 EC2 機群在可中斷的容量保留中啟動執行個體。

可中斷容量預留代表 AWS 組織內隨需容量預留擁有者提供給您的備用容量。這些保留適用於可中斷的工作負載，因為容量擁有者可以隨時透過 2 分鐘的中斷通知回收容量，之後 EC2 會終止執行個體。如需可中斷容量保留的詳細資訊，請參閱 [可中斷的容量保留](interruptible-capacity-reservations.md)。

EC2 機群請求的目標容量應小於或等於您要鎖定的可中斷容量保留的可用容量。如果目標容量請求超過可中斷容量保留的可用容量，EC2 機群會盡可能啟動執行個體，並在 API 回應中報告啟動的執行個體。

在可中斷容量保留中執行的執行個體會繼續執行，直到您手動停止或終止，或容量擁有者回收容量為止。當容量擁有者回收容量時，Amazon EC2 會在終止執行個體前 2 分鐘傳送 Amazon EventBridge 通知。

## 考量事項
<a name="ec2-fleet-interruptible-cr-considerations"></a>
+ 只有 類型的 EC2 機群請求`instant`支援在可中斷容量保留中啟動執行個體。
+ `DefaultTargetCapacityType` 不支援使用 `OnDemandTargetCapacity`或 `SpotTargetCapacity`同時`reserved-capacity`將 設定為 。
+ 當您指定多個啟動範本時，每個範本都以不同的可中斷容量保留為目標，EC2 機群會跨所有相符的保留佈建執行個體。
+ 對於可中斷的容量保留，EC2 機群不會回復為在所需目標容量的剩餘時間內啟動隨需執行個體或 Spot 執行個體。

## 驗證許可
<a name="ec2-fleet-interruptible-cr-verify-permissions"></a>

在建立 EC2 機群之前，請確認您擁有具有必要許可的 IAM 角色。如需詳細資訊，請參閱[EC2 Fleet 先決條件](ec2-fleet-prerequisites.md)。

若要在可中斷容量保留中啟動執行個體，您必須執行下列步驟：

## 步驟 1：建立啟動範本
<a name="ec2-fleet-interruptible-cr-step1"></a>

使用 [create-launch-template](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-launch-template.html) 命令來建立啟動範本，以指定要鎖定的可中斷容量保留。在啟動範本中，`MarketType`將 設定為 ，`interruptible-capacity-reservation`並指定可中斷容量保留`CapacityReservationId`的 。

啟動範本組態範例：

```
{
    "LaunchTemplateName": "interruptible-cr-launch-template",
    "LaunchTemplateData": {
        "InstanceType": "m5.large",
        "ImageId": "ami-0abcdef1234567890",
        "CapacityReservationSpecification": {
            "CapacityReservationTarget": {
                "CapacityReservationId": "cr-0123456789abcdef0"
            }
        },
        "InstanceMarketOptions": {
            "MarketType": "interruptible-capacity-reservation"
        }
    }
}
```

範例輸出

```
{
    "LaunchTemplate": {
        "LaunchTemplateId": "lt-0123456789example",
        "LaunchTemplateName": "interruptible-cr-launch-template",
        "CreateTime": "2026-03-12T10:00:00.000Z",
        "CreatedBy": "arn:aws:iam::123456789012:user/Admin",
        "DefaultVersionNumber": 1,
        "LatestVersionNumber": 1
    }
}
```

如需詳細資訊，請參閱[建立 Amazon EC2 啟動範本](create-launch-template.md)。

## 步驟 2：設定 EC2 機群
<a name="ec2-fleet-interruptible-cr-step2"></a>

為 EC2 機群建立指定啟動範本和目標容量的組態檔案。下列組態使用`interruptible-cr-launch-template`您在步驟 1 中建立的啟動範本。

使用 `reserved-capacity`做為 `interruptible-capacity-reservation`時，您必須指定 `ReservedCapacityOptions` ，並將 `ReservationType`設定為 `DefaultTargetCapacityType`。

建立名為 `config.json` 且具有下列內容的檔案：

```
{
    "LaunchTemplateConfigs": [
        {
            "LaunchTemplateSpecification": {
                "LaunchTemplateName": "interruptible-cr-launch-template",
                "Version": "1"
            },
            "Overrides": [
                {
                    "InstanceType": "m5.large",
                    "AvailabilityZone": "us-east-1a"
                }
            ]
        }
    ],
    "TargetCapacitySpecification": {
        "TotalTargetCapacity": 10,
        "DefaultTargetCapacityType": "reserved-capacity"
    },
    "ReservedCapacityOptions": {
        "ReservationType": ["interruptible-capacity-reservation"]
    },
    "Type": "instant"
}
```

金鑰組態參數：


| 參數 | Description | 
| --- | --- | 
| DefaultTargetCapacityType | 設定為 reserved-capacity，表示執行個體應該以預留容量啟動。 | 
| ReservedCapacityOptions | 指定預留容量的類型。對於可中斷的容量保留，請將 ReservationType設定為 ["interruptible-capacity-reservation"]。 | 
| Type | 必須設定為 instant。中斷容量保留僅支援即時機群。 | 

## 步驟 3：啟動機群並檢視結果
<a name="ec2-fleet-interruptible-cr-step3"></a>

使用 [create-fleet](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-fleet.html) 命令來建立機群：

```
aws ec2 create-fleet \
    --cli-input-json file://config.json
```

使用上述組態建立機`instant`群後，EC2 機群會在可中斷容量保留中啟動 10 個執行個體，以符合目標容量。

**注意**  
如果機群無法滿足完整目標容量，回應會包含已啟動的執行個體，以及未滿足容量的任何錯誤。

範例輸出

```
{
    "FleetId": "fleet-12345678-1234-1234-1234-123456789012",
    "Instances": [
        {
            "LaunchTemplateAndOverrides": {
                "LaunchTemplateSpecification": {
                    "LaunchTemplateId": "lt-0123456789example",
                    "Version": "1"
                },
                "Overrides": {
                    "InstanceType": "m5.large",
                    "AvailabilityZone": "us-east-1a"
                }
            },
            "Lifecycle": "interruptible-capacity-reservation",
            "InstanceIds": [
                "i-0123456789example1",
                "i-0123456789example2",
                "i-0123456789example3",
                "i-0123456789example4",
                "i-0123456789example5",
                "i-0123456789example6",
                "i-0123456789example7",
                "i-0123456789example8",
                "i-0123456789example9",
                "i-0123456789example0"
            ],
            "InstanceType": "m5.large",
            "Platform": "Linux/UNIX"
        }
    ],
    "Errors": []
}
```

如需詳細資訊，請參閱[建立 EC2 Fleet](create-ec2-fleet.md)。

## 清除
<a name="ec2-fleet-interruptible-cr-cleanup"></a>

若要停止產生費用，請在不再需要執行個體時將其終止。請注意，當容量擁有者回收容量時，EC2 也會自動終止在可中斷容量保留中啟動的執行個體。

## 相關資源
<a name="ec2-fleet-interruptible-cr-related-resources"></a>
+ [可中斷的容量保留](interruptible-capacity-reservations.md)
+ [使用 EC2 隨需容量保留來保留運算容量](ec2-capacity-reservations.md)
+ [使用 EC2 機群](manage-ec2-fleet.md)
+ [建立 EC2 Fleet](create-ec2-fleet.md)
+ [將執行個體啟動參數存放在 Amazon EC2 啟動範本中](ec2-launch-templates.md)