GCP Service Account キーを使わない Terraform
#Terraform #GoogleCloud
サービス アカウントの使用と管理のベスト プラクティス  |  Cloud IAM のドキュメント  |  Google Cloud
的にはキー作るな感があった
Terraform doc
Google Provider Configuration Reference | Guides | hashicorp/google | Terraform Registry
案内はある、一方で
Using Terraform-specific service accounts to authenticate with GCP is the recommended practice when using Terraform.
サービスアカウントを分けるが認証情報は
First, authenticate with GCP. The easiest way to do this is to run gcloud auth application-default login, if you already have gcloud installed. If you don't already have it, you can install it from here.
Configuring the Provider - Getting Started with the Google provider | Guides | hashicorp/google | Terraform Registry
Application Default Credential を使った時に project が食い違う場合はどうなる?
Quota project "your-project" was added to ADC which can be used by Google client libraries for billing and quota.
が出るのを確認する
impersonate
Terraform コードにおける Google Cloud サービス アカウントの権限借用 | Google Cloud Blog
Google Provider Configuration Reference | Guides | hashicorp/google | Terraform Registry
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=email があれば terraform からも impersonate される
provider "google" { impersonate_service_account = "..." } ででも指定できる
最初にどうするか
手で terraform ユーザを作る & token creator を付与する?
↑ を terraform に書いておきたいが...
最初は gcloud ユーザで実行する
code:impersonate.tf
# なりすまされるサービスアカウント
resource "google_service_account" "terraform" {
account_id = "terraform"
}
# そのサービスアカウントになりすませるようにユーザー(グループ)に TokenCreator を付与
resource "google_service_account_iam_binding" "token_creator_terraform" {
service_account_id = data.google_service_account.terraform.name
role = "roles/iam.serviceAccountTokenCreator"
members = [
"group:my-team@hatena.ne.jp",
]
}
その後 google provider に impersonate_service_account を設定する?
? backend でも渡す必要があるか確認