Skip to content

Commit

Permalink
adding encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
sohanyadav committed Sep 20, 2023
1 parent cebe10c commit be55127
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ eks/kubeconfig.yaml
*~

.idea/
.terraform.lock.hcl
25 changes: 25 additions & 0 deletions cluster.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
locals {
# Encryption
cluster_encryption_config = {
resources = var.cluster_encryption_config
provider_key_arn = aws_kms_key.cluster.arn
}
}

resource "aws_cloudwatch_log_group" "default" {
name = "/aws/eks/${var.environment_name}/cluster"
retention_in_days = 30
tags = local.tags
kms_key_id = aws_kms_key.cloudwatch_log.arn
}


resource "aws_eks_cluster" "cluster" {
name = var.environment_name
role_arn = aws_iam_role.cluster.arn
Expand All @@ -17,6 +33,15 @@ resource "aws_eks_cluster" "cluster" {
])
}

dynamic "encryption_config" {
for_each = [local.cluster_encryption_config]
content {
resources = lookup(encryption_config.value, "resources")
provider {
key_arn = lookup(encryption_config.value, "provider_key_arn")
}
}
}
enabled_cluster_log_types = var.cluster_logging

depends_on = [
Expand Down
59 changes: 59 additions & 0 deletions kms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "aws_kms_key" "cluster" {
description = "EKS Cluster ${var.environment_name} Encryption Config KMS Key"
enable_key_rotation = true
deletion_window_in_days = 30
policy = var.cluster_kms_policy
tags = local.tags
}


resource "aws_kms_key" "cloudwatch_log" {
description = "CloudWatch log group ${var.environment_name} Encryption Config KMS Key"
enable_key_rotation = true
deletion_window_in_days = 10
policy = data.aws_iam_policy_document.cloudwatch.json
tags = local.tags
}

data "aws_iam_policy_document" "cloudwatch" {
policy_id = "key-policy-cloudwatch"
statement {
sid = "Enable IAM User Permissions"
actions = [
"kms:*",
]
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format(
"arn:%s:iam::%s:root",
join("", data.aws_partition.current.*.partition),
data.aws_caller_identity.current.account_id
)
]
}
resources = ["*"]
}
statement {
sid = "AllowCloudWatchLogs"
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
effect = "Allow"
principals {
type = "Service"
identifiers = [
format(
"logs.%s.amazonaws.com",
data.aws_region.current.name
)
]
}
resources = ["*"]
}
}
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ variable "aws_load_balancer_controller_enabled" {
description = "Enable ALB controller by default"
}

variable "cluster_encryption_config" {
type = list(any)
default = ["secrets"]
description = "Cluster Encryption Config Resources to encrypt, e.g. ['secrets']"
}

variable "cluster_kms_policy" {
type = string
default = null
description = "Cluster Encryption Config KMS Key Resource argument - key policy"
}

variable "cluster_logging" {
default = [
"api",
Expand Down

0 comments on commit be55127

Please sign in to comment.