Skip to content

Commit

Permalink
UML-3418: add KMS key for elasticache
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-whitwell committed May 10, 2024
1 parent da55550 commit fcef01c
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions terraform/environment/elasticache.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ resource "aws_elasticache_replication_group" "lpa_redis" {
security_group_ids = [aws_security_group.lpa_redis_sg.id]
tags = local.default_tags
apply_immediately = true
at_rest_encryption_enabled = true
kms_key_id = aws_kms_alias.elasticache_kms_alias.target_key_arn
}

resource "aws_security_group" "lpa_redis_sg" {
Expand Down Expand Up @@ -70,3 +72,84 @@ resource "aws_security_group_rule" "lpa_redis_rules" {
cidr_blocks = each.value.target_type == "cidr_block" ? [each.value.target] : null
self = each.value.target_type == "self" ? each.value.target : null
}

resource "aws_kms_key" "elasticache_kms" {
description = "KMS Key for elasticache"
policy = data.aws_iam_policy_document.elasticache_kms_key.json
deletion_window_in_days = 7
}

data "aws_iam_policy_document" "elasticache_kms_key" {
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
resources = ["*"]
actions = ["kms:*"]

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

statement {
sid = "Allow access for Key Administrators"
effect = "Allow"
resources = ["*"]

actions = [
"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",
]

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

statement {
sid = "Allow Elasticache to use KMS key"
effect = "Allow"
resources = ["*"]

actions = [
"kms:DescribeKey",
"kms:GenerateDataKey*",
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:Decrypt"
]

condition {
test = "StringEquals"
variable = "kms:ViaService"
values = ["elasticache.region.amazonaws.com", "dax.region.amazonaws.com"]
}

condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [data.aws_caller_identity.current.account_id]
}
}
}

resource "aws_kms_alias" "elasticache_kms_alias" {
name = "alias/elasticache-lpa"
target_key_id = aws_kms_key.elasticache_kms.id
}

data "aws_caller_identity" "current" {}

0 comments on commit fcef01c

Please sign in to comment.