Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - How to modify the map object in Terraform?

Tags:

terraform

I have a map of the format:

datasets   = {
  test = {
    access = [
      {
        role = "OWNER"
        group_by_email = "grp-owner"
      },
      {
        role = "READER"
        group_by_email = "grp-reader"
      }
    ]
  }
  test2 = {
    access = [
      {
        role = "OWNER"
        group_by_email = "grp-owner"
      },
      {
        role = "WRITER"
        group_by_email = "grp-writer"
      }
    ]
  }
}

I a looking for a way to update the map(datasets/{dataset_name}/access/group_by_email) to add the domain name:

datasets   = {
  test = {
    access = [
      {
        role = "OWNER"
        group_by_email = "[email protected]"
      },
      {
        role = "READER"
        group_by_email = "[email protected]"
      }
    ]
  }
  test2 = {
    access = [
      {
        role = "OWNER"
        group_by_email = "[email protected]"
      },
      {
        role = "WRITER"
        group_by_email = "[email protected]"
      }
    ]
  }
}
like image 847
JDev Avatar asked Nov 28 '25 04:11

JDev


1 Answers

You can do that as follows:


variable "datasets" {
  default   = {
    test = {
      access = [
        {
          role = "OWNER"
          group_by_email = "grp-owner"
        },
        {
          role = "READER"
          group_by_email = "grp-reader"
        }
      ]
    }
    test2 = {
      access = [
        {
          role = "OWNER"
          group_by_email = "grp-owner"
        },
        {
          role = "WRITER"
          group_by_email = "grp-writer"
        }
      ]
    }
  }
}

locals {
  modified = { for k1, v1 in var.datasets:
                k1 => {access = [
                          for access in v1["access"]:  
                           merge(access, {group_by_email = "${access.group_by_email}@test.com"})
                         ]
                      }
             }
}

which results in:

 {
  "test" = {
    "access" = [
      {
        "group_by_email" = "[email protected]"
        "role" = "OWNER"
      },
      {
        "group_by_email" = "[email protected]"
        "role" = "READER"
      },
    ]
  }
  "test2" = {
    "access" = [
      {
        "group_by_email" = "[email protected]"
        "role" = "OWNER"
      },
      {
        "group_by_email" = "[email protected]"
        "role" = "WRITER"
      },
    ]
  }
}
like image 73
Marcin Avatar answered Nov 29 '25 20:11

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!