-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathfeedback.tf
More file actions
393 lines (324 loc) · 12.9 KB
/
feedback.tf
File metadata and controls
393 lines (324 loc) · 12.9 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# =============================================================================
# Feedback API
# Maps to: backend-stack.ts createFeedbackApi() + createFeedbackTable()
# =============================================================================
# -----------------------------------------------------------------------------
# DynamoDB Table for Feedback Storage
# -----------------------------------------------------------------------------
resource "aws_dynamodb_table" "feedback" {
name = "${var.stack_name_base}-feedback"
billing_mode = "PAY_PER_REQUEST"
hash_key = "feedbackId"
attribute {
name = "feedbackId"
type = "S"
}
attribute {
name = "feedbackType"
type = "S"
}
attribute {
name = "timestamp"
type = "N"
}
# GSI for querying by feedbackType with timestamp sorting
global_secondary_index {
name = "feedbackType-timestamp-index"
hash_key = "feedbackType"
range_key = "timestamp"
projection_type = "ALL"
}
# Deletion protection disabled (allows terraform destroy)
deletion_protection_enabled = false
# Point-in-time recovery
point_in_time_recovery {
enabled = true
}
# Server-side encryption (AWS managed)
server_side_encryption {
enabled = true
}
}
# -----------------------------------------------------------------------------
# CloudWatch Log Group for Lambda
# -----------------------------------------------------------------------------
resource "aws_cloudwatch_log_group" "feedback_lambda" {
name = "/aws/lambda/${var.stack_name_base}-feedback"
retention_in_days = local.log_retention_days
}
# -----------------------------------------------------------------------------
# IAM Role for Lambda Function
# -----------------------------------------------------------------------------
data "aws_iam_policy_document" "feedback_lambda_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "feedback_lambda" {
name = "${var.stack_name_base}-feedback-lambda-role"
assume_role_policy = data.aws_iam_policy_document.feedback_lambda_assume_role.json
description = "Execution role for feedback Lambda function"
}
data "aws_iam_policy_document" "feedback_lambda_policy" {
# CloudWatch Logs
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["${aws_cloudwatch_log_group.feedback_lambda.arn}:*"]
}
# DynamoDB access
statement {
effect = "Allow"
actions = [
"dynamodb:PutItem",
"dynamodb:GetItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:Query",
"dynamodb:Scan"
]
resources = [
aws_dynamodb_table.feedback.arn,
"${aws_dynamodb_table.feedback.arn}/index/*"
]
}
}
resource "aws_iam_role_policy" "feedback_lambda" {
name = "${var.stack_name_base}-feedback-lambda-policy"
role = aws_iam_role.feedback_lambda.id
policy = data.aws_iam_policy_document.feedback_lambda_policy.json
}
# -----------------------------------------------------------------------------
# Lambda Function for Feedback API
# -----------------------------------------------------------------------------
# Build lambda package with dependencies (pip install from requirements.txt)
resource "null_resource" "feedback_lambda_build" {
triggers = {
source_hash = sha256(join("", [for f in fileset(local.feedback_lambda_source_path, "*.py") : filesha256("${local.feedback_lambda_source_path}/${f}")]))
requirements_hash = fileexists("${local.feedback_lambda_source_path}/requirements.txt") ? filesha256("${local.feedback_lambda_source_path}/requirements.txt") : ""
}
provisioner "local-exec" {
command = <<-EOT
set -e
BUILD_DIR="${path.module}/artifacts/feedback_lambda_build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
cp ${local.feedback_lambda_source_path}/*.py "$BUILD_DIR/"
if [ -f "${local.feedback_lambda_source_path}/requirements.txt" ]; then
python3 -m pip install -r "${local.feedback_lambda_source_path}/requirements.txt" -t "$BUILD_DIR/" --quiet --upgrade
fi
EOT
}
}
data "archive_file" "feedback_lambda" {
type = "zip"
source_dir = "${path.module}/artifacts/feedback_lambda_build"
output_path = "${path.module}/artifacts/feedback_lambda.zip"
excludes = ["__pycache__", "*.pyc", "*.dist-info"]
depends_on = [null_resource.feedback_lambda_build]
}
resource "aws_lambda_function" "feedback" {
function_name = "${var.stack_name_base}-feedback"
role = aws_iam_role.feedback_lambda.arn
handler = "index.handler"
runtime = "python3.13"
timeout = 30
memory_size = 256
filename = data.archive_file.feedback_lambda.output_path
source_code_hash = data.archive_file.feedback_lambda.output_base64sha256
# Lambda Powertools layer
layers = [local.powertools_layer_arn]
# Environment variables
environment {
variables = {
TABLE_NAME = aws_dynamodb_table.feedback.name
CORS_ALLOWED_ORIGINS = "${var.frontend_url},http://localhost:3000"
}
}
depends_on = [aws_cloudwatch_log_group.feedback_lambda]
}
# -----------------------------------------------------------------------------
# API Gateway REST API
# -----------------------------------------------------------------------------
resource "aws_api_gateway_rest_api" "feedback" {
name = "${var.stack_name_base}-feedback-api"
description = "API Gateway for feedback collection"
endpoint_configuration {
types = ["REGIONAL"]
}
}
# -----------------------------------------------------------------------------
# Cognito Authorizer
# -----------------------------------------------------------------------------
resource "aws_api_gateway_authorizer" "cognito" {
name = "${var.stack_name_base}-cognito-authorizer"
rest_api_id = aws_api_gateway_rest_api.feedback.id
type = "COGNITO_USER_POOLS"
provider_arns = [var.user_pool_arn]
identity_source = "method.request.header.Authorization"
authorizer_result_ttl_in_seconds = local.api_cache_ttl_seconds
}
# -----------------------------------------------------------------------------
# API Gateway Resources
# -----------------------------------------------------------------------------
# /feedback resource
resource "aws_api_gateway_resource" "feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
parent_id = aws_api_gateway_rest_api.feedback.root_resource_id
path_part = "feedback"
}
# -----------------------------------------------------------------------------
# API Gateway Methods
# -----------------------------------------------------------------------------
# POST /feedback
resource "aws_api_gateway_method" "post_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = "POST"
authorization = "COGNITO_USER_POOLS"
authorizer_id = aws_api_gateway_authorizer.cognito.id
}
# OPTIONS /feedback (CORS preflight)
resource "aws_api_gateway_method" "options_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = "OPTIONS"
authorization = "NONE"
}
# -----------------------------------------------------------------------------
# API Gateway Integrations
# -----------------------------------------------------------------------------
# POST /feedback -> Lambda
resource "aws_api_gateway_integration" "post_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = aws_api_gateway_method.post_feedback.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.feedback.invoke_arn
}
# OPTIONS /feedback -> Mock (CORS)
resource "aws_api_gateway_integration" "options_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = aws_api_gateway_method.options_feedback.http_method
type = "MOCK"
request_templates = {
"application/json" = jsonencode({
statusCode = 200
})
}
}
# OPTIONS method response
resource "aws_api_gateway_method_response" "options_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = aws_api_gateway_method.options_feedback.http_method
status_code = "200"
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = true
"method.response.header.Access-Control-Allow-Methods" = true
"method.response.header.Access-Control-Allow-Origin" = true
}
}
# OPTIONS integration response
resource "aws_api_gateway_integration_response" "options_feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
resource_id = aws_api_gateway_resource.feedback.id
http_method = aws_api_gateway_method.options_feedback.http_method
status_code = aws_api_gateway_method_response.options_feedback.status_code
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
"method.response.header.Access-Control-Allow-Methods" = "'GET,POST,OPTIONS'"
"method.response.header.Access-Control-Allow-Origin" = "'${var.frontend_url}'"
}
depends_on = [aws_api_gateway_integration.options_feedback]
}
# -----------------------------------------------------------------------------
# Lambda Permission for API Gateway
# -----------------------------------------------------------------------------
resource "aws_lambda_permission" "feedback_api_gateway" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.feedback.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.feedback.execution_arn}/*/*"
}
# -----------------------------------------------------------------------------
# API Gateway Deployment
# -----------------------------------------------------------------------------
resource "aws_api_gateway_deployment" "feedback" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.feedback.id,
aws_api_gateway_method.post_feedback.id,
aws_api_gateway_method.options_feedback.id,
aws_api_gateway_integration.post_feedback.id,
aws_api_gateway_integration.options_feedback.id,
]))
}
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_api_gateway_integration.post_feedback,
aws_api_gateway_integration.options_feedback
]
}
resource "aws_api_gateway_stage" "prod" {
stage_name = "prod"
rest_api_id = aws_api_gateway_rest_api.feedback.id
deployment_id = aws_api_gateway_deployment.feedback.id
# Access logs
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api_gateway_access.arn
format = jsonencode({
requestId = "$context.requestId"
ip = "$context.identity.sourceIp"
caller = "$context.identity.caller"
user = "$context.identity.user"
requestTime = "$context.requestTime"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
status = "$context.status"
protocol = "$context.protocol"
responseLength = "$context.responseLength"
integrationError = "$context.integrationErrorMessage"
})
}
depends_on = [aws_cloudwatch_log_group.api_gateway_access]
}
# CloudWatch Log Group for API Gateway access logs
resource "aws_cloudwatch_log_group" "api_gateway_access" {
name = "/aws/apigateway/${var.stack_name_base}-feedback-api/access-logs"
retention_in_days = local.log_retention_days
}
# -----------------------------------------------------------------------------
# API Gateway Method Settings (throttling + caching)
# -----------------------------------------------------------------------------
resource "aws_api_gateway_method_settings" "all" {
rest_api_id = aws_api_gateway_rest_api.feedback.id
stage_name = aws_api_gateway_stage.prod.stage_name
method_path = "*/*"
settings {
throttling_rate_limit = local.api_throttling_rate_limit
throttling_burst_limit = local.api_throttling_burst_limit
caching_enabled = true
cache_data_encrypted = true
cache_ttl_in_seconds = local.api_cache_ttl_seconds
logging_level = "INFO"
metrics_enabled = true
data_trace_enabled = false
}
}