

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# を使用した起動テンプレートの作成と管理の例 AWS CLI
<a name="examples-launch-templates-aws-cli"></a>

起動テンプレートは、、 AWS Command Line Interface (AWS CLI) AWS マネジメントコンソール、または 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)
+ [外部ベンダーからソフトウェアライセンスを取得するための Dedicated Hosts を指定する](#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 で起動したインスタンスに、パブリックアドレスを割り当てる起動テンプレートを設定します。

**注記**  
ネットワークインターフェイスを指定する場合、Auto Scaling グループがインスタンスを起動する VPC のセキュリティグループに対応する`Groups`の値を指定します。Auto Scaling グループのプロパティとして VPC サブネットを指定します。



```
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-encoded 文字列としてユーザーデータスクリプトを指定します。[create-launch-template](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/create-launch-template.html)コマンドには、base64-encoded ユーザーデータが必要です。

```
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}}"}'
```

## 外部ベンダーからソフトウェアライセンスを取得するための Dedicated Hosts を指定する
<a name="example-dedicated-hosts"></a>

*ホスト*テナンシーを指定すると、ホストリソースグループと License Manager ライセンス構成を指定して、外部ベンダーから適格なソフトウェアライセンスを取得できます。その後、EC2 インスタンスでライセンスを使用するには、次の [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 '{"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 Adapters (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 以外のバリアントを使用するインスタンスでは、非対称ルーティングが発生する場合があります。このタイプの設定が必要な場合は、OS 内でセカンダリネットワークインターフェイスを設定する必要があります。例については、「 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>

起動テンプレートに関する情報を取得するには、[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) の 2 つの AWS CLI コマンドを使用できます。

[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
        }
    ]
}
```

出力を 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-versions](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-templatee](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}}'
```