12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GCP】営業時間内のみCloud SQLを起動するスニペット(Cloud Scheduler + Cloud Workflows)

Last updated at Posted at 2025-08-25

備忘録 + 共有用です。

目的

dev / stgなどで、営業時間内のみCloud SQLを起動し、コストを削減する

仕様

このサンプルは、下記の仕様で実行されます。(FaaS不要)

  • 実行ワークフロー(Cloud Workflows)
    • 引数に応じて、指定のCloud SQLインスタンスを起動/停止
    • GCPプロジェクトは、実行環境自身のものをWorkflows内で取得
  • 実行スケジュール(Cloud Scheduler)
    • 月〜金曜の9時に指定のCloud SQLインスタンスを起動
      • 18時に指定のCloud SQLインスタンスを停止
  • その他
    • 最低限の実装です

Cloud Workflows

※workflows名は、"start-stop-instance"とする

main:
  # 入力例: {"instance":"(Cloud SQLのインスタンス名)","action":"stop"}
  params: [input]
  steps:
  - init:
      assign:
      - project_id: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")} #workflows実行のプロジェクトID
      - instance: ${input.instance}
      - action: ${input.action}
      - desired: "NEVER"  # デフォルトは停止とする
  - choose_action:
      switch:
      - condition: ${action == "start"}
        steps:
        - set_start:
            assign:
            - desired: "ALWAYS"
      - condition: ${action == "stop"}
        steps:
        - set_stop:
            assign:
            - desired: "NEVER"
      - condition: ${true}
        steps:
        - finish_invalid:
            return:
              status: "error"
              invalid_action: ${action}

  # settingsVersion が必要なので一度 取得
  - get_current:
      call: googleapis.sqladmin.v1beta4.instances.get
      args:
        project: ${project_id}
        instance: ${instance}
      result: current

  - patch_policy:
      call: googleapis.sqladmin.v1beta4.instances.patch
      args:
        project: ${project_id}
        instance: ${instance}
        body:
          settings:
            activationPolicy: ${desired}
            settingsVersion: ${current.settings.settingsVersion}

  - finish:
      return:
        status: "ok"
        action: ${action}
        instance: ${instance}

実行テスト(Workflows単体)

export PROJECT_ID='(GCPプロジェクトID)'

gcloud workflows run start-stop-instance \
--location=asia-northeast1 --project ${PROJECT_ID} \
--data='{"instance":"app","action":"stop"}'

結果

停止成功

スクリーンショット 2025-08-25 11.44.08.png

Cloud Scheduler

gcloudで、下記スケジュールを追加
- 月〜金曜の9時に指定のCloud SQLインスタンスを起動
- 18時に指定のCloud SQLインスタンスを停止

[start]

gcloud scheduler jobs create http start-cloudsql \
  --location=asia-northeast1 \
  --schedule="0 9 * * 1-5" \
  --time-zone="Asia/Tokyo" \
  --http-method=POST \
  --uri="https://workflowexecutions.googleapis.com/v1/projects/${PROJECT_ID}/locations/asia-northeast1/workflows/start-stop-instance/executions" \
  --message-body="{\"argument\":\"{\\\"instance\\\":\\\"app\\\",\\\"action\\\":\\\"start\\\"}\"}" \
  --oauth-service-account-email="(サービスアカウント名)@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project ${PROJECT_ID}
[stop]

gcloud scheduler jobs create http stop-cloudsql \
  --location=asia-northeast1 \
  --schedule="0 18 * * 1-5" \
  --time-zone="Asia/Tokyo" \
  --http-method=POST \
  --uri="https://workflowexecutions.googleapis.com/v1/projects/${PROJECT_ID}/locations/asia-northeast1/workflows/start-stop-instance/executions" \
  --message-body="{\"argument\":\"{\\\"instance\\\":\\\"app\\\",\\\"action\\\":\\\"stop\\\"}\"}" \
  --oauth-service-account-email="(サービスアカウント名)@${PROJECT_ID}.iam.gserviceaccount.com" \
  --project ${PROJECT_ID}

実行テスト

スケジュール実行成功

スクリーンショット 2025-08-25 13.40.17.png

Cloud SQL起動成功
スクリーンショット 2025-08-25 12.27.56.png

備考

  • 各SAには権限が必要です

    • Cloud Scheduler実行SA
      • roles/workflows.invoker
    • Cloud Workflows実行SA
      • roles/cloudsql.editor
  • Cloud SQLは、単純な停止/起動では"受信"ipは不変です
    (https://cloud.google.com/sql/docs/mysql/private-ip)

For public and private IP addresses, the incoming address of the Cloud SQL instance is static; it doesn't change. The outgoing address isn't always static, except for outgoing public IP addresses of external server replicas which are always static.

terraform版

(TBD。別記事に書くかもしれません)

12
4
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
12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?