I want to do the following
Update the KMS key resource to include lifecycle precondition
If the referenced policy is null, use the constructed local policy
Add a validator for the policy that if not set and the key type is direct, then direct_key_principal should not be empty
resource "aws_kms_key" "direct_key" {
description = var.description
enable_key_rotation = true
deletion_window_in_days = var.deletion_window
policy = var.policy != null ? var.policy : data.aws_iam_policy_document.kms_key_policy_direct[0].json
key_usage = var.key_usage
customer_master_key_spec = var.customer_master_key_spec
lifecycle {
precondition {
condition = var.policy == null && var.key_type == "direct" && length(values(var.direct_key_principal)[0]) > 0
error_message = "When the policy is not set and the key type is direct, all direct_key_principal elements should be non-empty."
}
}
variable "direct_key_principal" {
description = "Principal Information - Type & Identifier required for a 'direct' key"
type = map(list(string))
default = {
AWS = []
}
validation {
condition = alltrue([for principal_type in keys(var.direct_key_principal) : contains(["AWS", "Service"], principal_type)])
error_message = "Valid values for Principal type are AWS and Service."
}
}
I get this error message below
│ 12: condition = var.policy == null && var.key_type == "direct" && length(values(var.direct_key_principal)[0]) > 0
│ ├────────────────
│ │ var.direct_key_principal is map of list of string with 1 element
│ │ var.key_type is "direct"
│ │ var.policy is "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Sid\": \"\",\n \"Effect\": \"Allow\",\n \"Action\": \"kms:*\",\n \"Resource\": \"arn:aws:*:*:765862725291:*\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::765862725291499556464691:root\"\n }\n },\n {\n \"Sid\": \"sid1\",\n \"Effect\": \"Allow\",\n \"Action\": [\n \"kms:TagResource\",\n \"kms:List*\",\n \"kms:Get*\",\n \"kms:Describe*\",\n \"kms:Decrypt\"\n ],\n \"Resource\": [\n \"arn:aws:*:*:499556464691:*\",\n \"arn:aws:*:*:999999955:*\"\n ],\n \"Principal\": {\n \"AWS\": \"999999955\"\n }\n }\n ]\n}"
│
│ When the policy is not set and the key type is direct, all
│ direct_key_principal elements should be non-empty.