Auto ScalingをCLIから使う as-cmd、as-version、as-describe-*、as-create-*、as-put-*

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Auto ScalingをCLIから使う

AutoScalingに関する説明はほどほどにして実際にCLI(コマンドラインインタフェース)から使ってみましょう!

環境設定

各種SDKをダウンロードしてパスを通します。

これをaws.shという名前で保存しておきます。保存先は各自パスに合わせてください。

export EC2_HOME=~/dev/aws/ec2-api-tools-1.4.4.0/
export AWS_AUTO_SCALING_HOME=~/dev/aws/AutoScaling-1.0.39.0/
export AWS_ELB_HOME=~/dev/aws/ElasticLoadBalancing-1.0.12.0/
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home/
export PATH=$PATH:$EC2_HOME/bin/:$JAVA_HOME/bin/:$AWS_AUTO_SCALING_HOME/bin/:$AWS_ELB_HOME/bin/:

#pemファイルはAWSログインして作成してください。
export EC2_PRIVATE_KEY=~/dev/key/pk-XXXXXXXXXXXXXXXXX.pem
export EC2_CERT=~/dev/key/cert-XXXXXXXXXXXXXXXXX.pem

source aws.shでパスを有効にします。

source aws.sh

Auto Scalingで使えるコマンド一覧を表示します。

as-cmdで一覧表示します。

$ as-cmd
Command Name                                Description                                            
------------                                -----------                                            
as-create-auto-scaling-group                Create a new auto scaling group                        
as-create-launch-config                     Create a new launch config                             
as-create-or-update-trigger                 Creates a new trigger or updates an existing trigger.  
as-delete-auto-scaling-group                Delete the specified auto scaling group                
as-delete-launch-config                     Delete the specified launch configuration              
as-delete-notification-configuration        Delete the specified notification configuration        
as-delete-policy                            Delete the specified policy                            
as-delete-scheduled-action                  Delete the specified scheduled action                  
as-delete-trigger                           Delete a  trigger.                                     
as-describe-adjustment-types                Describes all policy adjustment types.                 
as-describe-auto-scaling-groups             Describes the specified auto scaling group(s)          
as-describe-auto-scaling-instances          Describes the specified auto scaling instance(s)       
as-describe-auto-scaling-notification-types Describes all auto scaling notification types.         
as-describe-launch-configs                  Describe the specified launch configurations           
as-describe-metric-collection-types         Describes all metric colle... metric granularity types.
as-describe-notification-configurations     Describes all notification... given auto-scaling groups
as-describe-policies                        Describes the specified policy/policies                
as-describe-process-types                   Describes all scaling process types.                   
as-describe-scaling-activities              Describe a set of activiti...ties belonging to a group.
as-describe-scheduled-actions               Describes the specified scheduled action(s)            
as-describe-triggers                        Describes a trigger including its internal state.      
as-disable-metrics-collection               Disable collection of AutoScaling group metrics        
as-enable-metrics-collection                Enable collection of AutoScaling group metrics         
as-execute-policy                           Executes the specified policy                          
as-put-notification-configuration           Create or replace notifica...for the auto scaling group
as-put-scaling-policy                       Creates or updates a scaling policy                    
as-put-scheduled-update-group-action        Creates or updates a scheduled update group action     
as-resume-processes                         Resumes all suspended scal... given auto scaling group.
as-set-desired-capacity                     Set the desired capacity of the auto scaling group     
as-set-instance-health                      Set the health of the instance                         
as-suspend-processes                        Suspends all scaling proce... given auto scaling group.
as-terminate-instance-in-auto-scaling-group Terminate a given instance.                            
as-update-auto-scaling-group                Update specified auto scaling group                    
help                                        
version                                     Prints the version of the CLI tool and the API.        

    For help on a specific command, type '<commandname> --help'

Auto ScalingのSDKバージョン

$ as-version 
Amazon AutoScaling CLI version 1.0.39.0 (API 2011-01-01)

東京リージョンでAuto Scalingを試す準備をする

まず、東京リージョンの名前、アベイラビリティゾーンの名前、スケーリングするAMIのIDを確認します。

ステップ1:リージョン一覧から東京を見つける。

