-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbounce_handling.tf
More file actions
231 lines (196 loc) · 6.59 KB
/
bounce_handling.tf
File metadata and controls
231 lines (196 loc) · 6.59 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# ============================================================================
# SNS Topics for Bounce and Complaint Notifications
# ============================================================================
resource "aws_sns_topic" "ses_bounces" {
name = "ses-email-bounces"
tags = {
Name = "SES Email Bounces"
Environment = var.environment
ManagedBy = "Terraform"
}
}
resource "aws_sns_topic" "ses_complaints" {
name = "ses-email-complaints"
tags = {
Name = "SES Email Complaints"
Environment = var.environment
ManagedBy = "Terraform"
}
}
# ============================================================================
# Lambda Function for Bounce and Complaint Handling
# ============================================================================
resource "aws_lambda_function" "bounce_handler" {
filename = data.archive_file.bounce_lambda_zip.output_path
function_name = "ses-bounce-handler"
role = aws_iam_role.bounce_lambda_execution.arn
handler = "handler.lambda_handler"
source_code_hash = data.archive_file.bounce_lambda_zip.output_base64sha256
runtime = "python3.12"
timeout = 30
memory_size = 256
architectures = ["arm64"]
# Sentry layer for error monitoring
layers = ["arn:aws:lambda:us-east-1:943013980633:layer:SentryPythonServerlessSDK:188"]
environment {
variables = {
AIRTABLE_SECRET_NAME = var.airtable_secret_name
ENVIRONMENT = var.environment
}
}
depends_on = [
aws_cloudwatch_log_group.bounce_lambda,
aws_iam_role_policy_attachment.bounce_lambda_basic_execution,
aws_iam_role_policy.bounce_lambda_execution
]
tags = {
Name = "SES Bounce Handler"
Environment = var.environment
ManagedBy = "Terraform"
}
}
# CloudWatch Log Group
resource "aws_cloudwatch_log_group" "bounce_lambda" {
name = "/aws/lambda/ses-bounce-handler"
retention_in_days = 14
tags = {
Name = "SES Bounce Handler Lambda Logs"
Environment = var.environment
ManagedBy = "Terraform"
}
}
# ============================================================================
# IAM Role and Policies for Bounce Handler Lambda
# ============================================================================
resource "aws_iam_role" "bounce_lambda_execution" {
name = "ses-bounce-handler-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
tags = {
Name = "SES Bounce Handler Lambda Role"
Environment = var.environment
ManagedBy = "Terraform"
}
}
resource "aws_iam_role_policy" "bounce_lambda_execution" {
name = "ses-bounce-handler-lambda-policy"
role = aws_iam_role.bounce_lambda_execution.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "SecretsManagerGetSecret"
Effect = "Allow"
Action = [
"secretsmanager:GetSecretValue"
]
Resource = local.secret_arn
},
{
Sid = "CloudWatchLogs"
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:*:*:*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "bounce_lambda_basic_execution" {
role = aws_iam_role.bounce_lambda_execution.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# ============================================================================
# SNS Subscriptions - Connect SNS Topics to Lambda
# ============================================================================
resource "aws_sns_topic_subscription" "bounces_to_lambda" {
topic_arn = aws_sns_topic.ses_bounces.arn
protocol = "lambda"
endpoint = aws_lambda_function.bounce_handler.arn
}
resource "aws_sns_topic_subscription" "complaints_to_lambda" {
topic_arn = aws_sns_topic.ses_complaints.arn
protocol = "lambda"
endpoint = aws_lambda_function.bounce_handler.arn
}
# ============================================================================
# Lambda Permissions - Allow SNS to Invoke Lambda
# ============================================================================
resource "aws_lambda_permission" "sns_bounces_invoke" {
statement_id = "AllowSNSBouncesInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.bounce_handler.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.ses_bounces.arn
}
resource "aws_lambda_permission" "sns_complaints_invoke" {
statement_id = "AllowSNSComplaintsInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.bounce_handler.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.ses_complaints.arn
}
# ============================================================================
# SES Configuration Set - Routes Bounce/Complaint Events to SNS
# ============================================================================
resource "aws_ses_configuration_set" "main" {
name = "coders-email-forwarding-config"
reputation_metrics_enabled = true
}
resource "aws_ses_event_destination" "bounces" {
name = "bounce-notifications"
configuration_set_name = aws_ses_configuration_set.main.name
enabled = true
matching_types = ["bounce"]
sns_destination {
topic_arn = aws_sns_topic.ses_bounces.arn
}
}
resource "aws_ses_event_destination" "complaints" {
name = "complaint-notifications"
configuration_set_name = aws_ses_configuration_set.main.name
enabled = true
matching_types = ["complaint"]
sns_destination {
topic_arn = aws_sns_topic.ses_complaints.arn
}
}
# ============================================================================
# Data Source - Package Bounce Handler Lambda Code
# ============================================================================
data "archive_file" "bounce_lambda_zip" {
type = "zip"
source_dir = "${path.module}/../../lambda/ses_bounce_handler"
output_path = "${path.module}/bounce_handler.zip"
excludes = [
"tests",
"tests/*",
"README.md",
"__pycache__",
"__pycache__/*",
"*.pyc",
".pytest_cache",
".pytest_cache/*",
".coverage",
".coverage.*",
"htmlcov",
"htmlcov/*",
".venv",
".venv/*",
"venv",
"venv/*"
]
}