AWS CloudFormation(Azure Resource Manager,GCP Deployment Manager)
Terraform의 이해를 이해 먼저 AWS CloudFormation을 사용해본다. 단, 실무에서는 범용성의 이유로 CloudFormation보단 Terraform을 사용함을 기억하자.
주요 섹션
Resources(생성)
AWS 인프라의 실질적인 섹션이다. EC2 인스턴스, S3 버킷, ELB등과 같은 클라우드 포메이션을 이용해 AWS 웹 콘솔에서 실행하는 것으로 거의 모든 리소스 유형을 생성할 수 있다. 하지만 신규 또는 최첨단의 AWS 리소스는 즉시 제공되지 않는 경우가 종종 있다. 리소스에는 기본 반환값이 있다. Ref를 이용해 이 반환값을 얻어올 수 있고 템플릿의 다른 위치에 사용할 수 있다. 예를 들어 AWS::EC2::VPC 리소스 유형은 기본 반환값을 갖고 있고 이 값은 VPC의 ID 이다.
Parameters(입력)
명령줄 도구에 입력하는 매개변수와 동일하게 스택을 만들거나 업데이트할 때 정의하는 입력값이다. 파라미터는 템플릿의 변경 없이도 스택을 커스터마이즈할 수 있게 해준다. AMI ID, VPC ID, Subnet ID등과 같은 매개변수를 사용할 수 있다.
Output(출력)
스택이 완료된 후에 결과물을 출력하려고 할때 유용다. 예를 들어 ELB의 퍼블릭 URL이나 EC2의 퍼블릭 IP를 출력할 수 있다.
Mapping(지정)
리전의 특화된 템플릿에서 어떠한 요소를 참조할 때 필요하다. 예를 들어 템플릿에 EC2 AMI ID에 대한 매핑을 지정하는 것이다. AMI ID가 리전에 특화된 리소스이기 때문에 유효한 AMI ID를 리전별로 지정하려고 할때 사용한다.
AWS CloudFormation
서울 리전 VPC 스택 생성
# 새 메모장에 다음 내용을 입력하고 'test-vpc.yaml'로 저장
AWSTemplateFormatVersion: 2010-09-09 // 버전 날짜. 선택사항임
Resources:
VPC: // VPC(논리적 아이디)
Type: AWS::EC2::VPC // AWS - EC2 - VPC타입 (대소문자 구분)
Properties:
CidrBlock: 192.168.0.0/16 // 아이피 대역
EnableDnsSupport: true // DNS 확인 활성화
EnableDnsHostnames: true // DNS 호스트 이름 활성화
InstanceTenancy: default
Tags: // 태그
- Key: Name
Value: test-vpc
PUB2A: // PUB2A
Type: AWS::EC2::Subnet // AWS - EC2 - 서브넷 타입
Properties:
AvailabilityZone: ap-northeast-2a // 가용 영역
VpcId: !Ref VPC // !Ref를 통해 생성한 리소스 VPC의 아이디를 가져옴
CidrBlock: 192.168.0.0/20 // 대역폭
MapPublicIpOnLaunch: true // DHCP 할당(퍼블릭 아이피)
Tags: // 태그
- Key: Name
Value: test-pub-2a
PUB2B:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2b
VpcId: !Ref VPC
CidrBlock: 192.168.16.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-2b
PUB2C:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2c
VpcId: !Ref VPC
CidrBlock: 192.168.32.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-2c
PUB2D:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2d
VpcId: !Ref VPC
CidrBlock: 192.168.48.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-2d
PVT2A:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2a
VpcId: !Ref VPC
CidrBlock: 192.168.64.0/20
MapPublicIpOnLaunch: false // 프라이빗이므로 퍼블릭 아이피 불필요
Tags:
- Key: Name
Value: test-pvt-2a
PVT2B:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2b
VpcId: !Ref VPC
CidrBlock: 192.168.80.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-2b
PVT2C:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2c
VpcId: !Ref VPC
CidrBlock: 192.168.96.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-2c
PVT2D:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2d
VpcId: !Ref VPC
CidrBlock: 192.168.112.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-2d
InternetGateway: // InternetGateway
Type: AWS::EC2::InternetGateway // AWS - EC2 - 인터넷 게이트웨이
Properties:
Tags:
- Key: Name
Value: test-igw
VPCGatewayAttachment: // VPCGatewayAttachment
Type: AWS::EC2::VPCGatewayAttachment // VPC 게이트웨이 연결
Properties:
VpcId: !Ref VPC // VPC 아이디
InternetGatewayId: !Ref InternetGateway // 게이트웨이 아이디
PUBRTB: // PUBRTB
Type: AWS::EC2::RouteTable // 라우팅 테이블
Properties:
VpcId: !Ref VPC // VPC 아이디
Tags:
- Key: Name
Value: test-pub-rtb
PVTRTB: // PVTRTB
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: test-pvt-rtb
InternetRoute: // InternetRoute
Type: AWS::EC2::Route // 라우팅 정보
DependsOn: InternetGateway // 인터넷 게이트웨이와 연결
Properties:
DestinationCidrBlock: 0.0.0.0/0 // 모든 아이피
GatewayId: !Ref InternetGateway // 인터넷 게이트웨이 아이디
RouteTableId: !Ref PUBRTB // 라우팅 테이블 아이디
PUB2ARTBAssociation: // PUB2RTBAssociation
Type: AWS::EC2::SubnetRouteTableAssociation // 서브넷 라우팅 테이블 명시적 연결
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB2A
PUB2BRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB2B
PUB2CRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB2C
PUB2DRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB2D
PVT2ARTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT2A
PVT2BRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT2B
PVT2CRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT2C
PVT2DRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT2D









