
22E. CloudFormation ํ ํ๋ฆฟ์ผ๋ก CloudFormation ์คํ ์์ฑํ๊ธฐ
22E. CloudFormation ํ ํ๋ฆฟ์ผ๋ก CloudFormation ์คํ ์์ฑํ๊ธฐ ๊ด๋ จ

CloudFormation ํ ํ๋ฆฟ์ผ๋ก CloudFormation ์คํ์ ์์ฑํด๋ณด๊ฒ ์ต๋๋ค. ํ ํ๋ฆฟ์ ์์์ ์ค๋ช ํ ๊ธฐ๋ณธ ํ ํ๋ฆฟ์ ์กฐํฉํ์ฌ EC2 ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ , 22, 80๋ฒ ํฌํธ๋ฅผ ์ฌ๋ Security Group์ ์ค์ ํ๊ณ , ์ํ์น ์น ์๋ฒ๋ฅผ ์ค์น, ์คํํ๋ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
๋ฉ๋ชจ์ฅ์ด๋ ๊ธฐํ ํ ์คํธ ํธ์ง๊ธฐ๋ฅผ ์ด๊ณ ์๋์ ๊ฐ์ด ํ ํ๋ฆฟ์ ์์ฑํ ๋ค httpd.template์ผ๋ก ์ ์ฅํฉ๋๋ค.
EC2 ์ธ์คํด์ค ์์ฑ, 22, 80๋ฒ ํฌํธ๋ฅผ ์ฌ๋ Security Group ์ค์ , ์ํ์น ์น ์๋ฒ ์ค์น, ์คํ ํ ํ๋ฆฟ(
installhttpdsecuritygroup.template
)
{
"Description" : "Create an EC2 instance running the Amazon Linux 64 bit AMI.",
"Parameters" : {
"KeyPair" : {
"Description" : "The EC2 Key Pair to allow SSH access to the instance",
"Type" : "String",
"Default" : "awskeypair"
}
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyPair" },
"ImageId" : "ami-c9562fc8",
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"InstanceType" : "t1.micro",
"UserData": {
"Fn::Base64": {
"Fn::Join": [ "",
[ "#!/bin/bash\n",
"/opt/aws/bin/cfn-init --region ", { "Ref": "AWS::Region" },
" -s ", { "Ref": "AWS::StackName" },
" -r Ec2Instance\n" ]
]
}
}
},
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"yum" : {
"httpd" : []
}
},
"services" : {
"sysvinit" : {
"httpd" : {
"enabled" : "true",
"ensureRunning" : "true"
}
}
}
}
}
}
},
"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Allow HTTP and SSH access",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
}, {
"IpProtocol" : "tcp",
"FromPort" : "80",
"ToPort" : "80",
"CidrIp" : "0.0.0.0/0"
} ]
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "The InstanceId of the newly created EC2 instance",
"Value" : {
"Ref" : "Ec2Instance"
}
}
},
"AWSTemplateFormatVersion" : "2010-09-09"
}

[CloudFormation]
๋ฅผ ํด๋ฆญํฉ๋๋ค.์ค๋ฅธ์ชฝ ์์์ CloudFormation์ ๋ฆฌ์ ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ํ ํ๋ฆฟ์ผ๋ก CloudFormation ์คํ์ ์์ฑํ๋ฉด ์ฌ๊ธฐ์ ์ ํํ ๋ฆฌ์ ์ AWS ๋ฆฌ์์ค๊ฐ ์์ฑ๋ฉ๋๋ค.

์์ฑํ CloudFormation ์คํ์ด ํ๋๋ ์์ ๋ ํ์ด์ง๊ฐ ํ์๋ฉ๋๋ค.

