Clusterを停止するAutomation Documentがない
多分今後作られると思いますがなぜか AWS-StopRdsInstanceはあるのに AWS-StopRdsCluster がありません。
となると CloudWatchの Ruleから定期的に Cron Jobで停止することもできません。 AWS-StopRdsInstanceを参考に作成します。
すぐに結論
AWS-StopRdsInstanceを参考に下記のようなDocumentを作成しました。
結論から言うとAWS-StopRdsInstance の Instance を Clusterに置換、
状態判断の JSONの PropertySelector を DBInstanceStatus ではなく Status にするだけで適用可能です。
---
description: Stop RDS instance
schemaVersion: "0.3"
assumeRole: "{{ AutomationAssumeRole }}"
parameters:
ClusterId:
type: String
description: (Required) RDS Cluster Id to stop
AutomationAssumeRole:
type: String
description: (Optional) The ARN of the role that allows Automation to perform the actions on your behalf.
default: ""
mainSteps:
-
name: AssertNotStopped
action: aws:assertAwsResourceProperty
isCritical: false
onFailure: step:StopCluster
nextStep: CheckStop
inputs:
Service: rds
Api: DescribeDBClusters
DBClusterIdentifier: "{{ClusterId}}"
PropertySelector: "$.DBClusters[0].Status"
DesiredValues: ["stopped", "stopping"]
-
name: StopCluster
action: aws:executeAwsApi
inputs:
Service: rds
Api: StopDBCluster
DBClusterIdentifier: "{{ClusterId}}"
-
name: CheckStop
action: aws:waitForAwsResourceProperty
onFailure: Abort
maxAttempts: 10
timeoutSeconds: 600
inputs:
Service: rds
Api: DescribeDBClusters
DBClusterIdentifier: "{{ClusterId}}"
PropertySelector: "$.DBClusters[0].Status"
DesiredValues: ["stopped"]
結論
これをCloudWatchの Ruleに仕込んで Cronで起動すれば終了です。
その内AWSで提供されるようになるのでは…
というかなんで作らないのか…
参考資料
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/automation-aws-apis-calling.html
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/automation-actions.html#automation-action-executeAwsApi
https://docs.aws.amazon.com/en_pv/AmazonRDS/latest/APIReference/API_DescribeDBClusters.html
https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_StopDBCluster.html