서울 리전 EC2 스택 생성
# 텍스트 파일을 이용해서 test-ec2.yaml 만들기
AWSTemplateFormatVersion: 2010-09-09
Mappings: // 맵핑
RegionMap:
ap-northeast-2:
AMIID: ami-035da6a0773842f64 // 서울 리전의 이미지 아이디
ap-northeast-1:
AMIID: ami-08928044842b396f0 // 도쿄 리전의 이미지 아이디
Parameters:
InstanceType: // 인스턴스 유형
Type: String
Default: t2.micro
Description: Enter instance size. Default is t2.micro
VPC:
Type: String
Default: vpc-05292160232b4e265 // VPC 아이디 (콘솔 or CLI로 test-vpc 아이디 확인)
Description: VPC ID.
Subnet:
Type: String
Default: subnet-09d8b8ed61e8a7e27 // 서브넷 아이디 (pub-2a)
Description: Subnet ID.
AMI:
Type: String
Default: AMIID // 맵핑했던 리전에 맞는 AMIID로 지정됨.
Description: The Linux AMI to use.
Key:
Type: String
Default: test-key // 키 페어
Description: The key used to access the instance.
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup // AWS - EC2 - 보안 그룹
Properties:
GroupName: "test-sg-web" // 보안 그룹 이름
GroupDescription: "test-sg-web" // 설명
VpcId: !Ref VPC // VPC 아이디 가져옴
SecurityGroupIngress: // 인바운드 규칙
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 내 아이피/32 // 내 아이피(공인)
- IpProtocol: icmp
FromPort: '-1' // 모든 포트
ToPort: '-1' // 모든 포트
CidrIp: 0.0.0.0/0
SecurityGroupEgress: // 아웃바운드 규칙
- IpProtocol: -1 // 모든 프로토콜
CidrIp: 0.0.0.0/0 // 모든 아이피
EC2Insteance:
Type: AWS::EC2::Instance // AWS - EC2 - 인스턴스
Properties:
SubnetId: !Ref Subnet // 서브넷 아이디 참조
# ImageId: !Ref AMI // 이미지 아이디 참조(주석 처리, 아래 함수를 이용)
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", !Ref AMI ] // 함수: 사용자가 있는 리전과 일치하는 AMI 아이디 가져오기
InstanceType:
Ref: InstanceType // 인스턴스 타입 참조 (가독성을 위해 두줄로 나타낼 경우, 들여쓰고 느낌표 x)
KeyName: !Ref Key
SecurityGroupIds:
- Ref: InstanceSecurityGroup // '-' 이용 가능
BlockDeviceMappings:
- DeviceName: /dev/xvda // 동일한 여러 항목의 경우에는 '-'를 이용
Ebs:
VolumeSize: 8
- DeviceName: /dev/xvdb
Ebs:
VolumeSize: 8
Tags:
- Key: Name
Value: test-ec2
UserData:
Fn::Base64: | // 유저데이터를 파일이 아닌 Fn(Function 함수)를 이용해서 직접 커맨드로 처리. ' | '를 넣어야 개행이 입력됨
#cloud-boothook
#!/bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "Hello World!" > /var/www/html/index.html
Outputs: // 출력
PublicIp:
Description: PublicIp Output
Value: {"Fn::GetAtt": ["EC2Insteance","PublicIp"]} // Fn(함수)로 EC2인스턴스와 퍼블릭 아이피 값을 가져옴








