Skip to content

Commit 72fa89e

Browse files
authored
feat: Set kms_key_identifier for EventBridge archives (#175)
1 parent d80f7e6 commit 72fa89e

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

examples/with-archive/main.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ provider "aws" {
77
skip_credentials_validation = true
88
}
99

10+
data "aws_caller_identity" "current" {}
11+
data "aws_region" "current" {}
12+
13+
1014
module "eventbridge" {
1115
source = "../../"
1216

@@ -62,6 +66,7 @@ module "eventbridge_archive_only" {
6266
"detail-type" : ["EC2 Instance Launch Successful"]
6367
}
6468
)
69+
kms_key_identifier = module.kms.key_id
6570
}
6671
}
6772

@@ -79,3 +84,48 @@ resource "random_pet" "this" {
7984
resource "aws_cloudwatch_event_bus" "existing_bus" {
8085
name = "${random_pet.this.id}-existing-bus"
8186
}
87+
88+
module "kms" {
89+
source = "terraform-aws-modules/kms/aws"
90+
version = "~> 2.0"
91+
description = "KMS key for cross region automated backups replication"
92+
93+
# Aliases
94+
aliases = ["test"]
95+
aliases_use_name_prefix = true
96+
key_statements = [
97+
{
98+
sid = "Allow eventbridge"
99+
principals = [
100+
{
101+
type = "Service"
102+
identifiers = ["events.amazonaws.com"]
103+
}
104+
]
105+
actions = [
106+
"kms:DescribeKey",
107+
"kms:GenerateDataKey",
108+
"kms:Decrypt"
109+
]
110+
resources = ["*"]
111+
conditions = [
112+
{
113+
test = "StringEquals"
114+
variable = "kms:EncryptionContext:aws:events:event-bus:arn"
115+
values = [
116+
"arn:aws:events:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:event-bus/example",
117+
]
118+
},
119+
{
120+
test = "StringEquals"
121+
variable = "aws:SourceArn"
122+
values = [
123+
"arn:aws:events:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:event-bus/example",
124+
]
125+
}
126+
]
127+
}
128+
]
129+
130+
key_owners = [data.aws_caller_identity.current.arn]
131+
}

examples/with-pipes/main.tf

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module "eventbridge" {
3333
}
3434

3535
api_destinations = {
36-
smee = { # This key should match the key inside "connections"
36+
smee = {
37+
# This key should match the key inside "connections"
3738
description = "my smee endpoint"
3839
invocation_endpoint = "https://smee.io/6hx6fuQaVUKLfALn"
3940
http_method = "POST"
@@ -47,7 +48,8 @@ module "eventbridge" {
4748
source = aws_sqs_queue.source.arn
4849
target = aws_sqs_queue.target.arn
4950

50-
enrichment = "smee" # This key should match the key inside "api_destinations"
51+
enrichment = "smee"
52+
# This key should match the key inside "api_destinations"
5153
enrichment_parameters = {
5254
input_template = jsonencode({ input : "yes" })
5355

@@ -325,6 +327,16 @@ module "eventbridge" {
325327
}
326328
}
327329

330+
custom_kms_key = {
331+
source = aws_sqs_queue.source.arn
332+
target = aws_sqs_queue.target.arn
333+
kms_key_identifier = module.kms.key_id
334+
335+
tags = {
336+
Pipe = "minimal"
337+
}
338+
}
339+
328340
# Minimal with IAM role created outside of the module
329341
minimal_external_role = {
330342
create_role = false
@@ -358,7 +370,6 @@ resource "random_pet" "this" {
358370
length = 2
359371
}
360372

361-
362373
###############################
363374
# API Destination / Connection
364375
###############################

main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,10 @@ resource "aws_cloudwatch_event_archive" "this" {
287287
name = lookup(each.value, "name", each.key)
288288
event_source_arn = try(each.value["event_source_arn"], aws_cloudwatch_event_bus.this[0].arn)
289289

290-
description = lookup(each.value, "description", null)
291-
event_pattern = lookup(each.value, "event_pattern", null)
292-
retention_days = lookup(each.value, "retention_days", null)
290+
description = lookup(each.value, "description", null)
291+
event_pattern = lookup(each.value, "event_pattern", null)
292+
retention_days = lookup(each.value, "retention_days", null)
293+
kms_key_identifier = lookup(each.value, "kms_key_identifier", null)
293294
}
294295

295296
resource "aws_cloudwatch_event_permission" "this" {
@@ -667,8 +668,9 @@ resource "aws_pipes_pipe" "this" {
667668
source = each.value.source
668669
target = each.value.target
669670

670-
description = lookup(each.value, "description", null)
671-
desired_state = lookup(each.value, "desired_state", null)
671+
kms_key_identifier = lookup(each.value, "kms_key_identifier", null)
672+
description = lookup(each.value, "description", null)
673+
desired_state = lookup(each.value, "desired_state", null)
672674

673675
dynamic "source_parameters" {
674676
for_each = try([each.value.source_parameters], [])

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 6.0"
7+
version = ">= 6.2"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)