タダです.
Aurora AutoScaling をスケジュール実行したいことありますよね.AWS CLI でもできますが,Terraform で設定をしてみたのでこの記事でまとめます.
Terraform のコード
AutoScaling のスケジュール設定は aws_appautoscaling_scheduled_action
で行います.AM 10時に2台にスケールアウトし,PM20時に1台にスケールインするポリシーを設定してみます.
resource "aws_appautoscaling_scheduled_action" "scaleout" { name = "blog-test scale-out by cron" service_namespace = "rds" resource_id = "cluster:blog-test" scalable_dimension = "rds:cluster:ReadReplicaCount" schedule = "cron(0 10 * * ? *)" timezone = "Asia/Tokyo" scalable_target_action { min_capacity = 2 max_capacity = 4 } } resource "aws_appautoscaling_scheduled_action" "scalein" { name = "blog-test scale-in by cron" service_namespace = "rds" resource_id = "cluster:blog-test" scalable_dimension = "rds:cluster:ReadReplicaCount" schedule = "cron(0 20 * * ? *)" timezone = "Asia/Tokyo" scalable_target_action { min_capacity = 1 max_capacity = 4 } }
関連情報
上記の定義を適用すると以下のようになります.
Terraform will perform the following actions: # aws_appautoscaling_scheduled_action.scalein will be created + resource "aws_appautoscaling_scheduled_action" "scalein" { + arn = (known after apply) + id = (known after apply) + name = "blog-test scale-in by cron" + resource_id = "cluster:blog-test" + scalable_dimension = "rds:cluster:ReadReplicaCount" + schedule = "cron(0 20 * * ? *)" + service_namespace = "rds" + timezone = "Asia/Tokyo" + scalable_target_action { + max_capacity = "4" + min_capacity = "1" } } # aws_appautoscaling_scheduled_action.scaleout will be created + resource "aws_appautoscaling_scheduled_action" "scaleout" { + arn = (known after apply) + id = (known after apply) + name = "blog-test scale-out by cron" + resource_id = "cluster:blog-test" + scalable_dimension = "rds:cluster:ReadReplicaCount" + schedule = "cron(0 10 * * ? *)" + service_namespace = "rds" + timezone = "Asia/Tokyo" + scalable_target_action { + max_capacity = "4" + min_capacity = "2" } } Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_appautoscaling_scheduled_action.scalein-nighttime: Creating... aws_appautoscaling_scheduled_action.scaleout-nighttime: Creating... aws_appautoscaling_scheduled_action.scalein-nighttime: Creation complete after 0s [id=scalein-nighttime-rds-cluster:blog-test] aws_appautoscaling_scheduled_action.scaleout-nighttime: Creation complete after 0s [id=scaleout-nighttime-rds-cluster:blog-test] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
設定した AutoScaling Policy を確認する
設定した AutoScaling Policy が意図通りに入っているかを確認してみます.aws application-autoscaling describe-scheduled-actions --service-namespace rds
を実行することで Aurora AutoScaling のスケジュールしたAutoScaling ポリシーを確認できます.
❯ aws application-autoscaling describe-scheduled-actions --service-namespace rds { "ScheduledActions": [ { "ScheduledActionName": "blog-test scale-out by cron", "ScheduledActionARN": "arn:aws:autoscaling:ap-northeast-1:1234567891011:scheduledAction:c55ffe6e-2bb2-4591-9abe-ab12c6414ef0:resource/rds/cluster:blog-test:scheduledActionName/blog-test > "ServiceNamespace": "rds", "Schedule": "cron(0 10 * * ? *)", "Timezone": "Asia/Tokyo", "ResourceId": "cluster:blog-test", "ScalableDimension": "rds:cluster:ReadReplicaCount", "ScalableTargetAction": { "MinCapacity": 2, "MaxCapacity": 4 }, "CreationTime": "2023-12-02T17:00:02.052000+09:00" }, { "ScheduledActionName": "blog-test scale-in by cron", "ScheduledActionARN": "arn:aws:autoscaling:ap-northeast-1:1234567891011:scheduledAction:c55ffe6e-2bb2-4591-9abe-ab12c6414ef0:resource/rds/cluster:blog-test:scheduledActionName/blog-test > "ServiceNamespace": "rds", "Schedule": "cron(0 20 * * ? *)", "Timezone": "Asia/Tokyo", "ResourceId": "cluster:blog-test", "ScalableDimension": "rds:cluster:ReadReplicaCount", "ScalableTargetAction": { "MinCapacity": 1, "MaxCapacity": 4 }, "CreationTime": "2023-12-02T17:00:02.041000+09:00" } ] }
まとめ
Aurora AutoScaling を Terraform で定義する機会があったのでまとめました.これまでは AWS CLI でやっていたのですが,Terraform 化できることで簡単にポリシーの変更もしやすく便利です.