

# CloudFormation を使用して起動テンプレートを作成する
<a name="quickref-ec2-launch-templates"></a>

このセクションでは、CloudFormation を使用して Amazon EC2 起動テンプレートを作成する方法を例を挙げて説明します。起動テンプレートを使用すると、AWS 内で Amazon EC2 インスタンスを設定してプロビジョニングするためのテンプレートを作成することができます。起動テンプレートを使用すると起動パラメータを保存することができるため、インスタンスを起動するたびに起動パラメータ指定する必要がなくなります。その他の例については、`AWS::EC2::LaunchTemplate` リソースの「[Examples](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-launchtemplate.html#aws-resource-ec2-launchtemplate--examples)」のセクションを参照してください。

起動テンプレートの詳細については、『Amazon EC2 ユーザーガイド』の「[Amazon EC2 起動テンプレートにインスタンス起動パラメータを保存する](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html)」を参照してください。

Auto Scaling グループで使用する起動テンプレートの作成方法については、『Amazon EC2 Auto Scaling ユーザーガイド』の「[Auto Scaling 起動テンプレート](https://docs.aws.amazon.com/autoscaling/ec2/userguide/launch-templates.html)」を参照してください。

**Topics**
+ [セキュリティグループ、タグ、ユーザーデータ、IAM ロールを指定する起動テンプレートを作成する](#scenario-as-launch-template)

## セキュリティグループ、タグ、ユーザーデータ、IAM ロールを指定する起動テンプレートを作成する
<a name="scenario-as-launch-template"></a>

このスニペットには、インスタンスを起動する設定情報が含まれる [AWS::EC2::LaunchTemplate](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-launchtemplate.html) リソースが記されています。`ImageId`、`InstanceType`、`SecurityGroups`、`UserData`、および `TagSpecifications` プロパティの値を指定します。`SecurityGroups` プロパティは、既存の EC2 セキュリティグループと新しいセキュリティグループを指定します。`Ref` 関数は、スタックテンプレートの他の場所で宣言されている [AWS::EC2::SecurityGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-securitygroup.html) リソース `myNewEC2SecurityGroup` の ID を取得します。

起動テンプレートにはカスタムユーザーデータのセクションが含まれています。このセクションでは、インスタンスの起動時に実行される構成タスクとスクリプトを渡すことができます。この例では、ユーザーデータは AWS Systems Manager Agent をインストールし、エージェントを起動します。

起動テンプレートには、インスタンスで実行中のアプリケーションが、ユーザーの代わりにアクションを実行できるようにする IAM ロールも含まれています。この例では、起動テンプレートの [AWS::IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-iam-role.html) リソースを示しており、`IamInstanceProfile` プロパティを使用して IAM ロールを指定します。`Ref` 関数は、[AWS::IAM::InstanceProfile](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-iam-instanceprofile.html) リソース `myInstanceProfile` の名前を取得します。IAM ロールのアクセス許可を設定するには、`ManagedPolicyArns` プロパティの値を指定します。

### JSON
<a name="quickref-launch-template-example-1.json"></a>

```
 1. {
 2.   "Resources":{
 3.     "myLaunchTemplate":{
 4.       "Type":"AWS::EC2::LaunchTemplate",
 5.       "Properties":{
 6.         "LaunchTemplateName":{ "Fn::Sub": "${AWS::StackName}-launch-template" },
 7.         "LaunchTemplateData":{
 8.           "ImageId":"{{ami-02354e95b3example}}",
 9.           "InstanceType":"{{t3.micro}}",
10.           "IamInstanceProfile":{
11.             "Name":{
12.               "Ref":"myInstanceProfile"
13.             }
14.           },
15.           "SecurityGroupIds":[
16.             {
17.               "Ref":"{{myNewEC2SecurityGroup}}"
18.             },
19.             "{{sg-083cd3bfb8example}}"
20.           ],
21.           "UserData":{
22.             "Fn::Base64":{
23.               "Fn::Join": [
24.                 "", [
25.                   "{{#!/bin/bash\n}}",
26.                   "{{cd /tmp\n}}",
27.                   "{{yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm\n}}",
28.                   "{{systemctl enable amazon-ssm-agent\n}}",
29.                   "{{systemctl start amazon-ssm-agent\n}}"
30.                 ]
31.               ]
32.             }
33.           },
34.           "TagSpecifications":[
35.             {
36.               "ResourceType":"instance",
37.               "Tags":[
38.                 {
39.                   "Key":"{{environment}}",
40.                   "Value":"{{development}}"
41.                 }
42.               ]
43.             },
44.             {
45.               "ResourceType":"volume",
46.               "Tags":[
47.                 {
48.                   "Key":"{{environment}}",
49.                   "Value":"{{development}}"
50.                 }
51.               ]
52.             }
53.           ]
54.         }
55.       }
56.     },
57.     "myInstanceRole":{
58.       "Type":"AWS::IAM::Role",
59.       "Properties":{
60.         "RoleName":"InstanceRole",
61.         "AssumeRolePolicyDocument":{
62.           "Version": "2012-10-17",		 	 	 
63.           "Statement":[
64.             {
65.               "Effect":"Allow",
66.               "Principal":{
67.                 "Service":[
68.                   "ec2.amazonaws.com"
69.                 ]
70.               },
71.               "Action":[
72.                 "sts:AssumeRole"
73.               ]
74.             }
75.           ]
76.         },
77.         "ManagedPolicyArns":[
78.           "arn:aws:iam::aws:policy/{{myCustomerManagedPolicy}}"
79.         ]
80.       }
81.     },
82.     "myInstanceProfile":{
83.       "Type":"AWS::IAM::InstanceProfile",
84.       "Properties":{
85.         "Path":"/",
86.         "Roles":[
87.           {
88.             "Ref":"myInstanceRole"
89.           }
90.         ]
91.       }
92.     }
93.   }
94. }
```

### YAML
<a name="quickref-launch-template-example-1.yaml"></a>

```
 1. ---
 2. Resources:
 3.   myLaunchTemplate:
 4.     Type: AWS::EC2::LaunchTemplate
 5.     Properties:
 6.       LaunchTemplateName: !Sub ${AWS::StackName}-launch-template
 7.       LaunchTemplateData:
 8.         ImageId: {{ami-02354e95b3example}}
 9.         InstanceType: {{t3.micro}}
10.         IamInstanceProfile:
11.           Name: !Ref myInstanceProfile
12.         SecurityGroupIds:
13.         - !Ref {{myNewEC2SecurityGroup}}
14.         - {{sg-083cd3bfb8example}}
15.         UserData:
16.           Fn::Base64: !Sub |
17.             {{#!/bin/bash
18.             cd /tmp
19.             yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
20.             systemctl enable amazon-ssm-agent
21.             systemctl start amazon-ssm-agent}}
22.         TagSpecifications:
23.         - ResourceType: instance
24.           Tags:
25.           - Key: {{environment}}
26.             Value: {{development}}
27.         - ResourceType: volume
28.           Tags:
29.           - Key: {{environment}}
30.             Value: {{development}}
31.   myInstanceRole:
32.     Type: AWS::IAM::Role
33.     Properties:
34.       RoleName: InstanceRole
35.       AssumeRolePolicyDocument:
36.         Version: '2012-10-17'
37.         Statement:
38.         - Effect: 'Allow'
39.           Principal:
40.             Service:
41.             - 'ec2.amazonaws.com'
42.           Action:
43.           - 'sts:AssumeRole'
44.       ManagedPolicyArns:
45.         - 'arn:aws:iam::aws:policy/{{myCustomerManagedPolicy}}'
46.   myInstanceProfile:
47.     Type: AWS::IAM::InstanceProfile
48.     Properties:
49.       Path: '/'
50.       Roles:
51.       - !Ref myInstanceRole
```