Skip to content

Commit f1698a2

Browse files
feat: add reusable KMS module with key rotation and CloudTrail support (#195)
* feat: add reusable KMS module with key rotation and CloudTrail support * chore: update terraform lock files for all platforms --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent add5b47 commit f1698a2

File tree

9 files changed

+210
-93
lines changed

9 files changed

+210
-93
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ terraform-docs markdown . > README.md
234234
This is useful for some people and takes no effort on our side. We do this manually so far. Automating this and having this in pre-commit would be far better.
235235
I'm writing this here as a TODO.
236236

237+
## Modules
238+
239+
Reusable Terraform modules in `modules/`:
240+
241+
| Module | Description |
242+
|--------|-------------|
243+
| `aws-bootstrap` | S3 backend + optional DynamoDB table for state management |
244+
| `certificate` | ACM certificate with DNS validation |
245+
| `cluster-autoscaler` | Kubernetes Cluster Autoscaler with IRSA |
246+
| `eks` | EKS cluster with managed/self-managed node groups |
247+
| `github-oidc` | GitHub Actions OIDC provider + IAM role |
248+
| `kms` | Shared KMS key with key rotation, CloudWatch Logs and CloudTrail encryption |
249+
| `wireguard-ec2` | WireGuard VPN on EC2 with Packer AMI |
250+
237251
## naming conventions
238252
Basically just [this](https://www.terraform-best-practices.com/naming)
239253

examples/03-aws-github-actions-oidc/.terraform.lock.hcl

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/04-aws-wireguard-vpn/.terraform.lock.hcl

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/05-aws-complete/.terraform.lock.hcl

Lines changed: 53 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/06-minimal-aws-terraform-bootstrap/bootstrap/.terraform.lock.hcl

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/kms/main.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
locals {
2+
kms_policy = jsonencode({
3+
Version = "2012-10-17"
4+
Statement = [
5+
{
6+
Sid = "Allow everything in this AWS account to use this KMS key"
7+
Effect = "Allow"
8+
Principal = {
9+
AWS = "arn:aws:iam::${var.account_id}:root"
10+
}
11+
Action = "kms:*"
12+
Resource = "*"
13+
},
14+
{
15+
Sid = "Allow cloudwatch log group encryption"
16+
Effect = "Allow"
17+
Principal = {
18+
Service = "logs.${var.region}.amazonaws.com"
19+
}
20+
Action = [
21+
"kms:Encrypt*",
22+
"kms:Decrypt*",
23+
"kms:ReEncrypt*",
24+
"kms:GenerateDataKey*",
25+
"kms:Describe*"
26+
]
27+
Resource = "*"
28+
},
29+
{
30+
Sid = "Allow cloudtrail encryption"
31+
Effect = "Allow"
32+
Principal = {
33+
Service = "cloudtrail.amazonaws.com"
34+
}
35+
Action = [
36+
"kms:Encrypt*",
37+
"kms:Decrypt*",
38+
"kms:ReEncrypt*",
39+
"kms:GenerateDataKey*",
40+
"kms:Describe*"
41+
]
42+
Resource = "*"
43+
}
44+
]
45+
})
46+
}
47+
48+
resource "aws_kms_key" "main" {
49+
#checkov:skip=CKV_AWS_109:The asterisk identifies the KMS key to which the key policy is attached
50+
#checkov:skip=CKV_AWS_111:The asterisk identifies the KMS key to which the key policy is attached
51+
#checkov:skip=CKV_AWS_356:The asterisk identifies the KMS key to which the key policy is attached
52+
description = "Shared KMS key"
53+
deletion_window_in_days = var.deletion_window_in_days
54+
key_usage = "ENCRYPT_DECRYPT"
55+
enable_key_rotation = var.key_rotation_enabled
56+
is_enabled = true
57+
58+
policy = local.kms_policy
59+
}

modules/kms/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "kms_key" {
2+
description = "KMS key resource"
3+
value = aws_kms_key.main
4+
}
5+
6+
output "kms_key_arn" {
7+
description = "KMS key ARN"
8+
value = aws_kms_key.main.arn
9+
}
10+
11+
output "kms_key_id" {
12+
description = "KMS key ID"
13+
value = aws_kms_key.main.key_id
14+
}

modules/kms/variables.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "account_id" {
2+
description = "AWS account ID"
3+
type = string
4+
}
5+
6+
variable "region" {
7+
description = "AWS region name for the KMS key"
8+
type = string
9+
}
10+
11+
variable "key_rotation_enabled" {
12+
description = "Enable automatic key rotation"
13+
type = bool
14+
default = true
15+
}
16+
17+
variable "deletion_window_in_days" {
18+
description = "Duration in days after which the key is deleted after destruction"
19+
type = number
20+
default = 10
21+
}

modules/kms/versions.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 4.57.0, < 6.0.0"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)