[A-00133]Terraformの使い方色々

Terraformの色々な使い方について記載しておく

・for文の使い方

基本的な構文は下記のとおり

下記の使用例ではlocalsで定義した配列パラメータをforでループし、upper関数を適用して配列に格納している。

locals {
  list = [
    "hoge",
    "fuga"
  ]
}

output "output_list" {
    value = [for l in local.list : upper(l) ]
}

output定義したリソースはterraform applyでコンソールに表示される。上記を実行した結果は下記のとおり。

Outputs:

output_list = [
  "HOGE",
  "FUGA",
]

・ファイルを作成する

terraformでファイルを作成する方法。かなりシンプルな例として、ファイル名と内容をlocalsでパラメータ保持しておき、outputでファイルとして出力する。

locals {
  content = "Iam chanpion."
  filename = "hello.txt"
}

resource "local_file" "local_sample" {
  content = local.content
  filename = local.filename
}

上記の実行した結果、下記のようにファイルが作成される。注意点はoutputではなくresourceでlocal_filesを定義するところ。

@cloudshell:~/syntax-practice (xxxx)$ ls
hello.txt  main.tf  terraform.tfstate  terraform.tfstate.backup

別の方法として下記も記載しておく。こちらはパラメータの持ち方を変えた方法。ファイル作成のやり方としては全く前述した内容と同じ。

variable content2 {
  default = "Whats a matter."
}

variable filename2 {
  default = "second.txt"
}

resource "local_file" "local_sample2" {
  content = var.content2
  filename = var.filename2
}
@cloudshell:~/syntax-practice (xxxxx)$ ls
main.tf  second.txt  terraform.tfstate  terraform.tfstate.backup

・importの使い方

importブロックは既存のリソース(terraformで管理してない)をTerraform管理下において使い回す機能があります。

下記のコマンドでTerraform管理外のリソース(GCS)をまず作成しておきます。

gcloud storage buckets create gs://terraform-import-example

次に下記の2ファイルを作成します。

provider "google" {
    region = "asia-northeast1"
}
import {
    id = "terraform-import-example"
    to = google_storage_bucket.main
}

resource "google_storage_bucket" "main" {
    name = "terraform-import-example"
    location = "asia-northeast1"
    force_destroy = true
}

上記を作成したらterraform planで実行計画を確認します。

すると下記のように、先に作成したGCS(terraform-import-example)のlocationがmain.tfで指定したAsia-northeast1に書き換える旨の警告が表示されます。加えてreplacedと記載も表示されます。

@cloudshell:~/imp-test (xxxx)$ terraform plan
google_storage_bucket.main: Preparing import... [id=terraform-import-example]
google_storage_bucket.main: Refreshing state... [id=terraform-import-example]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # google_storage_bucket.main must be replaced
  # (imported from "terraform-import-example")
  # Warning: this will destroy the imported resource
-/+ resource "google_storage_bucket" "main" {
      - default_event_based_hold    = false -> null
      ~ force_destroy               = false -> true
      ~ id                          = "terraform-import-example" -> (known after apply)
      ~ labels                      = {} -> (known after apply)
      ~ location                    = "US" -> "ASIA-NORTHEAST1" # forces replacement
        name                        = "terraform-import-example"
      ~ project                     = "xxxx" -> (known after apply)
      ~ public_access_prevention    = "inherited" -> (known after apply)
      - requester_pays              = false -> null
      ~ self_link                   = "https://www.googleapis.com/storage/v1/b/terraform-import-example" -> (known after apply)
        storage_class               = "STANDARD"
      ~ uniform_bucket_level_access = false -> (known after apply)
      ~ url                         = "gs://terraform-import-example" -> (known after apply)

      - timeouts {}
    }

Plan: 1 to import, 1 to add, 0 to change, 1 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_storage_bucket.main: Importing... [id=terraform-import-example]
google_storage_bucket.main: Import complete [id=terraform-import-example]
google_storage_bucket.main: Destroying... [id=terraform-import-example]
google_storage_bucket.main: Destruction complete after 1s
google_storage_bucket.main: Creating...
google_storage_bucket.main: Still creating... [10s elapsed]
google_storage_bucket.main: Still creating... [20s elapsed]
google_storage_bucket.main: Still creating... [30s elapsed]
google_storage_bucket.main: Still creating... [40s elapsed]
google_storage_bucket.main: Still creating... [50s elapsed]
google_storage_bucket.main: Still creating... [1m0s elapsed]
google_storage_bucket.main: Still creating... [1m10s elapsed]
google_storage_bucket.main: Still creating... [1m20s elapsed]
google_storage_bucket.main: Still creating... [1m30s elapsed]
google_storage_bucket.main: Still creating... [1m40s elapsed]
google_storage_bucket.main: Still creating... [1m50s elapsed]
google_storage_bucket.main: Still creating... [2m0s elapsed]
google_storage_bucket.main: Still creating... [2m10s elapsed]
google_storage_bucket.main: Still creating... [2m20s elapsed]
google_storage_bucket.main: Still creating... [2m30s elapsed]
google_storage_bucket.main: Still creating... [2m40s elapsed]
google_storage_bucket.main: Still creating... [2m50s elapsed]
google_storage_bucket.main: Still creating... [3m0s elapsed]
google_storage_bucket.main: Still creating... [3m10s elapsed]
google_storage_bucket.main: Still creating... [3m20s elapsed]
google_storage_bucket.main: Still creating... [3m30s elapsed]
google_storage_bucket.main: Still creating... [3m40s elapsed]
google_storage_bucket.main: Still creating... [3m50s elapsed]
google_storage_bucket.main: Still creating... [4m0s elapsed]
google_storage_bucket.main: Still creating... [4m10s elapsed]
google_storage_bucket.main: Still creating... [4m20s elapsed]
google_storage_bucket.main: Still creating... [4m30s elapsed]
google_storage_bucket.main: Still creating... [4m40s elapsed]
google_storage_bucket.main: Still creating... [4m50s elapsed]
google_storage_bucket.main: Still creating... [5m0s elapsed]
google_storage_bucket.main: Still creating... [5m10s elapsed]
google_storage_bucket.main: Creation complete after 5m15s [id=terraform-import-example]

Apply complete! Resources: 1 imported, 1 added, 0 changed, 1 destroyed.

applyを実行した結果、かなりの時間がかかりました。

下記はdestroyの実行結果です。やはりTerraform管理下に置かれたようで、うまく消せてます。

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

google_storage_bucket.main: Destroying... [id=terraform-import-example]
google_storage_bucket.main: Destruction complete after 1s

Destroy complete! Resources: 1 destroyed.

・Appendix

参考文献はこちら

https://zenn.dev/kaminchu/scraps/e474e937bc0cbc

https://zenn.dev/sway/articles/terraform_biginner_varliable

https://zenn.dev/sway/articles/terraform_index_list

https://zenn.dev/wim/articles/terraform_loop

https://zenn.dev/kou_pg_0131/articles/tf-import-block

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*