[Create New Stack]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค.CloudFormation ํ ํ๋ฆฟ์ผ๋ก CloudFormation ์คํ์ ์์ฑํฉ๋๋ค.
- Name: CloudFormation ์คํ์ ์ด๋ฆ์ ๋๋ค. httpd๋ฅผ ์ ๋ ฅํฉ๋๋ค.
- Source: ์ฌ์ฉํ CloudFormation ํ
ํ๋ฆฟ์
๋๋ค. Upload a template to Amazon S3์ ์ ํํ๊ณ ํ์ผ ์ ํ ๋ฒํผ์ ํด๋ฆญํฉ๋๋ค.
- Select a sample template: AWS์์ ์ ๊ณตํ๋ ์์ ํ ํ๋ฆฟ์ ๋๋ค.
- Upload a template to Amazon S3: ํ์ฌ ์ปดํจํฐ์ ์๋ ํ ํ๋ฆฟ ํ์ผ์ S3์ ์ฌ๋ฆฐ ํ ์คํ์ ์์ฑํฉ๋๋ค.
- Specify an Amazon S3 Template URL: ํ ํ๋ฆฟ ํ์ผ์ ๋ฏธ๋ฆฌ S3 ๋ฒํท์ ์ฌ๋ ธ๋ค๋ฉด S3 ๋ฒํท์ ์ ์ฅ๋ ํ ํ๋ฆฟ ํ์ผ์ ๊ฒฝ๋ก์ ๋๋ค.

๋ฐฉ๊ธ ์ ์ฅํ httpd.template
ํ์ผ์ ์ ํํ๊ณ , [์ด๊ธฐ]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค. ํ์ผ์ ์ ํ์ ํ์ผ๋ฉด ์๋์ชฝ [Next]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค (๋์ค์ S3 ๋ฒํท ๋ชฉ๋ก์ ๋ณด๋ฉด ํ
ํ๋ฆฟ ํ์ผ์ด ์ ์ฅ๋ S3 ๋ฒํท์ด ์์ฑ๋์ด ์์ต๋๋ค).

EC2 ์ธ์คํด์ค์ ์ฌ์ฉํ ํค ์์ ์ค์ ํฉ๋๋ค. ์ฐ๋ฆฌ๊ฐ ๋ง๋ ํ
ํ๋ฆฟ์๋ awskeypair
๋ผ๋ ํค๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ํด๋์์ต๋๋ค. ๋ค๋ฅธ ํค ์์ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด KeyPair ๋ถ๋ถ์ ํค ์ ์ด๋ฆ์ ์
๋ ฅํ๋ฉด ๋ฉ๋๋ค.

[Next]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค.CloudFormation ์คํ์ ์์ฑํ ๋ ์ฌ์ฉํ ์ต์ ์ ๋๋ค.
- Tags: CloudFormation ์คํ์ ํ๊ทธ์ ๋๋ค. ๊ธฐ๋ณธ๊ฐ ๊ทธ๋๋ก ๋น์๋ก๋๋ค.
- Advanced: ์ถ๊ฐ ์ต์
์
๋๋ค. ์ด๋ฒ์๋ ๋ฐ๋ก ์ค์ ํ์ง ์๊ณ , ๊ฐ ํญ๋ชฉ ์ค๋ช
๋ง ํ๊ฒ ์ต๋๋ค.
- Notification options: CloudFormation ์คํ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด SNS๋ก ์๋ฆผ์ ๋ณด๋ ๋๋ค.
- Timeout: CloudFormation ์คํ ์์ฑ์ ์์ํ๊ณ ์ฌ๊ธฐ์ ์ค์ ํ ์๊ฐ(๋ถ) ์ด์ ์ง๋๋ฉด ์คํ ์์ฑ์ ์คํจํ ๊ฒ์ผ๋ก ๋ณด๊ณ , ๋ชจ๋ AWS ๋ฆฌ์์ค์ ์ค์ ์ ๋๋๋ฆฝ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก Timeout ๊ฐ์ด ์ค์ ๋์ด ์์ง ์์ผ๋ฏ๋ก ์คํ ์์ฑ์ด ์ฑ๊ณตํ ๋๊น์ง ๊ธฐ๋ค๋ฆฝ๋๋ค.
- Rollback on failure: CloudFormation ์คํ์ ์์ฑํ๋ค๊ฐ ์ค๊ฐ์ ์คํจํ๋ฉด AWS ๋ฆฌ์์ค์ ์ค์ ์ ๋๋๋ฆฝ๋๋ค.
- Stack policy: CloudFormation ์คํ ์ ๋ฐ์ดํธ ์ ์ฑ ์ ๋๋ค. ์ค์๋ก ์์ฑ๋ ์คํ์ ์ค์ ์ ๋ณ๊ฒฝํ์ง ์๋๋ก ํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ๋ค์ ๋งํฌ๋ฅผ ์ฐธ์กฐํ๊ธฐ ๋ฐ๋๋๋ค.

[Next]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค.์ง๊ธ๊น์ง ์ค์ ํ ๋ด์ฉ์ ์ด์์ด ์๋์ง ํ์ธํฉ๋๋ค.

