backup_ami_key.tf 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. resource "aws_kms_key" "key" {
  2. description = "Key for AMI Backups"
  3. enable_key_rotation = true
  4. policy = data.aws_iam_policy_document.kms_policy.json
  5. tags = merge(
  6. var.standard_tags,
  7. { "Name" = "ami_backup_key" },
  8. var.tags
  9. )
  10. }
  11. resource "aws_kms_alias" "alias" {
  12. name = "alias/ami_backup_key"
  13. target_key_id = aws_kms_key.key.key_id
  14. }
  15. data "aws_iam_policy_document" "kms_policy" {
  16. policy_id = "backup-ami-key-policy"
  17. statement {
  18. sid = "Enable IAM User Permissions"
  19. effect = "Allow"
  20. principals {
  21. type = "AWS"
  22. identifiers = [
  23. # The 'root' account is the entire account, we don't want that
  24. #"arn:${var.aws_partition}:iam::${var.aws_account_id}:root"
  25. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin", # MDRAdmin as a break glass
  26. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" # Terraformer always gets full access
  27. ]
  28. }
  29. actions = [ "kms:*" ]
  30. resources = [ "*" ]
  31. }
  32. statement {
  33. sid = "Allow access for Key Administrators"
  34. effect = "Allow"
  35. principals {
  36. type = "AWS"
  37. identifiers = [ "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer" ]
  38. }
  39. actions = [
  40. "kms:Create*",
  41. "kms:Describe*",
  42. "kms:Enable*",
  43. "kms:List*",
  44. "kms:Put*",
  45. "kms:Update*",
  46. "kms:Revoke*",
  47. "kms:Disable*",
  48. "kms:Get*",
  49. "kms:Delete*",
  50. "kms:TagResource",
  51. "kms:UntagResource",
  52. "kms:ScheduleKeyDeletion",
  53. "kms:CancelKeyDeletion"
  54. ]
  55. resources = [ "*" ]
  56. }
  57. statement {
  58. sid = "Allow use of the key"
  59. effect = "Allow"
  60. principals {
  61. type = "AWS"
  62. identifiers = [
  63. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  64. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/dlm-lifecycle-role"
  65. ]
  66. }
  67. actions = [
  68. "kms:Encrypt",
  69. "kms:Decrypt",
  70. "kms:ReEncrypt*",
  71. "kms:GenerateDataKey*",
  72. "kms:DescribeKey"
  73. ]
  74. resources = [ "*" ]
  75. }
  76. statement {
  77. sid = "Allow attachment of persistent resources"
  78. effect = "Allow"
  79. principals {
  80. type = "AWS"
  81. identifiers = [
  82. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  83. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/dlm-lifecycle-role"
  84. ]
  85. }
  86. actions = [
  87. "kms:CreateGrant",
  88. "kms:ListGrants",
  89. "kms:RevokeGrant"
  90. ]
  91. resources = [ "*" ]
  92. }
  93. }