LoginSignup
7
1

More than 3 years have passed since last update.

terraformで「run.googleapis.com/client-version」の差分をignore_changesする

Posted at

GCPのフルマネージドでコンテナをよろしく動かすサービスのCloudRun をterraformで構築・運用した時の、ちょっとした問題を解決できました。(1年くらい悩んでました)

Cloud Run: コンテナを秒単位で本番環境にデプロイ  |  Google Cloud
google_cloud_run_service | Resources | hashicorp/google | Terraform Registry

事象

template.metadata.annotations.run.googleapis.com/client-version が、必ず差分が出てしまう。

terraform planの結果抜粋

   ~ template {
          ~ metadata {
              ~ annotations = {
                    "autoscaling.knative.dev/maxScale"      = "1000"
                    "client.knative.dev/user-image"         = "gcr.io/XXXXXX"
                    "run.googleapis.com/client-name"        = "gcloud"
                  - "run.googleapis.com/client-version"     = "318.0.0" -> null
                    "run.googleapis.com/sandbox"            = "gvisor"
                }
                generation  = 0
                labels      = {}
            }

今の値が「318.0.0」だから、tfファイルに書けばよいのでは、、、と思うかもしれませんが、、、、

 template {
    metadata {
      annotations = {
        "autoscaling.knative.dev/maxScale" : 1000
        "run.googleapis.com/client-name" : "gcloud"
        "client.knative.dev/user-image" = "gcr.io/XXXXXX"
        "run.googleapis.com/client-version" = "318.0.0"
        "run.googleapis.com/sandbox" = "gvisor"
      }
    }
  }

このバージョンはCloudRunが自動でバージョンアップするので、通常のデプロイを繰り返していると、どこかのタイミングでずれます。

```terraform
   ~ template {
          ~ metadata {
              ~ annotations = {
                    "autoscaling.knative.dev/maxScale"      = "1000"
                    "client.knative.dev/user-image"         = "gcr.io/XXXXXX"
                    "run.googleapis.com/client-name"        = "gcloud"
                  - "run.googleapis.com/client-version"     = "319.0.0" -> "318.0.0"
                    "run.googleapis.com/sandbox"            = "gvisor"
                }
                generation  = 0
                labels      = {}
            }

対応

terraformで変更を無視するignore_change という設定があるので、これを入れればよいのですが、値が入れ子なので書き方にコツがいります。

正解はこうです。

  lifecycle {
    ignore_changes = [
      template[0].metadata[0].annotations["run.googleapis.com/client-version"]
    ]
  }

templatemetadataがlistになっているようで、[0]をつけないといけないのが大変わかりにくいです。

参考

terraformで入れ子の値を無視(ignore_change)する方法 - Qiita

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