LoginSignup
1
1

More than 1 year has passed since last update.

【GCP】予算設定を terraform でやろうとしてハマった話

Posted at

GCP環境を Terraform を用いて構築していた際、予算設定も Terraform で行おうとしてハマったのでその記録。

実行環境

  • Ubuntu 20.04.4 LTS (WSL)
  • Terraform v1.3.2
    • provider google v4.39.0
    • 実行アカウントとして、オーナー権限を付与したサービスアカウントを使用

エラー概要

操作しているアカウントに十分なロールを付与していたが、Terraform で予算設定しようとしたところ以下のエラーに遭遇した。

エラーメッセージ:

google_billing_budget.budget: Creating...
╷
│ Error: Error creating Budget: googleapi: Error 403: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the billingbudgets.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/. If you are getting this error with curl or similar tools, you may need to specify 'X-Goog-User-Project' HTTP header for quota and billing purposes. For more information regarding 'X-Goog-User-Project' header, please check https://cloud.google.com/apis/docs/system-parameters.
│ Details:
│ [
│   {
│     "@type": "type.googleapis.com/google.rpc.ErrorInfo",
│     "domain": "googleapis.com",
│     "metadata": {
│       "consumer": "projects/764086051850",
│       "service": "billingbudgets.googleapis.com"
│     },
│     "reason": "SERVICE_DISABLED"
│   }
│ ]
│
│   with google_billing_budget.budget,
│   on google_billing_budget.tf line 1, in resource "google_billing_budget" "budget":
│    1: resource "google_billing_budget" "budget" {
│

解決方法

公式ドキュメントによると
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/billing_budget

If you are using User ADCs (Application Default Credentials) with this resource, you must specify a billing_project and set user_project_override to true in the provider configuration. Otherwise the Billing Budgets API will return a 403 error. Your account must have the serviceusage.services.use permission on the billing_project you defined.

とあるので provider.google に設定を書き加える必要があるのだが、その前に次の API も有効化しておく。
前述のエラーメッセージや公式ドキュメントでは触れられていないものが含まれているが、実際にはこれらの API も必要になる。

  • Cloud Billing Budget API (billingbudgets.googleapis.com)
  • Cloud Billing API (cloudbilling.googleapis.com)
  • Cloud Resource Manager API (cloudresourcemanager.googleapis.com)
  • Identity and Access Management (IAM) API (iam.googleapis.com)
  • Service Usage API (serviceusage.googleapis.com)

これらを有効化した後に、provider の修正を加える。
https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference

provider "google" {
  billing_project       = "example-project"
  user_project_override = true
}

これで予算設定ができるようになる。

ハマった点

provider を修正してから API 有効化の順ではうまくいかず、そこでハマってしまった。
provider に billing_project を追記する前は Cloud Resource Manager API のみ有効化でサービスアカウントなどのリソースは作成できていたのに、追記後は追加で API を有効化しないと terraform plan 実行時にエラーになってしまう。

解決方法に至るまで、エラー確認→provider 切り戻し→不足している API の有効化→provider 修正を繰り返すことに。

リファレンスにある billing_project の説明からは読み取れなかったが、おそらく明示的にプロジェクトを指定したことで、そのプロジェクトが terraform からの API コールを直接受けるように変更されたのだと思われる。

billing_project 関連の正確な挙動に関する情報を入手次第、この記事に追加したい。

1
1
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
1
1