-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventbridge.tf
More file actions
143 lines (123 loc) · 4.43 KB
/
Copy patheventbridge.tf
File metadata and controls
143 lines (123 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
################################################################################
# Fraud pipeline — Connect contact events → EventBridge → fraud scorer Lambda
################################################################################
data "archive_file" "fraud_scorer" {
type = "zip"
source_file = "${path.module}/lambda/fraud_scorer.py"
output_path = "${path.module}/lambda/fraud_scorer.zip"
}
resource "aws_cloudwatch_event_rule" "connect_contact_events" {
name = "${var.instance_alias}-connect-contact-events"
description = "Route Amazon Connect contact events to the fraud scorer Lambda"
event_pattern = jsonencode({
source = ["aws.connect"]
detail-type = ["Amazon Connect Contact Event"]
detail = {
instanceArn = [try(module.amazon_connect.instance.arn, "")]
}
})
}
resource "aws_cloudwatch_event_target" "fraud_scorer" {
rule = aws_cloudwatch_event_rule.connect_contact_events.name
target_id = "fraud-scorer-lambda"
arn = aws_lambda_function.fraud_scorer.arn
}
resource "aws_lambda_permission" "eventbridge_fraud_scorer" {
statement_id = "AllowEventBridgeInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.fraud_scorer.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.connect_contact_events.arn
}
resource "aws_cloudwatch_event_rule" "fraud_demo_events" {
name = "${var.instance_alias}-fraud-demo-events"
description = "Synthetic fraud test events for pipeline validation (scripts/inject-test-event.sh)"
event_pattern = jsonencode({
source = ["fraud.demo"]
})
}
resource "aws_cloudwatch_event_target" "fraud_scorer_demo" {
rule = aws_cloudwatch_event_rule.fraud_demo_events.name
target_id = "fraud-scorer-lambda-demo"
arn = aws_lambda_function.fraud_scorer.arn
}
resource "aws_lambda_permission" "eventbridge_fraud_scorer_demo" {
statement_id = "AllowEventBridgeDemoInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.fraud_scorer.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.fraud_demo_events.arn
}
data "aws_iam_policy_document" "fraud_scorer_assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "fraud_scorer" {
name = "${var.instance_alias}-fraud-scorer"
assume_role_policy = data.aws_iam_policy_document.fraud_scorer_assume.json
}
resource "aws_iam_role_policy_attachment" "fraud_scorer_basic" {
role = aws_iam_role.fraud_scorer.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
data "aws_iam_policy_document" "fraud_scorer" {
statement {
sid = "WriteFraudEvents"
effect = "Allow"
actions = [
"s3:PutObject",
]
resources = ["${aws_s3_bucket.fraud_events.arn}/raw/*"]
}
statement {
sid = "VelocityTracking"
effect = "Allow"
actions = [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
]
resources = [aws_dynamodb_table.fraud_velocity.arn]
}
statement {
sid = "StartFraudWorkflow"
effect = "Allow"
actions = [
"states:StartExecution",
]
resources = [aws_sfn_state_machine.fraud_workflow.arn]
}
}
resource "aws_iam_role_policy" "fraud_scorer" {
name = "fraud-scorer"
role = aws_iam_role.fraud_scorer.id
policy = data.aws_iam_policy_document.fraud_scorer.json
}
resource "aws_lambda_function" "fraud_scorer" {
function_name = "${var.instance_alias}-fraud-scorer"
role = aws_iam_role.fraud_scorer.arn
handler = "fraud_scorer.handler"
runtime = "python3.12"
timeout = 30
filename = data.archive_file.fraud_scorer.output_path
source_code_hash = data.archive_file.fraud_scorer.output_base64sha256
environment {
variables = {
FRAUD_EVENTS_BUCKET = aws_s3_bucket.fraud_events.id
VELOCITY_TABLE_NAME = aws_dynamodb_table.fraud_velocity.name
FRAUD_STATE_MACHINE_ARN = aws_sfn_state_machine.fraud_workflow.arn
VELOCITY_THRESHOLD = tostring(var.fraud_velocity_threshold)
KNOWN_GOOD_CALLERS = join(",", var.fraud_known_good_callers)
}
}
depends_on = [
aws_iam_role_policy_attachment.fraud_scorer_basic,
aws_sfn_state_machine.fraud_workflow,
]
}