Hi, I’m working with kms policy , I’ using templatefile, but the issue here is that terraform plan is rearranging the arn’s and adding a comma at the last arn.
When running terraform apply its failing with malformed policy. Is there any way to avoid this ?
Terraform 0.12.20
variable "allowed_resources" {
description = "list of all principal resources"
type = list(string)
default = [
"arn:aws:iam::xxxxxxxxx:user/a",
"arn:aws:iam::xxxxxxxxx:user/b",
"arn:aws:iam::xxxxxxxxx:user/c",
"arn:aws:iam::xxxxxxxxx:role/abc"
}
${jsonencode({
"Version": "2012-10-17",
"Id": "key-policy-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": allowed_resources
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": allowed_resources
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
}
]
})}
resource "aws_kms_key" "key" {
description = ""
tags = local.common_tags
policy = templatefile("${path.module}/key_policy.json.tpl", {
allowed_resources = var.allowed_resources
})
}
Terraform plan:
resource "aws_kms_key" "amp_key" {
arn = "arn:aws:kms:us-east-1:xxxx:key/xxxxxx"
customer_master_key_spec = "SYMMETRIC_DEFAULT"
enable_key_rotation = false
id = "xxxx-xxxxxxxxx"
is_enabled = true
key_id = "xxxxxxxxxxx-xxxxxx"
key_usage = "ENCRYPT_DECRYPT"
policy = jsonencode(
{
Id = "key-policy-1"
Statement = [
{
Action = "kms:*"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::xxxxxxxxxxxx:root"
}
Resource = "*"
Sid = "Enable IAM User Permissions"
},
{
Action = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
Effect = "Allow"
Principal = {
AWS = [
"arn:aws:iam::xxxxxxxxxxxx:user/a",
"arn:aws:iam::xxxxxxxxxxxx:user/c",
"arn:aws:iam::xxxxxxxxxxxx:role/abc",
"arn:aws:iam::xxxxxxxxxxxx:user/a",
Terraform apply error:
10:53:19 Error: MalformedPolicyDocumentException: Policy contains a statement with one or more invalid principals.
10:53:19
10:53:19 on main.tf line 8, in resource "kms_key" "key":
10:53:19 8: resource "aws_kms_key" "key" {