MeWrite Docs

GCP: Quota exceeded

GCPでリソースクォータを超過した場合のエラー

概要

Google Cloud Platformでプロジェクトやリージョンに設定されたリソースクォータを超過した場合に発生するエラーです。

エラーメッセージ

ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - Quota 'CPUS' exceeded. Limit: 8.0 in region us-central1.

または

googleapiclient.errors.HttpError: <HttpError 403 when requesting ... returned "Quota exceeded for quota metric 'Queries' and limit 'Queries per minute' of service 'bigquery.googleapis.com'">

原因

  1. CPU/メモリクォータ: リージョンのCPU/メモリ制限
  2. APIレート制限: APIリクエスト数の制限
  3. ストレージクォータ: ディスクやオブジェクト数の制限
  4. ネットワーククォータ: IPアドレスや帯域幅の制限

解決策

1. 現在のクォータを確認

1
2
3
4
5
6
7
8
9
# プロジェクトのクォータ一覧
gcloud compute project-info describe --project PROJECT_ID

# リージョン別クォータ
gcloud compute regions describe us-central1 --format="table(quotas)"

# 特定のクォータを確認
gcloud compute regions describe us-central1 \
  --format="value(quotas[name=CPUS])"

2. クォータ増加をリクエスト

1
2
3
4
5
# Console経由でリクエスト
# https://console.cloud.google.com/iam-admin/quotas

# gcloudでは直接リクエストできないため、Consoleを使用
echo "https://console.cloud.google.com/iam-admin/quotas?project=PROJECT_ID"

3. 別リージョンを使用

1
2
3
4
5
6
7
# クォータに余裕のあるリージョンを確認
gcloud compute regions list --format="table(name,quotas[name=CPUS].limit,quotas[name=CPUS].usage)"

# 別リージョンでインスタンス作成
gcloud compute instances create my-instance \
  --zone=asia-northeast1-a \
  --machine-type=e2-medium

4. 不要リソースの削除

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 未使用のインスタンスを確認
gcloud compute instances list --filter="status=TERMINATED"

# 削除
gcloud compute instances delete INSTANCE_NAME --zone=ZONE

# 未使用のディスクを確認
gcloud compute disks list --filter="NOT users:*"

# ディスク削除
gcloud compute disks delete DISK_NAME --zone=ZONE

5. APIレート制限への対応

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import time
from google.api_core import retry
from google.api_core.exceptions import ResourceExhausted

# リトライデコレータを使用
@retry.Retry(predicate=retry.if_exception_type(ResourceExhausted))
def call_api():
    # API呼び出し
    pass

# 手動バックオフ
def call_with_backoff(func, max_retries=5):
    for i in range(max_retries):
        try:
            return func()
        except ResourceExhausted:
            wait_time = (2 ** i) + random.random()
            time.sleep(wait_time)
    raise Exception("Max retries exceeded")

6. バッチ処理でリクエスト削減

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from google.cloud import bigquery

client = bigquery.Client()

# 悪い例:個別リクエスト
for row in rows:
    client.insert_rows_json(table, [row])  # クォータ消費

# 良い例:バッチ挿入
client.insert_rows_json(table, rows)  # 1回のリクエスト

7. Preemptible/Spot VMの利用

1
2
3
4
5
# Preemptible VMはクォータが別枠
gcloud compute instances create my-instance \
  --zone=us-central1-a \
  --machine-type=n1-standard-4 \
  --preemptible

8. サービスアカウントのクォータ

1
2
3
4
5
# サービスアカウント作成クォータを確認
gcloud iam service-accounts list --format="value(email)" | wc -l

# 不要なサービスアカウントを削除
gcloud iam service-accounts delete SA_EMAIL

9. BigQueryのスロット管理

1
2
3
4
5
6
7
8
-- 現在のスロット使用状況
SELECT
  project_id,
  job_type,
  SUM(total_slot_ms) / 1000 as total_slot_seconds
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)
GROUP BY project_id, job_type;

10. モニタリングアラートの設定

1
2
3
4
5
6
# クォータ使用率のアラート
gcloud alpha monitoring policies create \
  --display-name="CPU Quota Alert" \
  --condition-filter="metric.type=\"compute.googleapis.com/quota/cpus/usage\" AND resource.type=\"compute.googleapis.com/Quota\"" \
  --condition-threshold-value=0.8 \
  --condition-threshold-comparison=COMPARISON_GT

よくある間違い

  • リージョン固有のクォータとグローバルクォータの混同
  • Preemptible VMと通常VMのクォータが別であることを知らない
  • クォータ増加リクエストの承認に時間がかかることを考慮していない
  • 開発環境と本番環境で同じクォータを共有している

GCP の他のエラー

最終更新: 2025-12-09