iganari / package-terraform

My Terraform Knowledge
0 stars 0 forks source link

for_each に関して #3

Open iganari opened 4 years ago

iganari commented 4 years ago

とにかく便利

iganari commented 4 years ago

3 つ以上

locals {
  vm_settings = [
    {
       name = "takagi-01"
       zone  = "asia-northeast1-a"
       machine_type = "n1-standard-1"
    },
    {
       name = "takagi-02"
       zone  = "asia-northeast1-b"
       machine_type = "n1-standard-2"
    },
    {
       name = "takagi-03"
       zone  = "asia-northeast1-c"
       machine_type = "n1-standard-4"
    },
  ]
}
resource "google_compute_instance" "takagi_yatu" {
  for_each = {for vm in local.vm_settings : vm.name => vm}
  name = each.value["name"]
  zone  = each.value["zone"]
  machine_type = each.value["machine_type"]
  ...(以下省略)...
}
iganari commented 4 years ago

普通の key/value もやる

iganari commented 4 years ago
locals {
  adobe_bucket_filter = {
    prd = {
      "hoge" = "hoge"
      "hage" = "hage"
    }
    stg = {
      "fuga" = "fuga"
      "debu" = "debu"
    }
    dev = {
      "neko" = "neko"
      "inu"  = "inu"
    }
  }
}
resource "google_logging_metric" "adobe_bucket_create_object" {
  for_each = local.adobe_bucket_filter[terraform.workspace]
  name     = each.key
  filter   = each.value
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}
iganari commented 4 years ago

local + for_each の使い方①


### GAE の TOP 画面の死活監視
locals {
  frontend_check_settings_2 = {
    dev = {
      display_name = "dev-frontend-healthcheck-2"
      project_id   = "iganari-dev"
    },
    stg = {
      display_name = "stg-frontend-healthcheck-2"
      project_id   = "iganari-stg"
    },
    prd = {
      display_name = "prd-frontend-healthcheck-2"
      project_id   = "iganari-prd"
    },
  }
}

### GAE の TOP 画面の死活監視
resource "google_monitoring_uptime_check_config" "iganari_gae-frontend_2_dev" {
  depends_on = [google_project_service.iganari_monitoring]

  for_each = local.frontend_check_settings_2

  display_name = each.value.display_name
  period       = "60s"
  timeout      = "5s"
  selected_regions = [
    "ASIA_PACIFIC",
    "USA"
  ]
  http_check {
    path         = "/"
    port         = "443"
    use_ssl      = true
    validate_ssl = true
  }
  monitored_resource {
    type = "uptime_url"
    labels = {
      project_id = each.value.project_id
      host       = "${each.value.project_id}.appspot.com"
    }
  }
}

local + for_each の使い方②

locals {
  frontend_check_settings_2 = {
    dev-frontend-healthcheck-2 = {
      project_id   = "iganari-dev"
    },
    stg-frontend-healthcheck-2 = {
      project_id   = "iganari-stg"
    },
    prd-frontend-healthcheck-2 = {
      project_id   = "iganari-prd"
    },
  }
}

### GAE の TOP 画面の死活監視
resource "google_monitoring_uptime_check_config" "iganari_gae-frontend_2_dev" {
  depends_on = [google_project_service.iganari_monitoring]

  for_each = local.frontend_check_settings_2

  display_name = each.key
  period       = "60s"
  timeout      = "5s"
  selected_regions = [
    "ASIA_PACIFIC",
    "USA"
  ]
  http_check {
    path         = "/"
    port         = "443"
    use_ssl      = true
    validate_ssl = true
  }
  monitored_resource {
    type = "uptime_url"
    labels = {
      project_id = each.value.project_id
      host       = "${each.value.project_id}.appspot.com"
    }
  }
}

となる

なお、 ① と ② は同じ結果になる