

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

# 使用 建立和管理啟動範本的範例 AWS CLI
<a name="examples-launch-templates-aws-cli"></a>

您可以透過 AWS 管理主控台、 AWS Command Line Interface (AWS CLI) 或 SDKs 建立和管理啟動範本。本節顯示從 建立和管理 Amazon EC2 Auto Scaling 啟動範本的範例 AWS CLI。

**Topics**
+ [範例使用方式](#example-usage)
+ [建立基本的啟動範本](#example-simple-launch-template)
+ [指定在啟動時標記執行個體的標籤](#example-tag-instances)
+ [指定要傳遞給執行個體的 IAM 角色](#example-iam-profile)
+ [指派公有 IP 地址](#example-primary-network-interface)
+ [指定啟動時設定執行個體的使用者資料指令碼](#example-user-data)
+ [指定區塊型設備映射](#example-block-device-mapping)
+ [指定專用主機以從外部廠商取得軟體授權](#example-dedicated-hosts)
+ [指定現有網路介面](#example-existing-eni-launch-template)
+ [建立多個網路介面](#example-multiple-efa-enabled-network-interfaces)
+ [管理啟動範本](#launch-templates-additional-cli-commands)
+ [更新 Auto Scaling 群組以使用啟動範本](#update-asg-launch-template-cli)

## 範例使用方式
<a name="example-usage"></a>

```
{
    "LaunchTemplateName": "my-template-for-auto-scaling",
    "VersionDescription": "test description",
    "LaunchTemplateData": {
        "ImageId": "ami-04d5cc9b88example",
        "InstanceType": "t2.micro",
        "SecurityGroupIds": [
            "sg-903004f88example"
        ], 
        "KeyName": "MyKeyPair",
        "Monitoring": {
            "Enabled": true
        },
        "Placement": {
            "Tenancy": "dedicated"
        },
        "CreditSpecification": {
            "CpuCredits": "unlimited"
        },
        "MetadataOptions": {
            "HttpTokens": "required",
            "HttpPutResponseHopLimit": 1,
            "HttpEndpoint": "enabled"
        }
    }
}
```

## 建立基本的啟動範本
<a name="example-simple-launch-template"></a>

若要建立基本的啟動範本，請按如下所示使用 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 命令，且必須進行這些修改：
+ 以從中啟動執行個體的 AMI 的 ID 取代 `ami-04d5cc9b88example`。
+ 以與您所指定的 AMI 相容的執行個體取代 `t2.micro`。

此範例會建立名稱為 *my-template-for-auto-scaling* 的啟動範本。如果此啟動範本建立的執行個體在預設 VPC 中啟動，則依預設，它們會接收公有 IP 地址。如果執行個體是在非預設的 VPC 中啟動，則其預設不會接收公有 IP 位址。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

如需有關引用 JSON 格式參數的詳細資訊，請參閱《AWS Command Line Interface 使用者指南》**中的[在 AWS CLI中搭配字串使用引號](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-quoting-strings.html)。

您也可以在組態檔案中指定 JSON 格式參數。

下列範例會建立基本的啟動範本，其會參考啟動範本參數值的組態檔案。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data file://config.json
```

`config.json` 的內容：

```
{ 
    "ImageId":"ami-04d5cc9b88example",
    "InstanceType":"t2.micro"
}
```

## 指定在啟動時標記執行個體的標籤
<a name="example-tag-instances"></a>

下列範例會在啟動時將標籤 (例如：`purpose=webserver`) 新增至執行個體。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"TagSpecifications":[{"ResourceType":"instance","Tags":[{"Key":"purpose","Value":"webserver"}]}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

**注意**  
如果在啟動範本中指定執行個體標籤，接著選擇將 Auto Scaling 群組的標籤傳播到其執行個體，則會合併所有標籤。如果為啟動範本中的標籤和 Auto Scaling 群組中的標籤指定了相同的標籤鍵，則以群組中的標籤值為優先。

## 指定要傳遞給執行個體的 IAM 角色
<a name="example-iam-profile"></a>

下列範例會指定與 IAM 角色相關聯的執行個體描述檔名稱，以便在啟動時傳遞給執行個體。如需詳細資訊，請參閱 [在 Amazon EC2 執行個體上執行的應用程式的 IAM 角色](us-iam-role.md)。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
--launch-template-data '{"IamInstanceProfile":{"Name":"my-instance-profile"},"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 指派公有 IP 地址
<a name="example-primary-network-interface"></a>

下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 範例會設定啟動範本，將公有地址指派給在非預設 VPC 中啟動的執行個體。

**注意**  
在指定網路介面時，請為 `Groups`，即對應至 Auto Scaling 群組在其中啟動執行個體的 VPC 安全群組指定數值。將 VPC 子網路指定為 Auto Scaling 群組的屬性。



```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"NetworkInterfaces":[{"DeviceIndex":0,"AssociatePublicIpAddress":true,"Groups":["sg-903004f88example"],"DeleteOnTermination":true}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 指定啟動時設定執行個體的使用者資料指令碼
<a name="example-user-data"></a>

下列範例會將使用者資料指令碼指定為在啟動時設定執行個體的 base64 編碼字串。[create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 命令需要 base64 編碼的使用者資料。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
--launch-template-data '{"UserData":"IyEvYmluL2Jhc...","ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 指定區塊型設備映射
<a name="example-block-device-mapping"></a>

下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 範例會建立具備區塊型設備映射的啟動範本：一個 22 GB 的 EBS 磁碟區，並將其映射至 `/dev/xvdcz`。`/dev/xvdcz` 磁碟區使用一般用途 SSD (gp2) 磁碟區類型，並會在與其相連接的執行個體終止時刪除。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"BlockDeviceMappings":[{"DeviceName":"/dev/xvdcz","Ebs":{"VolumeSize":22,"VolumeType":"gp2","DeleteOnTermination":true}}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 指定專用主機以從外部廠商取得軟體授權
<a name="example-dedicated-hosts"></a>

若您指定*主機*租用，則可以指定主機資源群組和 License Manager 授權組態，以便從外部廠商取得合格的軟體授權。然後，您可以藉由下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 命令，在 EC2 執行個體上使用授權。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"Placement":{"Tenancy":"host","HostResourceGroupArn":"arn"},"LicenseSpecifications":[{"LicenseConfigurationArn":"arn"}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 指定現有網路介面
<a name="example-existing-eni-launch-template"></a>

下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 範例會將主要網路介面設定為使用現有網路介面。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"NetworkInterfaces":[{"DeviceIndex":0,"NetworkInterfaceId":"eni-b9a5ac93","DeleteOnTermination":false}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

## 建立多個網路介面
<a name="example-multiple-efa-enabled-network-interfaces"></a>

下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 範例會新增次要網路介面。主要網路介面的裝置索引為 0，而次要網路介面的裝置索引為 1。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"NetworkInterfaces":[{"DeviceIndex":0,"Groups":["sg-903004f88example"],"DeleteOnTermination":true},{"DeviceIndex":1,"Groups":["sg-903004f88example"],"DeleteOnTermination":true}],"ImageId":"ami-04d5cc9b88example","InstanceType":"t2.micro"}'
```

如果使用支援多張網路卡和 Elastic Fabric Adapter (EFA) 的執行個體類型，您可以將次要介面新增至次要網路卡，再使用下列 [create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html) 命令啟用 EFA。如需詳細資訊，請參閱《*Amazon EC2 使用者指南*》中的[將 EFA 新增至啟動範本](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-efa.html#efa-launch-template)。

```
aws ec2 create-launch-template --launch-template-name my-template-for-auto-scaling --version-description version1 \
  --launch-template-data '{"NetworkInterfaces":[{"NetworkCardIndex":0,"DeviceIndex":0,"Groups":["sg-7c2270198example"],"InterfaceType":"efa","DeleteOnTermination":true},{"NetworkCardIndex":1,"DeviceIndex":1,"Groups":["sg-7c2270198example"],"InterfaceType":"efa","DeleteOnTermination":true}],"ImageId":"ami-09d95fab7fexample","InstanceType":"p4d.24xlarge"}'
```

**警告**  
p4d.24xlarge 執行個體類型所產生的成本高於本節中的其他範例。如需有關 P4d 執行個體定價的詳細資訊，請參閱 [Amazon EC2 P4d 執行個體定價](https://aws.amazon.com/ec2/instance-types/p4/)。

**注意**  
將相同子網路中的多個網路介面連接至執行個體，會導致非對稱路由，這種情況特別容易發生在使用非 Amazon Linux 變體的執行個體上。如果需要這種類型的組態，您必須在作業系統內設定次要網路介面。如需範例，請參閱 AWS 知識中心的[如何讓次要網路界面在 Ubuntu EC2 執行個體中運作？](https://repost.aws/knowledge-center/ec2-ubuntu-secondary-network-interface)。

## 管理啟動範本
<a name="launch-templates-additional-cli-commands"></a>

 AWS CLI 包含數個其他命令，可協助您管理啟動範本。

**Topics**
+ [列出並描述啟動範本](#describe-launch-template-aws-cli)
+ [建立啟動範本版本](#example-create-launch-template-version)
+ [刪除啟動範本版本](#example-delete-launch-template-version)
+ [刪除啟動範本](#example-delete-launch-template)

### 列出並描述啟動範本
<a name="describe-launch-template-aws-cli"></a>

您可以使用兩個 AWS CLI 命令來取得啟動範本的相關資訊：[describe-launch-templates](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-launch-templates.html) 和 [describe-launch-template-versions](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-launch-template-versions.html)。

[describe-launch-templates](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-launch-templates.html) 命令能取得您已建立的任何啟動範本的清單。您可以使用選項來篩選啟動範本名稱的結果、建立時間、標籤鍵或標籤鍵值對。此命令會傳回任何啟動範本的摘要資訊，包括啟動範本識別符、最新版本和預設版本。

下列範例提供所指定啟動範本的摘要。

```
aws ec2 describe-launch-templates --launch-template-names my-template-for-auto-scaling
```

以下是回應範例。

```
{
    "LaunchTemplates": [
        {
            "LaunchTemplateId": "lt-068f72b729example",
            "LaunchTemplateName": "my-template-for-auto-scaling",
            "CreateTime": "2020-02-28T19:52:27.000Z",
            "CreatedBy": "arn:aws:iam::123456789012:user/Bob",
            "DefaultVersionNumber": 1,
            "LatestVersionNumber": 1
        }
    ]
}
```

若您沒有使用 `--launch-template-names` 選項將輸出限制為一個啟動範本，則會傳回關於所有啟動範本的資訊。

下列 [describe-launch-template-versions](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-launch-template-versions.html) 命令提供描述指定啟動範本之版本的資訊。

```
aws ec2 describe-launch-template-versions --launch-template-id lt-068f72b729example
```

以下是回應範例。

```
{
    "LaunchTemplateVersions": [
        {
            "VersionDescription": "version1",
            "LaunchTemplateId": "lt-068f72b729example",
            "LaunchTemplateName": "my-template-for-auto-scaling",
            "VersionNumber": 1,
            "CreatedBy": "arn:aws:iam::123456789012:user/Bob",
            "LaunchTemplateData": {
                "TagSpecifications": [
                    {
                        "ResourceType": "instance",
                        "Tags": [
                            {
                                "Key": "purpose",
                                "Value": "webserver"
                            }
                        ]
                    }
                ],
                "ImageId": "ami-04d5cc9b88example",
                "InstanceType": "t2.micro",
                "NetworkInterfaces": [
                    {
                        "DeviceIndex": 0,
                        "DeleteOnTermination": true,
                        "Groups": [
                            "sg-903004f88example"
                        ],
                        "AssociatePublicIpAddress": true
                    }
                ]
            },
            "DefaultVersion": true,
            "CreateTime": "2020-02-28T19:52:27.000Z"
        }
    ]
}
```

### 建立啟動範本版本
<a name="example-create-launch-template-version"></a>

下列 [create-launch-template-version](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template-version.html) 命令，會建立以第 1 版啟動範本為基礎的新啟動範本版本，並指定不同的 AMI ID。

```
aws ec2 create-launch-template-version --launch-template-id lt-068f72b729example --version-description version2 \
  --source-version 1 --launch-template-data "ImageId=ami-c998b6b2example"
```

若要設定啟動範本的預設版本，請使用 [modify-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/modify-launch-template.html) 命令。

### 刪除啟動範本版本
<a name="example-delete-launch-template-version"></a>

下列 [delete-launch-template-versions](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/delete-launch-template-versions.html) 命令會刪除指定的啟動範本版本。

```
aws ec2 delete-launch-template-versions --launch-template-id lt-068f72b729example --versions 1
```

### 刪除啟動範本
<a name="example-delete-launch-template"></a>

若不再需要啟動範本，即可使用 [delete-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/delete-launch-template.html) 命令將其刪除。刪除啟動範本會刪除它的所有版本。

```
aws ec2 delete-launch-template --launch-template-id lt-068f72b729example
```

## 更新 Auto Scaling 群組以使用啟動範本
<a name="update-asg-launch-template-cli"></a>

您可以使用 [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) 命令將啟動範本新增至現有 Auto Scaling 群組。

### 更新 Auto Scaling 群組以使用最新版本的啟動範本
<a name="example-update-asg-launch-template-latest-version"></a>

下列 [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) 命令會更新指定的 Auto Scaling 群組，以便使用指定啟動範本的最新版本。

```
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
  --launch-template LaunchTemplateId=lt-068f72b729example,Version='$Latest'
```

### 更新 Auto Scaling 群組以使用特定版本的啟動範本
<a name="example-update-asg-launch-template-specific-version"></a>

下列 [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) 命令會更新指定的 Auto Scaling 群組，以便使用指定啟動範本的特定版本。

```
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
  --launch-template LaunchTemplateName=my-template-for-auto-scaling,Version='2'
```