I have created a group of objects(aws_iam_user) using for_each, I want to use these objects in another resource for creating new(aws_iam_access_key) objects. Below is the code .
resource "aws_iam_user" "unit-test-user" {
for_each = { for x in var.env-prefix: x => x}
name = "${var.implementation-shortname}-unit-test-user-${each.value}"
tags = {
}
}
resource "aws_iam_access_key" "unit-test-user-key" {
user = [ for x in aws_iam_user.unit-test-user : x.name ]
}
Am getting the following error
╷
│ Error: Incorrect attribute value type
│
│ on 04 - iam-init-users.tf line 17, in resource "aws_iam_access_key" "unit-test-user-key":
│ 17: user = [ for x in aws_iam_user.unit-test-user : x.name ]
│ ├────────────────
│ │ aws_iam_user.unit-test-user is object with 1 attribute "dev"
│
│ Inappropriate value for attribute "user": string required.
You are taking a list (or set) and turning into a map. You can just use a set for this, so either declare the variable as a set or convert it to a set.
When using a set you can use either each.key or each.value and they will return the same thing.
variable "env-prefix" {
type = set(string)
default = ["prod","test","dev"]
}
resource "aws_iam_user" "unit-test-user" {
for_each = var.env-prefix // or toset(var.env-prefix )
name = "${var.implementation-shortname}-unit-test-user-${each.key}"
tags = {}
}