jagannath shetagar
jagannath shetagar

Reputation: 13

Terraform need to skip/ignore resource creation if contains specified strings in the variables (map)

Hi below is my sample terraform code, I want to skip/ignore bucket creation for two variables develop and production mentioned in the locals, without removing from locals. Can someone please help. Thanks in advance.

    locals {
      buckets = {
        sandbox1    = "sandbox1-bucket",
        sandbox2    = "sandbox2-bucket",
        develop     = "develop-bucket",
        production  = "production-bucket"
      }
    }

   resource "google_storage_bucket" "gcs_buckets" {    
     for_each = local.buckets
     name          = each.value
     location      = "US"
     force_destroy = true

     lifecycle_rule {
      condition {
        age = 30
      }
      action {
        type = "Delete"
      }
    }
  }

Upvotes: 0

Views: 1064

Answers (1)

Oleksandr Bushkovskyi
Oleksandr Bushkovskyi

Reputation: 825

You can use for expression with filtering.

    locals {
      buckets = {
        sandbox1    = "sandbox1-bucket",
        sandbox2    = "sandbox2-bucket",
        develop     = "develop-bucket",
        production  = "production-bucket"
      }
    }

   resource "google_storage_bucket" "gcs_buckets" {
     for_each = {
       for k, name in local.buckets: k => name
       if k != "develop" && k != "production"
     }
     name          = each.value
     location      = "US"
     force_destroy = true

     lifecycle_rule {
      condition {
        age = 30
      }
      action {
        type = "Delete"
      }
    }
  }

Upvotes: 1

Related Questions