[AWS] 오토스케일링 PHP SDK 제어 및 현황 확인
현재 AWS의 오토스케일링 현황을 볼 수 있는 SDK 입니다.
필요에 따라서 문제가 있을것으로 추정 되면 종료 시켜서 자동으로 새로 생성 되게 할 수 있습니다.
원하는용량 - 이것은 한번에 생성 시킬 인스턴스를 말합니다. 보통 1을 지정하기도 하는데 이보다는 부하가 있기 때문에 생성 시키는 것으로 최소 2이상을 주는 것이 더 좋겠습니다.
아래 클래스는 오토스케일링 PHP SDK 현황 확인을 하는 것이며 아래쪽에 있는것은 종료 시키는 것입니다.
class AwsAutoScaling
{
private $client;
function __construct( $aAwsProfile ) {
$client = new AutoScalingClient($aAwsProfile);
$this->client = $client;
}
function describeAutoScalingGroups( $aGroupNames=array() )
{
$client = $this->client;
$result = $client->describeAutoScalingGroups();
foreach ($result['AutoScalingGroups'] as $aGroup) {
extract($aGroup);
echo "그룹명: $AutoScalingGroupName 최소: $MinSize 최대: $MaxSize 한번에생성되는갯수:$DesiredCapacity " . PHP_EOL;
foreach ($aGroup['Instances'] as $instance) {
if ( !isset($instance["HealthStatus"]) ) $instance["HealthStatus"] = '';
extract($instance);
echo " - 인스턴스Id: $InstanceId - LifecycleState: $LifecycleState - HealthStatus: $HealthStatus" . PHP_EOL;
echo "\n\n";
}
}
}
}
# 현재 오토스케일링 상태보기
$oAutoScaling = new AwsAutoScaling($aAwsProfile);
$oAutoScaling->describeAutoScalingGroups();
# 아래는 오토스케일링에 있는 인스턴스를 Unhealthy
$instance = 'i-01051abaea3d474e6';
$client = new AutoScalingClient($aAwsProfile);
# 헬스상태를 변경 Healthy, Unhealthy - AWS에서는 대상에서 볼 수 있다.
# ShouldRespectGracePeriod: 호출 유예기간을 따르겠냐?
$result = $client->setInstanceHealth([
'HealthStatus' => 'Unhealthy', // REQUIRED
'InstanceId' => $instance, // REQUIRED
'ShouldRespectGracePeriod' => true ,
]);
print_r($result);
# EC2 - 해당 인스턴스를 '종료'
# ShouldDecrementDesiredCapacity: 원하는 용량으로 감소 시킬거냐. 디폴트값은 true
$result = $client->terminateInstanceInAutoScalingGroup([
'InstanceId' => $instance, // REQUIRED
'ShouldDecrementDesiredCapacity' => true ,
]);
print_r($result);
* 같이 참고하면 좋은것
- [AWS] 어플리케이션 로드밸런서 PHP SDK > AWS.클라우드
위에서 프로파일 부분은 위 링크에 있는것을 참고 하면 됩니다.
- [AWS] 쉬운 EC2 생성을 위한 시작템플릿 작성하기 > AWS.클라우드
- [AWS] 오토스케일링 설정방법 - EC2 자동삭제/생성 > AWS.클라우드