Cloud/AWS
원본 글 보기

[AWS] CloudFormation으로 Auto Scaling 그룹 만들기

[AWS] CloudFormation으로 Auto Scaling 그룹 만들기

[ Amazon Web Services ]

AWS CloudFormation

-3-

auto.yml 파일



Description: A sample template
Resources:
MyTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
HealthCheckEnabled: true
HealthCheckIntervalSeconds: 30
HealthCheckPath: /
HealthCheckPort: 80
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 5
Matcher:
HttpCode: 200
Name: MyTargetGroup
      Port: 80
Protocol: HTTP
TargetType: instance
      UnhealthyThresholdCount: 2
VpcId: 본인 vpc ID
StackELB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
IpAddressType: ipv4
      Name: stack-elb
      SecurityGroups:
- 본인 보안그룹
      Subnets:
- 본인 subnet ID
- 본인 subnet ID
- 본인 subnet ID
- 본인 subnet ID
Type: application

  HTTPlistener:
Type: "AWS::ElasticLoadBalancingV2::Listener"
DependsOn:
- MyTargetGroup
     - StackELB
    Properties:
DefaultActions:
- Type: "forward"
ForwardConfig:
TargetGroups:
- TargetGroupArn: !Ref MyTargetGroup
      LoadBalancerArn: !Ref StackELB
      Port: 80
Protocol: "HTTP"
MyEC2Template:
Type: AWS::EC2::LaunchTemplate
    Properties:
LaunchTemplateName: MyEC2Template
      LaunchTemplateData:
BlockDeviceMappings:
- DeviceName: "/dev/xvda"
Ebs:
DeleteOnTermination: 'true'
VolumeSize: 8
VolumeType: gp2
        ImageId: ami-0ba5cd124d7a79612
        InstanceType: t2.micro
        KeyName: 본인 키페어
        SecurityGroupIds:
- 본인 보안그룹
        UserData:
Fn::Base64: !Sub |
            #!/bin/bash
            apt-get -y update
            apt-get install -y apache2
            systemctl start apache2

  MyASG:
Type: AWS::AutoScaling::AutoScalingGroup
    DependsOn:
- MyEC2Template
     - StackELB
     - MyTargetGroup
    Properties:
AutoScalingGroupName: MyASG
      LaunchTemplate:
LaunchTemplateName: MyEC2Template
        Version: !GetAtt MyEC2Template.LatestVersionNumber
      MaxSize: 3
MinSize: 1
TargetGroupARNs:
- !Ref MyTargetGroup
      VPCZoneIdentifier:
- 본인 서브넷 아이디
        - 본인 서브넷 아이디
        - 본인 서브넷 아이디
        - 본인 서브넷 아이디

  MyCPUPolicy:
Type: AWS::AutoScaling::ScalingPolicy
    DependsOn:
- MyASG
    Properties:
AutoScalingGroupName: !Ref MyASG
      PolicyType: TargetTrackingScaling
      TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
        TargetValue: 50


인스턴스 접속 후,

CPU 부하 프로그램 stress 설치하고 stress 줌.



sudo su - root

apt-get update
apt-get install stress

# cpu 코어 1개 100% 사용하겠다는 명령어
stress -c 1

cloudwatch 탭에서 부하 정도 확인:

CPU 그래프가 갑자기 100까지 오르고,

인스턴스 자동 생성.

부하 끄면 다시 인스턴스가 줄어들고,

스택을 삭제하면 인스턴스 및 만들어진 것 모두 종료된다.