Recap Terraform: Import blocks

Terraform について recap する。個人的な memo として。

まずは、Terraform `v1.5.0` で追加された `Import` blocks について。

https://www.hashicorp.com/blog/terraform-1-5-brings-config-driven-import-and-checks

https://developer.hashicorp.com/terraform/language/import

## Environment
```bash
Terraform v1.7.2
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.7.3. You can update by downloading from https://www.terraform.io/downloads.html
```

## before v1.5.0
### Plan and apply an import
インポートするには、以下の手順が必要だった。
1. `/modules/gcs` を準備。
1. `terraform import` で `.tfstate` を更新。
1. `terraform plan` で差分を確認。
1. HCL を調整。

```bash
.
├── environments
│   └── dev
│       ├── backend.tf
│       ├── main.tf       # edit
│       └── variables.tf
├── modules
│   └── gcs
│       ├── locals.tf     # add
│       ├── main.tf       # add
│       └── variables.tf  # add
└── README.md
```

```go: /modules/gcs/main.tf
resource "google_storage_bucket" "default" {
    project         = var.project_id
    name            = local.uploader_bucket_name
    location        = var.default_region
    force_destroy   = false
}

resource "google_storage_bucket_iam_binding" "object_admin" {
    bucket      = google_storage_bucket.default.name
    role        = "roles/storage.objectAdmin"
    members     = [for account in var.service_accounts : "serviceAccount:${account}"]
}
```

```go:/environments/dev/main.tf
module "hoge_bucket" {
  source = "../../modules/gcs"

  project_id        = var.project_id
  default_region    = var.region
  service_accounts  = var.service_accounts

  depends_on = [
    module.iam
  ]
}
```

- `hogebucket` は bucket name。
```bash: import
terraform import module.uploader_bucket.google_storage_bucket.default hogebucket
```

## v1.5.0 and later
### Plan and apply an import
インポートするには、以下の手順で出来るようになった。
1. `import.tf` を準備。
1. `terraform init`
1. `terraform plan -generate-config-out=generated.tf`
1. `terraform apply`

```bash
.
├── environments
│   └── dev
│       ├── backend.tf
│       ├── generated.tf  # generated
│       ├── import.tf     # add
│       ├── main.tf
│       └── variables.tf
```

```bash: import.tf
import {
  id = "hogehoge"
  to = google_bigquery_dataset.default
}
```

```go: generated.tf
# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform
resource "google_bigquery_dataset" "main" {
  dataset_id                      = "hogehoge"
  default_collation               = null
  default_partition_expiration_ms = 0
  default_table_expiration_ms     = null
  delete_contents_on_destroy      = false
  description                     = null
  friendly_name                   = null
  is_case_insensitive             = false
  labels                          = {}
  location                        = "asia-northeast2"
  max_time_travel_hours           = "168"
  project                         = "sandbox"
  storage_billing_model           = "LOGICAL"
  access {
    domain         = null
    group_by_email = null
    iam_member     = null
    role           = "OWNER"
    special_group  = "projectOwners"
    user_by_email  = null
  }
  access {
    domain         = null
    group_by_email = null
    iam_member     = null
    role           = "READER"
    special_group  = "projectReaders"
    user_by_email  = null
  }
  access {
    domain         = null
    group_by_email = null
    iam_member     = null
    role           = "WRITER"
    special_group  = "projectWriters"
    user_by_email  = null
  }
  timeouts {
    create = null
    delete = null
    update = null
  }
}
```

- `import` を認識。
```bash
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
```

## BTW
認知的変化について考える。

https://www.amazon.co.jp/%E7%A7%81%E3%81%9F%E3%81%A1%E3%81%AF%E3%81%A9%E3%81%86%E5%AD%A6%E3%82%93%E3%81%A7%E3%81%84%E3%82%8B%E3%81%AE%E3%81%8B-%E2%80%95%E2%80%95%E5%89%B5%E7%99%BA%E3%81%8B%E3%82%89%E8%A6%8B%E3%82%8B%E8%AA%8D%E7%9F%A5%E3%81%AE%E5%A4%89%E5%8C%96-%E3%81%A1%E3%81%8F%E3%81%BE%E3%83%97%E3%83%AA%E3%83%9E%E3%83%BC%E6%96%B0%E6%9B%B8-%E9%88%B4%E6%9C%A8%E5%AE%8F%E6%98%AD-ebook/dp/B0B3XD5HJB/ref=sr_1_3?adgrpid=140603340191&dib=eyJ2IjoiMSJ9.XUWCLZO6UMLzCpQr2fRNXjPJyTciN7sZ-wVghIME7gKM2y1wWttnEyq5rv48Zw4h2tF0QxRenYXTb_HuOUN5Ki13uJcfPiwm37TSsh3SRIcX51FisKfXKiz6YqxC1f8m-c11NBs8T0sH_DEJpPRYSgUOBP7vbA6V_AkLnLFPTPFSu3srq0cymFzlsMt2Rp2Zmk3XCdXB21CMkHPTaETXFi2Wphk3865W88WKsll4eGk.z5vEXPmOGxFTS8q8KcxuSvCHIJopIdi7DJyQIGUPH2Q&dib_tag=se&gclid=Cj0KCQiA5rGuBhCnARIsAN11vgTbATT6f1fKjwZxmcf1_QN14RYWKuzd8Pfgk7_OJiyNczH-Rdr4670aAnpkEALw_wcB&hvadid=651371047871&hvdev=c&hvlocphy=1009717&hvnetw=g&hvqmt=e&hvrand=1306166909015186630&hvtargid=kwd-1677009589361&hydadcr=22226_13457292&jp-ad-ap=0&keywords=%E7%A7%81%E3%81%9F%E3%81%A1%E3%81%AF%E3%81%A9%E3%81%86%E5%AD%A6%E3%82%93%E3%81%A7%E3%81%84%E3%82%8B+%E3%81%AE%E3%81%8B&qid=1707970764&sr=8-3

認知的変化 ≒ 学習

engineer それぞれの状態が異なるため、自分と同じことを求めることはできない。

その場合にどう言う選択をすることが良いのか。

自分に適した場所を探すのが 1 つの方法だと個人的に思う。

人生は一度しかない。