# locals ===================================================================== data "aws_caller_identity" "current" {} data "aws_partition" "current" {} locals { tags = merge( { ManagedBy = "terraform" Module = "security/kms" }, var.tags, ) default_policy = jsonencode({ Version = "2012-10-17" Statement = concat( [{ Sid = "Enable IAM User Permissions" Effect = "Allow" Principal = { AWS = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root" } Action = ["kms:*"] Resource = ["*"] }], length(var.cross_account_principals) > 0 ? [{ Sid = "AllowCrossAccountUse" Effect = "Allow" Principal = { AWS = var.cross_account_principals } Action = [ "kms:Decrypt", "kms:DescribeKey", "kms:Encrypt", "kms:GenerateDataKey*", "kms:ReEncrypt*", ] Resource = ["*"] }] : [], ) }) effective_policy = coalesce(var.policy, local.default_policy) }