[Create]
๋ฒํผ์ ํด๋ฆญํฉ๋๋ค.CloudFormation ์คํ ๋ชฉ๋ก์ CloudFormation ์คํ(httpd
)์ด ์์ฑ๋์์ต๋๋ค.

CREATE_IN_PROGRESS
๋ก ๋์ค๋ฉฐ AWS ๋ฆฌ์์ค๋ฅผ ์์ฑํ๊ณ ์ค์ ํ๊ณ ์์ต๋๋ค. ์์ ํ ์์ฑ๋๋ ์๊ฐ์ AWS ๋ฆฌ์์ค์ ์ข
๋ฅ์ ๊ฐ์์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋๋ค.CloudFormation ์คํ ์์ฑ์ด ์๋ฃ๋ ๋ค Output ํญ์ ํด๋ฆญํ๋ฉด ์์ฑ๋ EC2 ์ธ์คํด์ค์ ID๊ฐ ํ์๋ฉ๋๋ค.

EC2 ์ธ์คํด์ค ๋ชฉ๋ก([INSTANCES]
โ [Instances]
)์ CloudFormation์ด ์์ฑํ EC2 ์ธ์คํด์ค๊ฐ ์ถ๊ฐ๋์์ต๋๋ค. ์ CloudFormation ์คํ Outputs์ ๋์จ ์ธ์คํด์ค ID์ ๊ฐ์ต๋๋ค.

์น ๋ธ๋ผ์ฐ์ ๋ฅผ ์คํํ๊ณ EC2 ์ธ์คํด์ค์ Public IP์ ์ ์ํฉ๋๋ค.

์ด์ฒ๋ผ CloudFormation ํ ํ๋ฆฟ์ผ๋ก AWS ๋ฆฌ์์ค๋ฅผ ์กฐํฉํ CloudFormation ์คํ์ ์์ฑํ ์ ์์ต๋๋ค. CloudFormation ์คํ์ ์ญ์ ํ๋ฉด ์ง๊ธ๊น์ง ์์ฑ๋ AWS ๋ฆฌ์์ค๋ ๋ชจ๋ ์ญ์ ๋ฉ๋๋ค. CloudFormation ์คํ์ ํฌํจ๋ EC2 ์ธ์คํด์ค ๋ฑ์ ์ ํ์ ๋ณ๊ฒฝํ๊ฑฐ๋ AWS ๋ฆฌ์์ค์ ์ค์ ์ ๋ณ๊ฒฝํ๊ณ ์ถ์ผ๋ฉด ์คํ ์ ๋ฐ์ดํธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค. ํ ํ๋ฆฟ์ ๋ด์ฉ์ ์์ ํ ๋ค Update Stack ๋ฒํผ์ ํด๋ฆญํ๊ณ ํด๋น ํ ํ๋ฆฟ์ ์ ํํ๋ฉด ๋ฉ๋๋ค. ์ค์ ํ๋ฉด์ ์คํ ์์ฑ๊ณผ ๋์ผํฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด CloudFormation ์คํ์ผ๋ก EC2 ์ธ์คํด์ค๋ฅผ 100๊ฐ ๋ง๋ ๋ค EC2 ์ธ์คํด์ค ์ ํ์ ๋ณ๊ฒฝํ๋ฉด, ์ EC2 ์ธ์คํด์ค 100๊ฐ๊ฐ ๋ชจ๋ ์์ฑ๋๊ณ ๋์ ๊ธฐ์กด EC2 ์ธ์คํด์ค 100๊ฐ๊ฐ ๋์์ ์ญ์ ๋ฉ๋๋ค.
์คํ ์ ๋ฐ์ดํธ ๊ธฐ๋ฅ์ ๋ง์ AWS ๋ฆฌ์์ค์ ์ค์ ์ ์ผ๊ด์ ์ผ๋ก ๋ณ๊ฒฝํ ๋ ์ ์ฉํฉ๋๋ค. ์ค์ ํ๋ ๊ฐ๋ฅผ ๋ณ๊ฒฝํ ๋์๋ AWS ์ฝ์์ ํด๋น AWS ๋ฆฌ์์ค ํ์ด์ง์์ ์ง์ ์ค์ ์ ๋ณ๊ฒฝํด๋ ๋ฉ๋๋ค.