

# CloudFormation을 사용하여 시작 템플릿 생성
<a name="quickref-ec2-launch-templates"></a>

이 섹션에서는 CloudFormation을 사용하여 Amazon EC2 시작 템플릿을 생성하는 예를 제공합니다. 시작 템플릿을 사용하면 AWS 내에서 Amazon EC2 인스턴스를 구성하고 프로비저닝하기 위한 템플릿을 생성할 수 있습니다. 시작 템플릿을 사용하면 인스턴스를 시작할 때마다 지정할 필요가 없도록 시작 파라미터를 저장할 수 있습니다. 더 많은 예는 `AWS::EC2::LaunchTemplate` 리소스의 [예](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 역할도 포함되어 있습니다. 이 예제는 `IamInstanceProfile` 속성을 사용하여 IAM 역할을 지정하는 시작 템플릿에 대한 [AWS::IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-iam-role.html) 리소스를 보여줍니다. `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
```