I'm trying to concatenate a declared variable within the Terraform data assignation to build a dynamic call.
Having the following code:
# Policy 1
data "aws_iam_policy_document" "1_s3_access_policy" {
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
]
resources = [
"arn:aws:s3:::1_s3_access_policy/*",
"arn:aws:s3:::1_s3_access_policy",
]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::67435677645:user/d2c-user-us-west-1"]
}
}
}
# policy 2
data "aws_iam_policy_document" "2_s3_access_policy" {
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
]
resources = [
"arn:aws:s3:::2_s3_access_policy/*",
"arn:aws:s3:::2_s3_access_policy",
]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::67435677645:user/d2c-user-us-west-1"]
}
}
}
# Policy 3
...
variable "s3_bucket_names" {
type = list(any)
default = ["1_s3_access_policy", "2_s3_access_policy", "3_s3_access_policy"]
}
module "platform-cloud" {
source = "./module"
count = length(var.s3_bucket_names) //count will be 3
bucket_name = var.s3_bucket_names[count.index]
sse_algorithm = "aws:kms"
iam_policy_document = data.aws_iam_policy_document.${var.s3_bucket_names[count.index]}.json
}
It's failing with error:
Error: Invalid character
on main.tf line 10, in module "platform-cloud":
iam_policy_document = data.aws_iam_policy_document.${var.s3_bucket_names[count.index]}.json
This character is not used within the language.
Is there a way to concatenate a variable within terraform data call?