AWS CloudFormation을 사용한 인프라 배포 자동화
▶ CloudFormation 템플릿 배포
step 1) VPC 네트워킹 계층을 만드는 yml 파일 작성 <aws 템플릿 참고>
링크 : docs.aws.amazon.com/ko_kr/AWSCloudFormation/latest/UserGuide/template-guide.html
Resources:
# VPC with Internet Gateway
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 192.168.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: bluevpc
IGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: blueigw
VPCtoIGWConnection:
Type: AWS::EC2::VPCGatewayAttachment
DependsOn:
- IGW
- VPC
Properties:
InternetGatewayId: !Ref IGW
VpcId: !Ref VPC
# Public Route Table
PublicRouteTable:
Type: AWS::EC2::RouteTable
DependsOn: VPC
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: Public Route Table
PublicRoute:
Type: AWS::EC2::Route
DependsOn:
- PublicRouteTable
- IGW
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IGW
RouteTableId: !Ref PublicRouteTable
# Public Subnet
PublicSubnet:
Type: AWS::EC2::Subnet
DependsOn: VPC
Properties:
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
CidrBlock: 192.168.0.0/24
AvailabilityZone: !Select
- 0
- !GetAZs
Ref: AWS::Region
Tags:
- Key: Name
Value: Public_subnet
PublicRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
DependsOn:
- PublicRouteTable
- PublicSubnet
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet
# App Security Group
AppSecurityGroup:
Type: AWS::EC2::SecurityGroup
DependsOn: VPC
Properties:
GroupName: Server_SG
GroupDescription: Enable access to Server
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: web_SG
step 2) CloudFormation 스택 생성
스택 생성 -> 준비된 템플릿 -> 템플릿 파일 업로드
-> 스택 이름 생성(나중에 다른 스택이 참조 할 수 있다) -> 태그 구성 -> 스택 생성
step 3) 스택 이벤트에서 CloudFormation에서 실행되는 활동들을 확인 할 수 있다.
확인 :
VPC, 서브넷 .. 전부 제대로 설치 된것을 확인 할 수 있다.
▶ 기존 CloudFormatiom 스택 참조 & 활용
step 1) 웹 서버를 생성하는 인스턴스 yml파일 작성
- Parameters에 NetworkStackName을 꼭 지정해 주어야 blue-network 템플릿의 스택값을 지금 생성하고 있는 템플릿이 참조할 수 있다.
Parameters:
NetworkStackName:
Type: String
Default: blue-network
AmazonLinuxAMIID:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
WebServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable access to http
VpcId:
Fn::ImportValue:
!Sub ${NetworkStackName}-VPCID
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value:
Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref AmazonLinuxAMIID
SubnetId:
Fn::ImportValue:
!Sub ${NetworkStackName}-SubnetID
SecurityGroupIds:
- !Ref WebServerSecurityGroup
Tags:
- Key: Name
Value: web server
step 2) CloudFormation 스택 생성
스택 생성 -> 준비된 템플릿 -> 템플릿 파일 업로드
스택 이름 생성 -> 파라미터 (NetworkStackName이 참조할 스택이름을 표기하고 있다)
step 3) 스택 이벤트에서 CloudFormation에서 실행되는 활동들을 확인 할 수 있다.
확인 :
▶ 스택 업데이트
step 1) 기존 네트워크 템플릿에 보안 그룹 하나만 추가해본다.
step 2) 업데이트할 스택 선택 -> 업데이트 -> 현재 템플릿 교체 -> 템플릿 파일 업로드 -> 스택 업데이트 -> 확인해봄
전부 설치되면 UPDATE_COMPLETE라고 뜬다.
업데이트 전:
업데이트 후:
▶ CloudFormation Designer
AWS CloudFormation Designer은 AWS CloudFormation 템플릿을 작성, 확인, 수정하기 위한 그래픽 도구이다. Designer를 사용하면 드래그 앤 드롭 인터페이스를 사용하여 템플릿 리소스를 다이어그램으로 표시한 후 통합된 JSON및 YAML 편집기를 사용하여 해당 세부 정보를 편집할 수 있다.
step 1) Designer -> 템플릿 열기 -> 로컬 파일 -> 탬플릿 선택
아래와 같이 표시 된다.
▶ 스택 삭제 & 삭제 정책
스택 삭제시 Deletion Police(삭제 정택)을 사용하면 스택이 삭제될 때 생성되었던 리소스를 보존하거나 백업할 수 잇는 리소스에 대해서도 지정할 수 있습니다.
step 1) 웹 서버 설정 탬플릿에 아래 행을 추가 한 후 다시 업데이트 한다.
DiskVolume:
Type: AWS::EC2::Volume
Properties:
Size: 100
AvailabilityZone: ap-northeast-1a
DeletionPolicy: Snapshot
step 2) 웹 서버 스택을 삭제 해 본다.
step 3) 웹 서버 스택이 삭제 되자 웹 서버도 함께 삭제 되는 것을 확인 할 수 있다.
step 4) EC2의 스냅샷에 가보면 DeletionPolicy에 의해 생성된 스냅샷을 확인 할 수 있다.