kms.tf 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. data "aws_iam_policy_document" "key" {
  2. statement {
  3. actions = ["kms:*"]
  4. effect = "Allow"
  5. resources = ["*"]
  6. principals {
  7. type = "AWS"
  8. identifiers = ["arn:${local.partition}:iam::${local.account_id}:root"]
  9. }
  10. }
  11. statement {
  12. actions = ["kms:GenerateDataKey*"]
  13. effect = "Allow"
  14. resources = ["*"]
  15. condition {
  16. test = "StringLike"
  17. variable = "kms:EncryptionContext:aws:cloudtrail:arn"
  18. values = local.kms_key_encrypt_resources
  19. }
  20. principals {
  21. type = "Service"
  22. identifiers = ["cloudtrail.amazonaws.com"]
  23. }
  24. }
  25. statement {
  26. actions = [
  27. "kms:Encrypt*",
  28. "kms:Decrypt*",
  29. "kms:ReEncrypt*",
  30. "kms:GenerateDataKey*",
  31. "kms:Describe*",
  32. ]
  33. effect = "Allow"
  34. resources = ["*"]
  35. principals {
  36. type = "Service"
  37. identifiers = ["logs.${var.region}.amazonaws.com"]
  38. }
  39. }
  40. statement {
  41. actions = ["kms:Describe*"]
  42. effect = "Allow"
  43. resources = ["*"]
  44. principals {
  45. type = "Service"
  46. identifiers = ["cloudtrail.amazonaws.com"]
  47. }
  48. }
  49. }
  50. resource "aws_kms_key" "this" {
  51. deletion_window_in_days = 7
  52. description = "CloudTrail Encryption Key"
  53. enable_key_rotation = true
  54. policy = data.aws_iam_policy_document.key.json
  55. tags = merge(
  56. {
  57. "Name" = "cloudtrail-key"
  58. },
  59. var.tags
  60. )
  61. }
  62. resource "aws_kms_alias" "this" {
  63. name = "alias/cloudtrail_key"
  64. target_key_id = aws_kms_key.this.id
  65. }