Skip to content

Commit e58ac8c

Browse files
authored
Merge pull request #4 from rajdban/main
CMK changes to Step Functions,SQS
2 parents b36d190 + 2efa1db commit e58ac8c

File tree

8 files changed

+131
-8
lines changed

8 files changed

+131
-8
lines changed

source/event-pipes/main.tf

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ resource "aws_iam_role_policy" "target" {
5858
})
5959
}
6060

61+
resource "aws_iam_role_policy" "kms" {
62+
role = aws_iam_role.this.id
63+
policy = jsonencode({
64+
Version = "2012-10-17"
65+
Statement = [
66+
{
67+
Effect = "Allow"
68+
Action = [
69+
"kms:Encrypt",
70+
"kms:Decrypt",
71+
"kms:ReEncrypt*",
72+
"kms:GenerateDataKey*",
73+
"kms:DescribeKey"
74+
],
75+
Resource = [
76+
"*"
77+
]
78+
},
79+
]
80+
})
81+
}
82+
6183
resource "aws_iam_role_policy" "invoke_dedup" {
6284
role = aws_iam_role.this.id
6385
policy = jsonencode({
@@ -81,6 +103,7 @@ resource "aws_pipes_pipe" "this" {
81103
role_arn = aws_iam_role.this.arn
82104
source = var.stream_arn
83105
target = var.eb_arn
106+
kms_key_identifier = var.kms_key_id
84107
#target = "arn:aws:events:eu-west-1:926516876030:event-bus/default"
85108

86109
source_parameters {
@@ -146,4 +169,4 @@ resource "aws_pipes_pipe" "this" {
146169
EOT
147170
}
148171

149-
}
172+
}

source/event-pipes/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ variable "target_event_source" {
2626
type = string
2727
description = "Source property of the event published to the target."
2828
default = null
29-
}
29+
}
30+
31+
variable "kms_key_id" {
32+
description = "Pass the ARN of the KMS Key Id for CMK"
33+
type = string
34+
default = null
35+
}

source/event_bridge/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ locals {
4545
}
4646
}
4747
resource "aws_cloudwatch_event_bus" "this" {
48-
name = var.event_bridge_name
48+
name = var.event_bridge_name
49+
kms_key_identifier = var.kms_key_id
4950
}
5051

5152
resource "aws_schemas_discoverer" "this" {
@@ -285,4 +286,4 @@ resource "aws_cloudwatch_event_target" "posted_queue" {
285286
dead_letter_config {
286287
arn = aws_sqs_queue.dlq.arn
287288
}
288-
}
289+
}

source/event_bridge/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ variable "state_machine_arn" {
5050
type = string
5151
default = ""
5252
description = "Pass the State Machine arn"
53-
}
53+
}
54+
55+
variable "kms_key_id" {
56+
description = "Pass the ARN of the KMS Key Id for CMK"
57+
type = string
58+
default = null
59+
}

source/main.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ provider "aws" {
33
}
44

55
provider "random" {}
6+
data "aws_caller_identity" "current" {}
67

78
locals {
89
definition_template = <<EOF
@@ -203,6 +204,10 @@ locals {
203204
}
204205
}
205206
EOF
207+
tags = {
208+
Name = "payments"
209+
Environment = "PROD"
210+
}
206211
}
207212

208213

