locals { account_id = data.aws_caller_identity.current.account_id } resource "aws_kms_key" "encryption_key" { description = "This key is used to encrypt bucket objects" deletion_window_in_days = 10 policy = data.aws_iam_policy_document.kms_key_policy_doc.json enable_key_rotation = true tags = var.tags } data "aws_iam_policy_document" "kms_key_policy_doc" { #checkov:skip=CKV_AWS_109: "Ensure IAM policies does not allow permissions management / resource exposure without constraints" #checkov:skip=CKV_AWS_111: "Ensure IAM policies does not allow write access without constraints" statement { sid = "Enable IAM User Permissions" effect = "Allow" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:aws:iam::${local.account_id}:root"] } } statement { sid = "Allow access for Key Administrators" effect = "Allow" actions = ["kms:*"] resources = ["${var.codepipeline_role_arn}"] principals { type = "AWS" identifiers = [ var.codepipeline_role_arn ] } } statement { sid = "Allow use of the key" effect = "Allow" actions = [ "kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:DescribeKey" ] resources = ["${var.codepipeline_role_arn}"] principals { type = "AWS" identifiers = [ var.codepipeline_role_arn ] } } statement { sid = "Allow attachment of persistent resources" effect = "Allow" actions = [ "kms:CreateGrant", "kms:ListGrants", "kms:RevokeGrant" ] resources = ["${var.codepipeline_role_arn}"] principals { type = "AWS" identifiers = [ var.codepipeline_role_arn ] } condition { test = "Bool" variable = "kms:GrantIsForAWSResource" values = ["true"] } } }