aws cli 명령어를 이용하여 ec2 서버를 껏다 켰다 하는 스크립트 이다.
일반 환경에서는 불필요 하고 개발 환경의 서버를 24/7 을 켜둘 이유가 없다.
개발자의 개발 환경을 고려하여 월~금 사이 AM 8:50 ~ PM 18:30 까지 만 사용 한다면 1일에 10시간만 사용함으로서 청구금액을 낮출 수 있다.
t3.medium ( 2 Core / 4G-RAM ) = 시간당 US$ 0.0416
0.0416 * 24 * 365 = $364
0.0416 * 10 * (365 / 7 * 5) = $108
* 예약 인스턴스 (Reserved instances) 를 사용한다면 더 낮출 수 있다.(30% ~ 63%) *
그래서 상시 떠있어야 하는 product 레벨의 서버 라든가 syslog 서버 등에서 아래와 같이 스크립트를 작성 하고 운영 하여 절감을 한다.
서버가 UTC 으로 세팅 되어 있는 서버 이기 때문에 cron 등록은 아래와 같이 진행하였다.
(cron : 50 23 * * 0-4 bash /shell/aws-start-instance.sh ) 월~금 KST AM 08:50 분에 작동
(cron : 30 09 * * * bash /shell/aws-start-instance.sh ) 매일 KST PM 18:30에 작동
인스턴스 자동 시작 스크립트 ( /shell/aws-start-instance.sh )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#!/bin/bash ################################################################################## # - script dependency awscli. (~]# pip install awscli) # by Enteroa ( enteroa.j@gmail.com ) ################################################################################## export AWS_ACCESS_KEY_ID=AAAAAAAAAAAAAAAAAAAA export AWS_SECRET_ACCESS_KEY=BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB AWSCLI=$(which aws) AWS_INSTANCES=( ap-northeast-2:i-xxxxxxxxxxxxxxxxx ap-northeast-2:i-88888888888888888 ) ## instance status : stopped -> pending -> running instance_start() { until [[ $INSTANCE_CHECK == "running" ]] do INSTANCE_CHECK=$($AWSCLI ec2 describe-instances --region $REGION --instance-ids $INSTANCE --output text \ --query "Reservations[*].Instances[*].[State.Name]") if [[ $INSTANCE_CHECK == "stopped" ]];then $AWSCLI ec2 start-instances --region $REGION --instance-ids $INSTANCE sleep 5 elif [[ $INSTANCE_CHECK == "pending" ]];then sleep 10 elif [[ -z $INSTANCE_CHECK ]];then break 1; fi ((COUNT_LOOP++)) if [ $COUNT_LOOP -ge 360 ];then break 1; fi done unset INSTANCE_CHECK COUNT_LOOP } for a in ${AWS_INSTANCES[*]} do REGION=$(echo $a|cut -d":" -f1) INSTANCE=$(echo $a|cut -d":" -f2) instance_start; unset REGION INSTANCE done unset AWS_INSTANCES AWSCLI AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY exit 0 |
인스턴스 자동 정지 스크립트 ( /shell/aws-start-instance.sh )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#!/bin/bash ################################################################################## # - script dependency awscli. (~]# pip install awscli) # by Enteroa ( enteroa.j@gmail.com ) ################################################################################## export AWS_ACCESS_KEY_ID=AAAAAAAAAAAAAAAAAAAA export AWS_SECRET_ACCESS_KEY=BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB AWSCLI=$(which aws) AWS_INSTANCES=( ap-northeast-2:i-xxxxxxxxxxxxxxxxx ap-northeast-2:i-88888888888888888 ) ## instance status : running -> stopping -> stopped instance_stop() { until [[ $INSTANCE_CHECK == "stopped" ]] do INSTANCE_CHECK=$($AWSCLI ec2 describe-instances --region $REGION --instance-ids $INSTANCE --output text \ --query "Reservations[*].Instances[*].[State.Name]") if [[ $INSTANCE_CHECK == "running" ]];then $AWSCLI ec2 stop-instances --region $REGION --instance-ids $INSTANCE sleep 5 elif [[ $INSTANCE_CHECK == "stopping" ]];then sleep 10 elif [[ -z $INSTANCE_CHECK ]];then break 1; fi ((COUNT_LOOP++)) if [ $COUNT_LOOP -ge 360 ];then break 1; fi done unset INSTANCE_CHECK COUNT_LOOP } for a in ${AWS_INSTANCES[*]} do REGION=$(echo $a|cut -d":" -f1) INSTANCE=$(echo $a|cut -d":" -f2) instance_stop; unset REGION INSTANCE done unset AWS_INSTANCES AWSCLI AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY exit 0 |