도쿄 리전 VPC 스택 생성
# 텍스트 파일로 test-vpc-tokyo.yaml 생성
AWSTemplateFormatVersion: 2010-09-09
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 192.168.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: Name
Value: test-vpc
PUB1A:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1a
VpcId: !Ref VPC
CidrBlock: 192.168.0.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-1a
PUB1C:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1c
VpcId: !Ref VPC
CidrBlock: 192.168.32.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-1c
PUB1D:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1d
VpcId: !Ref VPC
CidrBlock: 192.168.48.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: test-pub-1d
PVT1A:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1a
VpcId: !Ref VPC
CidrBlock: 192.168.64.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-1a
PVT1C:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1c
VpcId: !Ref VPC
CidrBlock: 192.168.96.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-1c
PVT1D:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1d
VpcId: !Ref VPC
CidrBlock: 192.168.112.0/20
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: test-pvt-1d
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: test-igw
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PUBRTB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: test-pub-rtb
PVTRTB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: test-pvt-rtb
InternetRoute:
Type: AWS::EC2::Route
DependsOn: InternetGateway
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref PUBRTB
PUB1ARTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB1A
PUB1CRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB1C
PUB1DRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PUBRTB
SubnetId: !Ref PUB1D
PVT1ARTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT1A
PVT1CRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT1C
PVT1DRTBAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PVTRTB
SubnetId: !Ref PVT1D





도쿄 리전 EC2 스택 생성
# 도쿄 리전에 키 생성 (현재 aws configure가 서울인데, '--region'옵션을 통해 도쿄에 생성)
aws ec2 create-key-pair --region ap-northeast-1 --key-name tokyo-key --query 'KeyMaterial' --output text > tokyo-key.pem

# 텍스트 파일로 'test-ec2-tokyo.yaml' 생성
AWSTemplateFormatVersion: 2010-09-09
Mappings:
RegionMap:
ap-northeast-2:
AMIID: ami-035da6a0773842f64
ap-northeast-1:
AMIID: ami-08928044842b396f0
Parameters:
InstanceType:
Type: String
Default: t2.micro
Description: Enter instance size. Default is t2.micro
VPC:
Type: String
Default: vpc-05292160232b4e265
Description: VPC ID.
Subnet:
Type: String
Default: subnet-01132b9dddcf71d3d
Description: Subnet ID.
AMI:
Type: String
Default: AMIID
Description: The Linux AMI to use.
Key:
Type: String
Default: tokyo-key
Description: The key used to access the instance.
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "test-sg-web"
GroupDescription: "test-sg-web"
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 123.142.252.25/32
- IpProtocol: icmp
FromPort: '-1'
ToPort: '-1'
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
EC2Insteance:
Type: AWS::EC2::Instance
Properties:
SubnetId: !Ref Subnet
# ImageId: !Ref AMI
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", !Ref AMI ]
InstanceType:
Ref: InstanceType
KeyName: !Ref Key
SecurityGroupIds:
- Ref: InstanceSecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 8
- DeviceName: /dev/xvdb
Ebs:
VolumeSize: 8
Tags:
- Key: Name
Value: test-ec2
UserData:
Fn::Base64: |
#cloud-boothook
#!/bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "Hello Tokyo!" > /var/www/html/index.html
Outputs:
PublicIp:
Description: PublicIp Output
Value: {"Fn::GetAtt": ["EC2Insteance","PublicIp"]}







