I’ve been trying to figure this one out for a few days and I can’t seem to wrap my head around a solution. I have two resources: snowflake_view
and snowflake_view_grant
. As seen here:
resource “snowflake_view” “this” {
for_each = local.view_map
database = each.value.database
schema = each.value.schema
name = each.value.name
comment = each.value.comment
statement = each.value.statement
}resource “snowflake_view_grant” “grant” {
for_each = local.acl_map
database_name = snowflake_view.this[each.key].database
schema_name = snowflake_view.this[each.key].schema
view_name = snowflake_view.this[each.key].name
privilege = try(each.value.privilege, null)
roles = try(each.value.roles, null)lifecycle {
replace_triggered_by = [snowflake_view.this[each.key].id]
}
}
For a single snowflake_view
, I can have zero to many snowflake_view_grant
resources. I also have added a lifecycle argument to snowflake_view_grant
because I want each grant basically tied to the hip of its view. If the view gets destroyed/recreated I want the associated grants to also follow suit.
In my locals I have the following defined:
locals {
view_map = {
test_view_1 = {
database = “DL_TEST”
schema = “TEST”
name = “TEST_VIEW_1”
comment = null
statement = “SELECT 1 as HELLO;”
privilege = “SELECT”
roles = [“ROLE_A”]
},
test_view_2 = {
database = “DL_TEST”
schema = “TEST”
name = “TEST_VIEW_2”
comment = null
statement = “SELECT 1 as HELLO;”
privilege = “SELECT”
roles = [“ROLE_B”]
}
}acl_map = { test_view_1 = { privilege = "SELECT" roles = ["ROLE_A"] }, test_view_1 = { privilege = "REFERENCES" roles = ["ROLE_B"] } test_view_2 = { privilege = "SELECT" roles = ["ROLE_B"] } }
}
The problem I am running into is terraform doesn’t seem to like duplicate values in maps, so one of the “test_view_1” grants gets ignored. Is there a different way to structure this so that I can still implicitly tell Terraform there is a relationship between specific views and grants and so my lifecycle argument functions how I want it to?