0
0

More than 1 year has passed since last update.

【terraform】terraform_remote_stateで外部のstateファイルを参照する

Posted at

ユースケース

  • terraform workspace の切り替えなどのstateファイルが分かれているケースで、別のworkspace(stateファイル)からそのworkspaceのリソース情報を参照したい時などに terraform_rempte_state dataリソースが活用できます
  • 本ページではGCP BigqueryのView情報を別workspaceから参照するケースで手順記載しています

環境

  • terraform version 1.0.3
  • state file backend -> GCS (GCP)
  • Linux 3.10.0-1160.66.1.el7.x86_64 (GCE instance)

構成

  • terraform workspace -> prd(本番環境) に切り替えている時に、stg(ステージング)のリソース情報を読み込みたい場合
  • terraform適用作業は GCP の GCE instance で実施
  • stateファイルの格納先(Backend)はリモート(GCS)
    スクリーンショット 2022-06-17 12.00.44.png

手順

(1) terraform workspace stg側で、prodから参照させたいリソース情報(value)をoutputさせます

output "project_id" {
  value = "${google_bigquery_table.billing_view[0].project}"
}
output "table_id" {
  value = "${google_bigquery_table.billing_view[0].table_id}"
}
output "dataset_id" {
  value = "${google_bigquery_dataset.billing[0].dataset_id}"
}

→上記でterraform apply (stg)

(2) terraform workspace prd側で、(1)でoutputさせたstgのリソース情報を、terraform_remote_state dataリソースを用いて prd stateファイルにスナップショットします

data "terraform_remote_state" "billing" {
  backend = "gcs"
  workspace = "stg"
  config  = {
    bucket  = "test-backend"
    prefix  = "state/bigquery"
  }
}

→上記でterraform apply (prd)

以下のように prd.tfstate に stgのリソース情報(outputs)が記録されます

{
  "version": 4,
  "terraform_version": "1.0.3",
  "serial": 70,
  "lineage": "***",
    ,
    {
      "mode": "data",
      "type": "terraform_remote_state",
      "name": "billing",
      "provider": "provider[\"terraform.io/builtin/terraform\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "backend": "gcs",
            "config": {
              "value": {
                "bucket": "test-backend",
                "prefix": "state/bigquery"
              },
              "type": [
                "object",
                {
                  "bucket": "string",
                  "prefix": "string"
                }
              ]
            },
            "defaults": null,
            "outputs": {
              "value": {
                "dataset_id": "billing",
                "project_id": "test-project",
                "table_id": "billing_view"
              },
              "type": [
                "object",
                {
                  "dataset_id": "string",
                  "project_id": "string",
                  "table_id": "string"
                }
              ]
            },
            "workspace": "stg"
          },
          "sensitive_attributes": []
        }
      ]
    },

(3) 取得したstgリソース情報(terraform_remote_state)をprd側のterraformリソースで利用します

以下では google_bigquery_dataset_access terraformリソース view blockの定義に stgのproject_iddataset_idtable_id情報(value)を取得しています

resource "google_bigquery_dataset_access" "billing_access" {
  count         = local.count.billing_access[terraform.workspace]
  dataset_id    = google_bigquery_dataset.billing[0].dataset_id
  view {
    project_id = "${data.terraform_remote_state.billing.outputs.project_id}"
    dataset_id = "${data.terraform_remote_state.billing.outputs.dataset_id}"
    table_id   = "${data.terraform_remote_state.billing.outputs.table_id}"
  }
}

→上記でterraform apply (prd)

value取得時のフォーマット Terraform >= 0.12

data.terraform_remote_state.(terraform_remote_stateで定義時のリソース名).outputs.(outputの指定したリソース名)

Terraform <= 0.11では.outputsは必要無い(公式Doc参照)

以上

参考URL

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