data "aws_caller_identity" "current" {}

data "aws_iam_policy_document" "elasticache" {
  statement {
    sid       = "EnableRootAccess"
    effect    = "Allow"
    resources = ["*"]
    actions   = ["kms:*"]

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
    }
  }

  statement {
    sid       = "AllowElastiCacheKMSAccess"
    effect    = "Allow"
    resources = ["*"]
    actions = [
      "kms:Decrypt",
      "kms:GenerateDataKey*",
      "kms:CreateGrant",
      "kms:DescribeKey",
    ]

    principals {
      type        = "Service"
      identifiers = ["elasticache.amazonaws.com"]
    }
  }
}
