Hi Terraform Fam!
I am successfully using import blocks, but unfortunately, the option -generate-config-out
can not be used with for_each
The problem that I am facing is that if I run this plan, the existing policies are going to be destroyed because this line
policy = jsonencode({})
in my code (I had to add a policy because this field is mandatory)
. How can I tell Terraform I want to keep the policy the same?
variable "users" {
type = list(object({
name = string
policies = list(string)
}))
default = [
{ name = "user1", policies = ["p1", "p2", "p99", "p200"] },
{ name = "user2", policies = ["p3", "p4"] },
{ name = "user3", policies = ["p5", "p6"] },
{ name = "user4", policies = ["p7", "p8"] },
]
}
import {
for_each = toset(concat([for u in var.users : u.policies]...))
to = aws_iam_policy.this[each.value]
id = format("arn:aws:iam::054445454545:policy/%s", each.value)
}
resource "aws_iam_policy" "this" {
for_each = toset(concat([for u in var.users : u.policies]...))
policy = jsonencode({})
}