This repository was archived by the owner on Mar 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaging.tf
More file actions
182 lines (163 loc) · 5.53 KB
/
staging.tf
File metadata and controls
182 lines (163 loc) · 5.53 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
# Lambda function for syncing Redshift tables to Postgres checkout optimization STAGING
# Syncs: aggregated_performance_metrics_daily, lever_traffic_daily
locals {
staging_function_name = "redshift-to-postgres-sync-staging"
# Staging Postgres security group
postgres_staging_rds_sg = "sg-09c1ed14fd824be27" # hive-checkout-optimization-rds-vercel-staging
}
# IAM Role for Staging Lambda
resource "aws_iam_role" "staging_lambda_role" {
name = "${local.staging_function_name}-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
tags = {
Name = "${local.staging_function_name}-role"
Environment = "staging"
Project = "checkout-optimization"
}
}
# IAM Policy for Staging Lambda
resource "aws_iam_role_policy" "staging_lambda_policy" {
name = "${local.staging_function_name}-policy"
role = aws_iam_role.staging_lambda_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
# CloudWatch Logs
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:eu-central-1:*:*"
},
{
# VPC ENI management (required for Lambda in VPC)
Effect = "Allow"
Action = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:AssignPrivateIpAddresses",
"ec2:UnassignPrivateIpAddresses"
]
Resource = "*"
},
{
# Secrets Manager (for Postgres and Redshift credentials)
Effect = "Allow"
Action = [
"secretsmanager:GetSecretValue"
]
Resource = [
"arn:aws:secretsmanager:eu-central-1:849181912040:secret:applications/staging/checkout-optimization-*",
"arn:aws:secretsmanager:eu-central-1:849181912040:secret:redshift/*"
]
},
{
# Redshift IAM authentication
Effect = "Allow"
Action = [
"redshift:GetClusterCredentials",
"redshift:DescribeClusters"
]
Resource = [
"arn:aws:redshift:eu-central-1:849181912040:cluster:hive",
"arn:aws:redshift:eu-central-1:849181912040:dbuser:hive/dbt",
"arn:aws:redshift:eu-central-1:849181912040:dbname:hive/main"
]
}
]
})
}
# Staging Lambda function
resource "aws_lambda_function" "staging_redshift_postgres_sync" {
function_name = local.staging_function_name
role = aws_iam_role.staging_lambda_role.arn
handler = "redshift_to_postgres_sync.handler"
runtime = "python3.12"
timeout = 300
memory_size = 512
# Code package (same as production)
filename = "${path.module}/lambda_code.zip"
source_code_hash = filebase64sha256("${path.module}/lambda_code.zip")
# Attach the dependencies layer (shared with production)
layers = [aws_lambda_layer_version.dependencies.arn]
vpc_config {
subnet_ids = local.subnet_ids
security_group_ids = [
local.redshift_client_sg,
local.postgres_staging_rds_sg
]
}
environment {
variables = {
REDSHIFT_HOST = "hive.cjc3cej8scym.eu-central-1.redshift.amazonaws.com"
REDSHIFT_PORT = "5439"
REDSHIFT_DATABASE = "main"
REDSHIFT_USER = "dbt"
# Staging Postgres
POSTGRES_HOST = "hive-checkout-optimization-staging.cqufmenwec0c.eu-central-1.rds.amazonaws.com"
POSTGRES_PORT = "5432"
POSTGRES_DATABASE = "hive_checkout_optimization"
POSTGRES_USER = "hive"
# Staging secret name
POSTGRES_SECRET = "applications/staging/checkout-optimization"
# Source schema in Redshift where dbt marts live
SOURCE_SCHEMA = "events"
TARGET_SCHEMA = "public"
}
}
tags = {
Name = local.staging_function_name
Environment = "staging"
Project = "checkout-optimization"
}
}
# CloudWatch Log Group for Staging
resource "aws_cloudwatch_log_group" "staging_lambda_logs" {
name = "/aws/lambda/${local.staging_function_name}"
retention_in_days = 14
tags = {
Name = "${local.staging_function_name}-logs"
Environment = "staging"
Project = "checkout-optimization"
}
}
# EventBridge Rule for Staging - Run daily at 07:00 UTC (after dbt models refresh)
resource "aws_cloudwatch_event_rule" "staging_daily_sync" {
name = "${local.staging_function_name}-daily"
description = "Trigger Redshift to Postgres sync daily (staging)"
schedule_expression = "cron(0 6 * * ? *)"
tags = {
Name = "${local.staging_function_name}-schedule"
Environment = "staging"
Project = "checkout-optimization"
}
}
# EventBridge Target for Staging
resource "aws_cloudwatch_event_target" "staging_lambda_target" {
rule = aws_cloudwatch_event_rule.staging_daily_sync.name
target_id = "RedshiftPostgresSyncStaging"
arn = aws_lambda_function.staging_redshift_postgres_sync.arn
}
# Lambda permission for EventBridge (Staging)
resource "aws_lambda_permission" "staging_allow_eventbridge" {
statement_id = "AllowExecutionFromEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.staging_redshift_postgres_sync.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.staging_daily_sync.arn
}