@@ -225,6 +230,7 @@ module "dynamodb" {
225230
module "event_bridge" {
226231
source = "./event_bridge"
227232
event_bridge_name = var.event_bridge_name
233+
kms_key_id = module.kms.key_arn
228234
posting_queue_arn = module.posting_queue.arn
229235
posted_queue_arn = module.posted_queue.arn
230236
enrich_lambda_arn = module.enrich_lambda.arn
@@ -263,6 +269,7 @@ module "event-pipes" {
263269
stream_arn = module.dynamodb.stream_arn
264270
eb_arn = module.event_bridge.arn
265271
lambda_arn = module.dedup_lambda.arn
272+
kms_key_id = module.kms.key_arn
266273
target_event_detail_type = "TransactionAuthorized"
267274
target_event_source = "octank.payments.posting.visaIngest"
268275
}
@@ -369,6 +376,51 @@ module "fx_lambda" {
369376

370377
}
371378

379+
module "kms" {
380+
source = "terraform-aws-modules/kms/aws"
381+
version = "~> 1.0"
382+
description = "Securing SFN and EventBridge with KMS Keys"
383+
384+
# Aliases
385+
aliases = ["realtimepayments"]
386+
aliases_use_name_prefix = true
387+
388+
#key_owners = [data.aws_caller_identity.current.arn]
389+
policy = jsonencode({
390+
Version = "2012-10-17",
391+
Id = "default",
392+
Statement = [
393+
{
394+
Sid = "Enable IAM User Permissions"
395+
Effect = "Allow"
396+
Principal = {
397+
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
398+
}
399+
Action = "kms:*"
400+
Resource = "*"
401+
},
402+
{
403+
Sid = "AllowSFNEventBridgeToGenerateDataKey",
404+
Effect = "Allow",
405+
Principal = {
406+
#Service = "events.amazonaws.com"
407+
Service = ["events.amazonaws.com", "pipes.amazonaws.com", "states.amazonaws.com"]
408+
},
409+
Action = [
410+
"kms:Encrypt",
411+
"kms:Decrypt",
412+
"kms:ReEncrypt*",
413+
"kms:GenerateDataKey*",
414+
"kms:DescribeKey"
415+
]
416+
Resource = "*"
417+
}
418+
]
419+
})
420+
421+
tags = local.tags
422+
}
423+
372424
module "sfn" {
373425
source = "./stepfunction"
374426

@@ -379,6 +431,12 @@ module "sfn" {
379431
definition = local.definition_template
380432
publish = true
381433

434+
encryption_configuration = {
435+
type = "CUSTOMER_MANAGED_KMS_KEY"
436+
kms_key_id = module.kms.key_arn
437+
kms_data_key_reuse_period_seconds = 600
438+
}
439+
382440
logging_configuration = {
383441
include_execution_data = true
384442
level = "ALL"
@@ -390,6 +448,10 @@ module "sfn" {
390448
xray = true
391449
}
392450

451+
kms = {
452+
kms = true
453+
}
454+
393455
stepfunction = {
394456
stepfunction = true
395457
}

source/stepfunction/locals.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ locals {
1515
}
1616
}
1717

18+
kms = {
19+
kms = {
20+
actions = [
21+
"kms:*"
22+
]
23+
default_resources = ["*"]
24+
}
25+
}
26+
1827
stepfunction = {
1928
stepfunction = {
2029
actions = [
@@ -55,4 +64,4 @@ locals {
5564
}
5665
}
5766

58-
data "aws_caller_identity" "current" {}
67+
data "aws_caller_identity" "current" {}

source/stepfunction/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ resource "aws_sfn_state_machine" "this" {
2121
definition = var.definition
2222
publish = var.publish
2323

24+
dynamic "encryption_configuration" {
25+
for_each = length(var.encryption_configuration) > 0 ? [var.encryption_configuration] : []
26+
27+
content {
28+
type = encryption_configuration.value.type
29+
kms_key_id = try(encryption_configuration.value.kms_key_id, null)
30+
kms_data_key_reuse_period_seconds = try(encryption_configuration.value.kms_data_key_reuse_period_seconds, null)
31+
}
32+
}
33+
2434
dynamic "logging_configuration" {
2535
for_each = local.enable_logging ? [true] : []
2636

@@ -313,4 +323,4 @@ resource "aws_cloudwatch_log_group" "sfn" {
313323
kms_key_id = var.cloudwatch_log_group_kms_key_id
314324

315325
tags = merge(var.tags, var.cloudwatch_log_group_tags)
316-
}
326+
}

source/stepfunction/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ variable "publish" {
7373
default = false
7474
}
7575

76+
variable "encryption_configuration" {
77+
description = "Newly added encryption configuration which is used to encrypt data in the State Machine."
78+
type = any
79+
default = {}
80+
}
81+
7682
#################
7783
# CloudWatch Logs
7884
#################
@@ -256,4 +262,4 @@ variable "policy_statements" {
256262
description = "Map of dynamic policy statements to attach to IAM role"
257263
type = any
258264
default = {}
259-
}
265+
}

0 commit comments

Comments
 (0)