Terraform 计划显示导入 GCE 持久磁盘后的差异

问题描述 投票:0回答:2

我必须从快照恢复 GCE 永久磁盘。现在我想将新磁盘导入 terraform。

所以我做到了:

terragrunt import google_compute_region_disk.region_disk projects/my-project/regions/australia-southeast1/disks/ops-syd-jenkins-00

这有效,因为我可以更正远程状态文件中的快照 ID。因此,状态文件刷新良好。 然而,当我这样做时

terragrunt plan
,它会向我显示差异并想要删除我现有的磁盘。

Terraform will perform the following actions:

  # google_compute_region_disk.region_disk will be destroyed
  - resource "google_compute_region_disk" "region_disk" {
      - creation_timestamp        = "2022-03-15T18:11:16.301-07:00" -> null
      - id                        = "projects/my-project/regions/australia-southeast1/disks/ops-syd-jenkins-00" -> null
      - label_fingerprint         = "nT7_dAxskBs=" -> null
      - labels                    = {
          - "goog-gke-volume" = ""
        } -> null
      - last_attach_timestamp     = "2022-03-15T18:13:37.907-07:00" -> null
      - name                      = "ops-syd-jenkins-00" -> null
      - physical_block_size_bytes = 4096 -> null
      - project                   = "my-project" -> null
      - region                    = "australia-southeast1" -> null
      - replica_zones             = [
          - "https://www.googleapis.com/compute/v1/projects/my-project/zones/australia-southeast1-b",
          - "https://www.googleapis.com/compute/v1/projects/my-project/zones/australia-southeast1-a",
        ] -> null
      - self_link                 = "https://www.googleapis.com/compute/v1/projects/my-project/regions/australia-southeast1/disks/ops-syd-jenkins-00" -> null
      - size                      = 64 -> null
      - snapshot                  = "https://www.googleapis.com/compute/v1/projects/my-project/global/snapshots/ops-syd-jenkins-00-australia-southea-20220314173349-1k0o9ddg" -> null
      - source_snapshot_id        = "1331153218805351057" -> null
      - type                      = "pd-ssd" -> null
      - users                     = [
          - "https://www.googleapis.com/compute/v1/projects/my-project/zones/australia-southeast1-b/instances/gke-ops-syd-02-ops-n2s8-2021040716053-76d39e68-wz86",
        ] -> null

      - timeouts {}
    }

  # google_compute_region_disk.region_disk["ops-syd-jenkins-00"] will be created
  + resource "google_compute_region_disk" "region_disk" {
      + creation_timestamp        = (known after apply)
      + id                        = (known after apply)
      + label_fingerprint         = (known after apply)
      + labels                    = {
          + "goog-gke-volume" = ""
        }
      + last_attach_timestamp     = (known after apply)
      + last_detach_timestamp     = (known after apply)
      + name                      = "ops-syd-jenkins-00"
      + physical_block_size_bytes = (known after apply)
      + project                   = "my-project"
      + region                    = "australia-southeast1"
      + replica_zones             = [
          + "https://www.googleapis.com/compute/v1/projects/my-project/zones/australia-southeast1-b",
          + "https://www.googleapis.com/compute/v1/projects/my-project/zones/australia-southeast1-a",
        ]
      + self_link                 = (known after apply)
      + size                      = 64
      + snapshot                  = "https://www.googleapis.com/compute/v1/projects/my-project/global/snapshots/ops-syd-jenkins-00-australia-southea-20220314173349-1k0o9ddg"
      + source_snapshot_id        = (known after apply)
      + type                      = "pd-ssd"
      + users                     = (known after apply)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

不确定我做错了什么。谢谢。

terraform terraform-provider-gcp gce-persistent-disk
2个回答
0
投票

我的错,这是一个糟糕的进口。需要运行以下命令才能导入:

terragrunt import google_compute_region_disk.region_disk["ops-syd-jenkins-00"] projects/my-project/regions/australia-southeast1/disks/ops-syd-jenkins-00

0
投票

terraform 导入的一个好方法是有一个 diff 脚本,例如它将显示状态上真正发生的变化。 下面的示例脚本:

#!/usr/bin/env bash
#
# Get plan
terraform plan -out=tfplan > /dev/null 2>&1

# Convert plan to json
CHANGES=$(terraform show -json tfplan | jq '.resource_changes[].change')

# Diff before and after with newlines expanded
diff -u \
  <(echo "$CHANGES" | jq '.before' | sed 's/\\n/\//g') \
  <(echo "$CHANGES" | jq '.after' | sed 's/\\n/\//g')
© www.soinside.com 2019 - 2024. All rights reserved.