Skip to content

Instantly share code, notes, and snippets.

@mrbitsdcf
Created March 15, 2023 14:14
Show Gist options
  • Save mrbitsdcf/55b881adf76aad3d53062421bdd0dd4e to your computer and use it in GitHub Desktop.
Save mrbitsdcf/55b881adf76aad3d53062421bdd0dd4e to your computer and use it in GitHub Desktop.
Snipet Terraform for KMS
resource "aws_kms_key" "kms_key" {
deletion_window_in_days = var.deletion_window_in_days
enable_key_rotation = var.enable_key_rotation
key_usage = var.key_usage
customer_master_key_spec = var.customer_master_key_spec
multi_region = var.multi_region
is_enabled = var.is_enabled
tags = var.vpc_tags
policy = <<POLICY
{
"Id": "key-consolepolicy-3",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${var.aws_account_id}:root"
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow access for Key Administrators",
"Effect": "Allow",
"Principal": {
"AWS": [
"*"
]
},
"Action": [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
},
{
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": [
"*"
]
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
},
{
"Sid": "Allow attachment of persistent resources",
"Effect": "Allow",
"Principal": {
"AWS": [
"*"
]
},
"Action": [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
],
"Resource": "*",
"Condition": {
"Bool": {
"kms:GrantIsForAWSResource": "true"
}
}
}
]
}
POLICY
}
resource "aws_kms_alias" "kms_key_alias" {
name = "alias/${var.key_alias}"
target_key_id = aws_kms_key.kms_key.key_id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment