로드밸런스 사용 이유?
여기까지가 2회차에 실습한 내용이다. (로드밸런스를 통해 사용자가 교대로 EC2 로 접근하면 된다.)
순서가 중요하지 않다면 표준 타입으로 유형을 설정한다.
구독을 생성한다.
주제 ARN(생성한 SNS 주제) - 프로토콜(이메일) - 엔드포인트(내 이메일 주소 snddjk...)
내가 메일을 확인해서 confirmed 눌러야 한다. -이후 SNS 구독 상태가 확인으로 변경됨.
웹 서버 인스턴스에 경보를 설정한다. (첫 번째 EC2)
첫 번째 인스턴스로 간다 작업 – 모니터링 및 문제 해결 – CloudWatch 경보 관리
경보 알림(AdminTopic-015) {조건 발생시 해당 토픽을 발행한다} - 경보 임계값
CPU 사용률을 30%로 변경 후 생성
로드밸런서를 통해 접근한 첫 번째 EC2에 LOAD TEST를 들어간다
AWSTemplateFormatVersion: 2010-09-09
Description: Make a VPC 1
Resources:
ToturialVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 172.0.0.0/16
EnableDnsHostnames: true
InternetGateway:
Type: AWS::EC2::InternetGateway
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref ToturialVPC (Ref : 내장함수)
InternetGatewayId: !Ref InternetGateway
PublicSubnet01:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ToturialVPC
CidrBlock: 172.0.0.0/24
AvailabilityZone: !Select
- '0' => 가용 영역의 첫 번째 값을 반환
- !GetAZs '' => 만약 서울 리전이라면 ap-northeast-2a, 2b, 2c, 2d를 반환
PrivateSubnet01:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ToturialVPC
CidrBlock: 172.0.1.0/24
AvailabilityZone: !Select
- '0'
- !GetAZs ''
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ToturialVPC
PublicRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet01
RouteTableId: !Ref PublicRouteTable
PrivateRouteTable: <= 인터넷 게이트웨이로의 라우팅을 별도로 정의, 연결하지 않음
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ToturialVPC
PrivateSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet01
RouteTableId: !Ref PrivateRouteTable
Outputs:
VPC:
Description: Toturial VPC ID
Value: !Ref ToturialVPC
AZ1:
Description: Availability Zone 1
Value: !GetAtt
- PublicSubnet01 => PublicSubnet01에 AZ를 결과물로 내놓는다.
- AvailabilityZone
https://docs.aws.amazon.com/ko_kr/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html
1. Ref : 지정된 파라미터 또는 리소스에 대한 정보를 반환한다. (Reference)
2. Fn::Seclect : 인덱스별 객체 목록에서 객체 하나를 반환
생성된 서브넷 subnet-052df8c996534ab31 / subnet-00bec86707efcac17
아래와 같이 기존 ymal 에서 수정(추가)하고 저장한다.
AWSTemplateFormatVersion: 2010-09-09
Description: Make a VPC 2
Resources:
ToturialVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 172.0.0.0/16
EnableDnsHostnames: true
InternetGateway:
Type: AWS::EC2::InternetGateway
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref ToturialVPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet02:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ToturialVPC
CidrBlock: 172.0.2.0/24
AvailabilityZone: !Select
- '2'
- !GetAZs ''
PrivateSubnet02:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ToturialVPC
CidrBlock: 172.0.3.0/24
AvailabilityZone: !Select
- '2'
- !GetAZs ''
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ToturialVPC
PublicRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet01
RouteTableId: !Ref PublicRouteTable
PublicSubnetRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet02
RouteTableId: !Ref PublicRouteTable
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ToturialVPC
PrivateSubnetRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet01
RouteTableId: !Ref PrivateRouteTable
PrivateSubnetRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet02
RouteTableId: !Ref PrivateRouteTable
Outputs:
VPC:
Description: Toturial VPC ID
Value: !Ref ToturialVPC
AZ1:
Description: Availability Zone 1
Value: !GetAtt
- PublicSubnet01
- AvailabilityZone
AZ1:
Description: Availability Zone 3
Value: !GetAtt
- PublicSubnet02
- AvailabilityZone
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template: Sample template that can be used to test EC2 updates. **WARNING** This template creates an Amazon Ec2 Instance. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
"InstanceType": { // 사용하려는 인스턴스의 타입 지정
"Description": "WebServer EC2 instance type",
"Type": "String",
"Default": "t2.nano",
"AllowedValues": [
"t1.micro",
"t2.nano",
"t2.micro",
"t2.small"
],
"ConstraintDescription": "must be a valid EC2 instance type."
}
},
"Mappings": { // 사용할 값에 대해 미리 정의한다.
"AWSInstanceType2Arch": {
"t1.micro": {
"Arch": "HVM64"
},
"t2.nano": {
"Arch": "HVM64"
},
"t2.micro": {
"Arch": "HVM64"
},
"t2.small": {
"Arch": "HVM64"
}
},
"AWSRegionArch2AMI": {
"us-east-1": {
"HVM64": "ami-0ff8a91507f77f867",
"HVMG2": "ami-0a584ac55a7631c0c"
},
"us-west-2": {
"HVM64": "ami-a0cfeed8",
"HVMG2": "ami-0e09505bc235aa82d"
},
"us-west-1": {
"HVM64": "ami-0bdb828fd58c52235",
"HVMG2": "ami-066ee5fd4a9ef77f1"
},
"eu-west-1": {
"HVM64": "ami-047bb4163c506cd98",
"HVMG2": "ami-0a7c483d527806435"
},
"eu-west-2": {
"HVM64": "ami-f976839e",
"HVMG2": "NOT_SUPPORTED"
},
"eu-west-3": {
"HVM64": "ami-0ebc281c20e89ba4b",
"HVMG2": "NOT_SUPPORTED"
},
"eu-central-1": {
"HVM64": "ami-0233214e13e500f77",
"HVMG2": "ami-06223d46a6d0661c7"
},
"ap-northeast-1": {
"HVM64": "ami-06cd52961ce9f0d85",
"HVMG2": "ami-053cdd503598e4a9d"
},
"ap-northeast-2": {
"HVM64": "ami-0a10b2721688ce9d2",
"HVMG2": "NOT_SUPPORTED"
},
"ap-northeast-3": {
"HVM64": "ami-0d98120a9fb693f07",
"HVMG2": "NOT_SUPPORTED"
},
"ap-southeast-1": {
"HVM64": "ami-08569b978cc4dfa10",
"HVMG2": "ami-0be9df32ae9f92309"
},
"ap-southeast-2": {
"HVM64": "ami-09b42976632b27e9b",
"HVMG2": "ami-0a9ce9fecc3d1daf8"
},
"ap-south-1": {
"HVM64": "ami-0912f71e06545ad88",
"HVMG2": "ami-097b15e89dbdcfcf4"
},
"us-east-2": {
"HVM64": "ami-0b59bfac6be064b78",
"HVMG2": "NOT_SUPPORTED"
},
"ca-central-1": {
"HVM64": "ami-0b18956f",
"HVMG2": "NOT_SUPPORTED"
},
"sa-east-1": {
"HVM64": "ami-07b14488da8ea02a0",
"HVMG2": "NOT_SUPPORTED"
},
"cn-north-1": {
"HVM64": "ami-0a4eaf6c4454eda75",
"HVMG2": "NOT_SUPPORTED"
},
"cn-northwest-1": {
"HVM64": "ami-6b6a7d09",
"HVMG2": "NOT_SUPPORTED"
}
}
},
"Resources": {
"WebServerInstance": {
"Type": "AWS::EC2::Instance", // 인스턴스 생성
"Metadata": {
"Comment": "Install a simple PHP application",
"AWS::CloudFormation::Init": {
"config": {
"packages": { // 사전 패키징된 앱 및 구성 요소를 다운로드 및 설치 예를 들면 아래에서 "/etc/cfn/cfn-hup.conf" 라는 파일 다운로드시켜준다.
"yum": {
"httpd": [],
"php": []
}
},
"files": { // EC2 인스턴스에 파일을 생성한다
"/var/www/html/index.php": {
"content": {
"Fn::Join": [ // 생성하는 파일은 이 내용을 포함한다.
"",
[
"<?php\n",
"echo '<h1>AWS CloudFormation sample PHP application</h1>';\n",
"?>\n"
]
]
},
"mode": "000644",
"owner": "apache",
"group": "apache"
},
"/etc/cfn/cfn-hup.conf": {
"content": {
"Fn::Join": [
"",
[
"[main]\n",
"stack=",
{
"Ref": "AWS::StackId"
},
"\n",
"region=",
{
"Ref": "AWS::Region"
},
"\n"
]
]
},
"mode": "000400", // 실행권한을 가진다.
"owner": "root",
"group": "root"
},
"/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
"content": {
"Fn::Join": [
"",
[
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init\n",
"action=/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"runas=root\n"
]
]
}
}
},
"services": { // 인스턴스 실행 시 활성/비활성 되는 서비스를 정의한다.
"sysvinit": {
"httpd": {
"enabled": "true",
"ensureRunning": "true"
},
"cfn-hup": {
"enabled": "true",
"ensureRunning": "true",
"files": [
"/etc/cfn/cfn-hup.conf",
"/etc/cfn/hooks.d/cfn-auto-reloader.conf"
]
}
}
}
}
}
},
"Properties": { // 웹 서비스가 가질 수 있는 속성들
"ImageId": {
"Fn::FindInMap": [ // 웹에서 데이터를 가져오는 함수, 데이터를 가져올 때, [어떤 리전]에 따라 [무엇을 할지] 결정한다.
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region" // 위에서 "ARSRedgionArch2AMI" 에서 정의한 값을 가져올 때 정하는 기준, 만약 ap-northeast-1 를 사용한다면 하위 "HVM64": "ami-06cd52961ce9f0d85", "HVMG2": "ami-053cdd503598e4a9d" 값들이 리턴되는 것.
},
{
"Fn::FindInMap": [ // 사용자의 [AWSInstanceType2Arch]에 해당하는 값에 따라 Arch의 값을 반환한다. 즉, "t1.micro" 이면, "Arch": "HVM64" 을 가져오는 것이다.
"AWSInstanceType2Arch",
{
"Ref": "InstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroups": [
{
"Ref": "WebServerSecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"yum install -y aws-cfn-bootstrap\n", // AWS에서 제공하는 유틸리티 패키지로, EC2 인스턴스를 프로비저닝, 초기화 하기 위한 도구.
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init -v ", // EC2 초기 설정 관리
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"# Start up the cfn-hup daemon to listen for changes to the Web Server metadata\n",
"/opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup'\n",
"# Signal the status from cfn-init\n",
"/opt/aws/bin/cfn-signal -e $? ", // EC2 인스턴스가 성공적으로 초기화 되었음을 CloudFormation 스택에 알림
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT5M"
}
}
},
"WebServerSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable HTTP access via port 80",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
}
]
}
}
},
"Outputs": {
"WebsiteURL": {
"Description": "Application URL",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"WebServerInstance",
"PublicDnsName"
]
}
]
]
}
}
}
}
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template: Sample template that can be used to test EC2 updates. **WARNING** This template creates an Amazon Ec2 Instance. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
"InstanceType": {
"Description": "WebServer EC2 instance type",
"Type": "String",
"Default": "t2.nano",
"AllowedValues": [
"t1.micro",
"t2.nano",
"t2.micro",
"t2.small"
],
"ConstraintDescription": "must be a valid EC2 instance type."
},
"SSHLocation": {
"Description": "SSH 접속을 허용할 IP 대역",
"Type": "String",
"MaxLength": "18",
"MinLength": "9",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "IP CIDR 형식(x.x.x.x/x)을 입력하세요."
},
"KeyName": {
"Description": "SSH 접속에 사용할 키페어 이름",
"Type": "AWS::EC2::KeyPair::KeyName"
}
},
"Mappings": {
"AWSInstanceType2Arch": {
"t1.micro": {
"Arch": "HVM64"
},
"t2.nano": {
"Arch": "HVM64"
},
"t2.micro": {
"Arch": "HVM64"
},
"t2.small": {
"Arch": "HVM64"
}
},
"AWSRegionArch2AMI": {
"us-east-1": {
"HVM64": "ami-0ff8a91507f77f867",
"HVMG2": "ami-0a584ac55a7631c0c"
},
"us-west-2": {
"HVM64": "ami-a0cfeed8",
"HVMG2": "ami-0e09505bc235aa82d"
},
"us-west-1": {
"HVM64": "ami-0bdb828fd58c52235",
"HVMG2": "ami-066ee5fd4a9ef77f1"
},
"eu-west-1": {
"HVM64": "ami-047bb4163c506cd98",
"HVMG2": "ami-0a7c483d527806435"
},
"eu-west-2": {
"HVM64": "ami-f976839e",
"HVMG2": "NOT_SUPPORTED"
},
"eu-west-3": {
"HVM64": "ami-0ebc281c20e89ba4b",
"HVMG2": "NOT_SUPPORTED"
},
"eu-central-1": {
"HVM64": "ami-0233214e13e500f77",
"HVMG2": "ami-06223d46a6d0661c7"
},
"ap-northeast-1": {
"HVM64": "ami-06cd52961ce9f0d85",
"HVMG2": "ami-053cdd503598e4a9d"
},
"ap-northeast-2": {
"HVM64": "ami-0a10b2721688ce9d2",
"HVMG2": "NOT_SUPPORTED"
},
"ap-northeast-3": {
"HVM64": "ami-0d98120a9fb693f07",
"HVMG2": "NOT_SUPPORTED"
},
"ap-southeast-1": {
"HVM64": "ami-08569b978cc4dfa10",
"HVMG2": "ami-0be9df32ae9f92309"
},
"ap-southeast-2": {
"HVM64": "ami-09b42976632b27e9b",
"HVMG2": "ami-0a9ce9fecc3d1daf8"
},
"ap-south-1": {
"HVM64": "ami-0912f71e06545ad88",
"HVMG2": "ami-097b15e89dbdcfcf4"
},
"us-east-2": {
"HVM64": "ami-0b59bfac6be064b78",
"HVMG2": "NOT_SUPPORTED"
},
"ca-central-1": {
"HVM64": "ami-0b18956f",
"HVMG2": "NOT_SUPPORTED"
},
"sa-east-1": {
"HVM64": "ami-07b14488da8ea02a0",
"HVMG2": "NOT_SUPPORTED"
},
"cn-north-1": {
"HVM64": "ami-0a4eaf6c4454eda75",
"HVMG2": "NOT_SUPPORTED"
},
"cn-northwest-1": {
"HVM64": "ami-6b6a7d09",
"HVMG2": "NOT_SUPPORTED"
}
}
},
"Resources": {
"WebServerInstance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"Comment": "Install a simple PHP application",
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"yum": {
"httpd": [],
"php": []
}
},
"files": {
"/var/www/html/index.php": {
"content": {
"Fn::Join": [
"",
[
"<?php\n",
"echo '<h1>AWS CloudFormation sample PHP application</h1>';\n",
"echo '새로운 내용을 추가한 스택';\n",
"?>\n"
]
]
},
"mode": "000644",
"owner": "apache",
"group": "apache"
},
"/etc/cfn/cfn-hup.conf": {
"content": {
"Fn::Join": [
"",
[
"[main]\n",
"stack=",
{
"Ref": "AWS::StackId"
},
"\n",
"region=",
{
"Ref": "AWS::Region"
},
"\n"
]
]
},
"mode": "000400",
"owner": "root",
"group": "root"
},
"/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
"content": {
"Fn::Join": [
"",
[
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init\n",
"action=/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"runas=root\n"
]
]
}
} },
"services": {
"sysvinit": {
"httpd": {
"enabled": "true",
"ensureRunning": "true"
},
"cfn-hup": {
"enabled": "true",
"ensureRunning": "true",
"files": [
"/etc/cfn/cfn-hup.conf",
"/etc/cfn/hooks.d/cfn-auto-reloader.conf"
]
}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region"
},
{
"Fn::FindInMap": [
"AWSInstanceType2Arch",
{
"Ref": "InstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"SecurityGroups": [
{
"Ref": "WebServerSecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"yum install -y aws-cfn-bootstrap\n",
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"# Start up the cfn-hup daemon to listen for changes to the Web Server metadata\n",
"/opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup'\n",
"# Signal the status from cfn-init\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WebServerInstance ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT5M"
}
}
},
"WebServerSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable HTTP access via port 80",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": { "Ref": "SSHLocation" }
}
]
}
}
},
"Outputs": {
"WebsiteURL": {
"Description": "Application URL",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"WebServerInstance",
"PublicDnsName"
]
}
]
]
}
}
}
}
이제 이 EC2에 Bitvise SSH 프로그램을 사용해 접속해 보자.
Client key manager 에서 나의 키페어를 등록해 준다. (import 로 가져온다)
아래 그림처럼 Server, Authentication, 키페어를 가지고 Log in 버튼을 누른다. 성공적으로 시작하면, New Terminal console 창이 나온다. 해당 아이콘을 클릭해보자.