dshimizu/blog/alpha

とりとめのないITブログ

Google Cloud 向けに Terraform を使うためのローカル環境を準備する

はじめに

Google Cloud の環境構築を Terraform で行いたいので、Terraform を実行するにあたってどういう風に設定すれば良いのか調べた。

事前準備

Google Cloud は利用可能になっていることとする。

Google Cloud 用の Provider 設定

Terraform の Google Provider というプラグインを定義する必要がある。 tf ファイルに以下のような感じのことを記述する。provider.tf とかファイル名は何でも良い。私は main.tf にまとめている。 Google Provider に、Terraform を利用する Google Cloud プロジェクトとデフォルトリージョン、ゾーンを設定する。

Provider バージョンは運用方針によると思うけど、互換性のある最小バージョンにするのが良さそう。ここでは 4.0.0 以上としている。

provider "google" {
  project = "hogehoge-prj"
  region  = "asia-northeast1"
  zone    = "asia-northeast1-a"
}

terraform {
  required_providers {
    google = {
      version = "~> 4.0.0"
    }
  }
}

Terraform を実行する Google Cloud プロジェクトの指定

ローカル実行するには Google Cloud の API を利用できるようにしておく必要がある。

以下のいずれかを利用する必要がある。

  • サービスアカウントを使う
  • 個人ユーザーアカウント(Google アカウント)を使う(Cloud SDK)

CI を行う場合はサービスアカウントを利用する感じになると思うが、サービスアカウントだと、複数人で Terraform を利用する場合、各々がローカルで実行する時にアカウント情報を使い回すことになるので、ローカル実行時は自身のアカウントを使いたい、といったケースもありそうだなと思い、自分自身の個人ユーザーアカウントを使うこととする。

この場合、個人ユーザーアカウントで認証することで、~/.config/gcloud/application_default_credentials.json (Application Default Credentials (ADC)) が作成され、Terraform の Google Provider (多分 Cloud SDK)がこれを使うようになる。

プロジェクト設定

まず、Terraform を実行する対象となる Google Cloud プロジェクトの設定をする。 プロジェクトが Terraform を実行したいプロジェクトではない場合、指定する。

% gcloud config set project hogehoge-prj

Google Cloud 認証用の設定

init を実行する。

% terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v4.14.0...
- Installed hashicorp/google v4.14.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

そのまま何もせず実行しようとすると plan 実行時に以下のようなエラーになる。

% terraform plan
╷
│ Error: Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block.  No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'.  Original error: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
│ 
│   with provider["registry.terraform.io/hashicorp/google"],
│   on main.tf line 2, in provider "google":
│    2: provider "google" {
│ 

Terraform を介して認証を突破できる状態にないため、メッセージにある通り、gcloud auth application-default login を実行する。

% gcloud auth application-default login

ブラウザが起動し、認証許可画面が出るので、そのまま許可する。 以下のようなクレデンシャル情報を保持したファイルが生成される。これで認証できるようになる。

% cat ~/.config/gcloud/application_default_credentials.json
{
  "client_id": "7**********0-********************************.apps.googleusercontent.com",
  "client_secret": "d-******************Ty",
  "quota_project_id": "hogehoge-prj",
  "refresh_token": "1//**********************************************************2qo",
  "type": "authorized_user"
}

実行

これで実行できるようになる。

% terraform plan

まとめ

Google Cloud で Terraform を実行するためのローカル環境構築について雑にまとめた。 あとは tfstate を GCS に置くようにしたほうが良い。

参考