$ ec2-describe-regions 
REGION	eu-west-1	ec2.eu-west-1.amazonaws.com
REGION	us-east-1	ec2.us-east-1.amazonaws.com
REGION	ap-northeast-1	ec2.ap-northeast-1.amazonaws.com
REGION	us-west-1	ec2.us-west-1.amazonaws.com
REGION	ap-southeast-1	ec2.ap-southeast-1.amazonaws.com

ステップ2:リージョンを指定してアベイラビリティゾーン一覧を得る。

$ ec2-describe-availability-zones --region ap-northeast-1
AVAILABILITYZONE	ap-northeast-1a	available	ap-northeast-1	
AVAILABILITYZONE	ap-northeast-1b	available	ap-northeast-1

ステップ3:テスト用にAmazon LinuxのAMIの名前を得るには、ググればだいたい出てきます。Amazon Linux 32bitで東京リージョンのAMIは、ami-300ca731でした。インスタンスはt1.microとします。それでは、次に起動コンフィグを指定します。

起動コンフィグを作成する

前のステップで得たパラメータを使って起動コンフィグを作成します。

$ as-create-launch-config t1config ¥
   --region ap-northeast-1 --image-id ami-300ca731 ¥
   --instance-type t1.micro --group default
OK-Created launch config

起動コンフィグの状態を確認します。

$ as-describe-launch-configs --region ap-northeast-1
LAUNCH-CONFIG  t1config  ami-300ca731  t1.micro

確かに作られましたね。次にAuto Scalingグループを作成します。

$ as-create-auto-scaling-group mygroup1 ¥
   --region ap-northeast-1 --availability-zones ap-northeast-1a ¥
   --launch-configuration t1config --max-size 2 --min-size 1
OK-Created AutoScalingGroup

Auto Scalingグループを表示します。

$ as-describe-auto-scaling-groups --region ap-northeast-1
AUTO-SCALING-GROUP  mygroup1  t1config  ap-northeast-1a  1  2  1
INSTANCE  i-4e14484f  ap-northeast-1a  InService  Healthy  t1config

実際にインスタンスが起動したか確認する。

EC2コマンドを使います。いけましたね。でも、インスタンスが2つ起動していない。なぜだろ。これは後で説明します。

$ ec2-describe-instances --region ap-northeast-1
RESERVATION	r-34411435	771293814336	default
INSTANCE	i-4e14484f	ami-300ca731	ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com	ip-XXX.XXX.XXX.XXX.ap-northeast-1.compute.internal	running		0		t1.micro	2011-08-15T16:52:30+0000	ap-northeast-1a	aki-d209a2d3			monitoring-enabled	XXX.XXX.XXX.XXX	XXX.XXX.XXX.XXX			ebs					paravirtual	xen	84d5bc6a-b07f-43c8-bc8a-ade982b50785	sg-06c26807	default
BLOCKDEVICE	/dev/sda1	vol-fb350f91	2011-08-15T16:52:50.000Z	
TAG	instance	i-4e14484f	aws:autoscaling:groupName	mygroup1

わざとインスタンスを落とす

落とします。

$ ec2-stop-instances --region ap-northeast-1 i-4e14484f
INSTANCE	i-4e14484f	running	stopping

最少1つインスタンスが必要なところを0の場合、不健康であるという判断のはず。インスタンスがストップしたら確認しましょう。

$ as-describe-auto-scaling-groups --region ap-northeast-1
AUTO-SCALING-GROUP  mygroup1  t1config  ap-northeast-1a  1  2  1
INSTANCE  i-4e14484f  ap-northeast-1a  Terminating  Unhealthy  t1config

Unhealthyでしたー。そして、少々時間を置いてインスタンス一覧を確認すると新しいのが立ち上がっていて、グループの状態はHealthyに戻っていました

$ as-describe-auto-scaling-groups --region ap-northeast-1
AUTO-SCALING-GROUP  mygroup1  t1config  ap-northeast-1a  1  2  1
INSTANCE  i-a2c19da3  ap-northeast-1a  InService  Healthy  t1config

まとめ

次回は、ポリシーの動作について詳しくご紹介します〜