스택 삭제

이런 식으로, 클라우도 포메이션을 삭제하면, 클라우도 포메이션으로 생성됐던 모든 것들이 같이 사라진다.(삭제가 편리함)





Output(출력)
스택이 완료된 후에 결과물을 출력하려고 할때 유용다. 예를 들어 ELB의 퍼블릭 URL이나 EC2의 퍼블릭 IP를 출력할 수 있다.
Mapping(지정)
리전의 특화된 템플릿에서 어떠한 요소를 참조할 때 필요하다. 예를 들어 템플릿에 EC2 AMI ID에 대한 매핑을 지정하는 것이다. AMI ID가 리전에 특화된 리소스이기 때문에 유효한 AMI ID를 리전별로 지정하려고 할때 사용한다.
IT 인프라의 유형
전통적인 인프라
전통전인 인프라의 경우는 데이터센터, 데이터 스토리지(구글 드라이브 같은 오브젝트 스토리지)를 비롯한 구성 요소 모두를 기업의 자체 시설에서 소유하고 관리하는 것을 의미한다.
이 경우를 On - Premise (온 프레미스)라고 부른다.
초기 비용이 들기에, 비용이 많이 들고, 공간이 필요하고 직접 관리해야한다.
- On - Premise (온 프레미스) : '구내에서 켜져있다'라는 의미 그대로, 회사의 전산실 내부에 라우터, 스위치 같은 각종 IT 장비들이 전원이 켜진채로 있다는 것.
- Off - Premise (온 프레미스) : '구내에서 꺼져있다'라는 의미 그대로, 회사에 직접 전원을 키고 운영하는 것이 아니라, 회사 외부에서 IT 장비를 둔 것을 의미한다. (클라우드가 이에 해당한다)
클라우드 인프라
클라우드 컴퓨팅에 필요한 구성 요소와 리소스를 뜻한다. 전용 리소스를 사용해 프라이빗(Private) 클라우드를 자체적으로 구축하거나 대표적인 CSP(Cloud Service Provider)인 Alibaba, Amazon, Google, IBM, Microsoft 등 클라우드 제공업체의 클라우드 인프라를 대여해 퍼블릭(Public) 클라우드를 사용할 수 도 있다.
더 나아가, 여러 클라우드 끼리 연결하여 멀티 클라우드를 구성하거나, 프라이빗 클라우드와 퍼블릭 클라우드를 연결하여 하이브리드 클라우드를 구축할 수도 있다.

Infrastructure : 클라우드의 Core - Infra(핵심) 이다.
- Compute : CPU, 메모리
- Block Storage : SSD(메모리 방식), HDD(물리적 방식)
- Network : IP
- Public IP : 공인
- Private IP : 사설
나머지 Platform 과 Application은 선택적인 서비스이다.
하이퍼컨버지드 인프라 ( HCI )
가상화에 기반을 둔 것이 가장 큰 특징이다. 가상화를 통해 분산되어 있던 하드웨어들을 하나로 묶으므로, 단일 인터페이스에서 컴퓨팅, 네트워크, 데이터 스토리지 리소스를 관리할 수 있다.
이 때, 하드웨어 각각의 자투리 공간들을 모아 하나로 묶는 것을 Pool (공유)이라 한다.
복잡하고 비용이 많이 드는 레거시 인프라는 표준 서버에서 실행되는 플랫폼으로 대체된다.(효율적으로)

'메가존 클라우드 2기 교육 > 실무 특화' 카테고리의 다른 글
Ansible - CLI, 플레이북 (1) | 2023.05.22 |
---|---|
Terraform - GCP에서의 사용 (0) | 2023.05.19 |
Terraform - 모듈식 생성, ALB와 ASG, NAT + RDS 생성 (0) | 2023.05.17 |
Terraform - 설치 및 사용 (1) | 2023.05.16 |
Iac(코드형 인프라) 개념 및 환경 세팅, AWS CLI로 인스턴스 생성 (1) | 2023.05.15 |