LoginSignup
9
8

More than 5 years have passed since last update.

自動起動処理するEC2インスタンス自身をオートスケールで1時間だけ起動する

Last updated at Posted at 2015-04-30

EC2インスタンスを業務時間だけ自動起動する処理をAWS SDK for PHP 2で作成するで作成したインスタンス自身を1時間だけ立ち上げたら節約になるのでは??
→それCDP:Scheduled Autoscalingパターンでできるよ

自動起動処理を実行するインスタンスのAMIを作成する

  1. サービス→EC2→インスタンス→自動起動処理実行インスタンス選択→アクション→イメージ→イメージ作成
  2. イメージ名:AWS_tools→再起動しないにチェック→イメージの作成

Auto Scaling 起動設定を作成する

  1. 起動設定→起動設定の作成
  2. マイ AMI→AWS_toolsを選択→t2.micro
  3. 起動設定の作成でIAM ロール選択 AWS SDK for PHP 2をインストールして、サンプルコードを動かすで作成したAutoStartを選択
  4. 残りの項目をポチポチ押して作成

Auto Scaling グループを作成する

  1. この起動設定を使用してAuto Scalingグループを作成するを押す 1.グループ名:AWS_tools_SG
  2. グループサイズ:開始時 0 インスタンス
  3. ネットワーク:デフォルト→サブネット:AZのどれでもいいので選択
  4. このグループを初期のサイズに維持するにチェック
  5. タグName:AWS_tools_scheduledと設定。起動時のインスタンス名になる

AWS CLI を使用してスケーリングのスケジュールを作成する

自動起動処理を実行するインスタンスにはIAM Roleが付与してあって、Auto Scallingの権限もあるので、ここで操作する

実行できるコマンド

$ aws autoscaling cmd
usage: aws [options] <command> <subcommand> [parameters]
aws: error: argument operation: Invalid choice, valid choices are:

attach-instances                         | complete-lifecycle-action
create-auto-scaling-group                | create-launch-configuration
create-or-update-tags                    | delete-auto-scaling-group
delete-launch-configuration              | delete-lifecycle-hook
delete-notification-configuration        | delete-policy
delete-scheduled-action                  | delete-tags
describe-account-limits                  | describe-adjustment-types
describe-auto-scaling-groups             | describe-auto-scaling-instances
describe-auto-scaling-notification-types | describe-launch-configurations
describe-lifecycle-hook-types            | describe-lifecycle-hooks
describe-metric-collection-types         | describe-notification-configurations
describe-policies                        | describe-scaling-activities
describe-scaling-process-types           | describe-scheduled-actions
describe-tags                            | describe-termination-policy-types
detach-instances                         | disable-metrics-collection
enable-metrics-collection                | enter-standby
execute-policy                           | exit-standby
put-lifecycle-hook                       | put-notification-configuration
put-scaling-policy                       | put-scheduled-update-group-action
record-lifecycle-action-heartbeat        | resume-processes
set-desired-capacity                     | set-instance-health
suspend-processes                        | terminate-instance-in-auto-scaling-group
update-auto-scaling-group                | help

スケールグループ確認

$ aws autoscaling describe-auto-scaling-groups --region ap-southeast-2

スケジュールを登録

UTC 23:13(JST 8:13)に1台起動する設定にする
$ aws autoscaling put-scheduled-update-group-action --region ap-southeast-2 --output json --auto-scaling-group-name AWS_tools_SG --scheduled-action-name scale_out --recurrence "13 23 * * *" --min-size 1 --max-size 1


UTC 0:12(JST 9:12)に0台起動する設定にする。つまりTerminateされる。
$ aws autoscaling put-scheduled-update-group-action --region ap-southeast-2 --output json --auto-scaling-group-name AWS_tools_SG --scheduled-action-name scale_in --recurrence "12 0 * * *" --min-size 0 --max-size 0

スケジュールアクションを確認

$ aws autoscaling describe-scheduled-actions --region ap-southeast-2

登録を削除したい時

aws autoscaling delete-scheduled-action --auto-scaling-group-name AWS_tools_SG --scheduled-action-name scale_out --region ap-southeast-2

あとは、scale_outの時間になったら1台起動して、scale_inの時間に0台になる。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8