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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641data "aws_caller_identity" "current" {}
locals {
frontend_domain = var.domain_name != null ? "app.${var.domain_name}" : null
api_domain = var.domain_name != null ? "api.${var.domain_name}" : null
graphql_domain = var.domain_name != null ? "graphql.${var.domain_name}" : null
use_custom_domains = var.domain_name != null && var.route53_zone_id != null
artifacts_dir = "${path.module}/artifacts"
# Predicted (not referenced-from-resource) name/ARN for the backend Lambda โ
# see modules/cognito/variables.tf's backend_lambda_predicted_arn for why.
backend_lambda_name = "${var.project_name}-${var.environment}-api"
backend_lambda_predicted_arn = "arn:aws:lambda:${var.aws_region}:${data.aws_caller_identity.current.account_id}:function:${local.backend_lambda_name}"
}
# =============================================================================
# Networking
# =============================================================================
module "networking" {
source = "./modules/networking"
project_name = var.project_name
environment = var.environment
vpc_cidr = var.vpc_cidr
single_nat_gateway = var.single_nat_gateway
}
# =============================================================================
# Secrets
# =============================================================================
module "secrets" {
source = "./modules/secrets"
project_name = var.project_name
environment = var.environment
sso_enabled = var.sso_enabled
}
data "aws_secretsmanager_secret_version" "sso_config" {
count = var.sso_enabled ? 1 : 0
secret_id = module.secrets.sso_config_secret_arn
depends_on = [module.secrets]
}
locals {
sso_config = var.sso_enabled ? jsondecode(data.aws_secretsmanager_secret_version.sso_config[0].secret_string) : null
}
# =============================================================================
# RDS
# =============================================================================
module "rds" {
source = "./modules/rds"
project_name = var.project_name
environment = var.environment
vpc_id = module.networking.vpc_id
private_subnet_ids = module.networking.private_subnet_ids
security_group_id = module.networking.rds_security_group_id
db_name = var.db_name
db_username = var.db_username
db_password = module.secrets.db_password
instance_class = var.db_instance_class
allocated_storage = var.db_allocated_storage
multi_az = var.db_multi_az
deletion_protection = var.db_deletion_protection
skip_final_snapshot = var.db_skip_final_snapshot
}
# SSM-only bastion: no SSH key, no public IP, no inbound rules โ a relay for
# scripts/deploy-app.sh's one-off DB access (schema load, migrations) to the
# otherwise fully private RDS instance. See infrastructure/README.md.
module "bastion" {
source = "./modules/bastion"
project_name = var.project_name
environment = var.environment
subnet_id = module.networking.private_subnet_ids[0]
security_group_id = module.networking.bastion_security_group_id
instance_type = var.bastion_instance_type
}
# =============================================================================
# Cognito
# =============================================================================
module "cognito" {
source = "./modules/cognito"
project_name = var.project_name
environment = var.environment
cognito_domain_prefix = var.cognito_domain_prefix
callback_urls = [local.use_custom_domains ? "https://${local.frontend_domain}" : "https://${module.frontend_hosting.distribution_domain_name}"]
logout_urls = [local.use_custom_domains ? "https://${local.frontend_domain}" : "https://${module.frontend_hosting.distribution_domain_name}"]
backend_lambda_function_name = module.lambda_backend.function_name
backend_lambda_predicted_arn = local.backend_lambda_predicted_arn
}
# =============================================================================
# SQS
# =============================================================================
module "sqs" {
source = "./modules/sqs"
project_name = var.project_name
environment = var.environment
}
# =============================================================================
# Custom domain certs + DNS (only when domain_name + route53_zone_id are set)
# =============================================================================
resource "aws_acm_certificate" "frontend" {
count = local.use_custom_domains ? 1 : 0
provider = aws.us_east_1
domain_name = local.frontend_domain
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "frontend_cert_validation" {
for_each = local.use_custom_domains ? {
for dvo in aws_acm_certificate.frontend[0].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
type = dvo.resource_record_type
record = dvo.resource_record_value
}
} : {}
zone_id = var.route53_zone_id
name = each.value.name
type = each.value.type
records = [each.value.record]
ttl = 60
allow_overwrite = true
}
resource "aws_acm_certificate_validation" "frontend" {
count = local.use_custom_domains ? 1 : 0
provider = aws.us_east_1
certificate_arn = aws_acm_certificate.frontend[0].arn
validation_record_fqdns = [for r in aws_route53_record.frontend_cert_validation : r.fqdn]
}
resource "aws_acm_certificate" "api" {
count = local.use_custom_domains ? 1 : 0
domain_name = local.api_domain
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "api_cert_validation" {
for_each = local.use_custom_domains ? {
for dvo in aws_acm_certificate.api[0].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
type = dvo.resource_record_type
record = dvo.resource_record_value
}
} : {}
zone_id = var.route53_zone_id
name = each.value.name
type = each.value.type
records = [each.value.record]
ttl = 60
allow_overwrite = true
}
resource "aws_acm_certificate_validation" "api" {
count = local.use_custom_domains ? 1 : 0
certificate_arn = aws_acm_certificate.api[0].arn
validation_record_fqdns = [for r in aws_route53_record.api_cert_validation : r.fqdn]
}
resource "aws_acm_certificate" "graphql" {
count = local.use_custom_domains ? 1 : 0
domain_name = local.graphql_domain
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "graphql_cert_validation" {
for_each = local.use_custom_domains ? {
for dvo in aws_acm_certificate.graphql[0].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
type = dvo.resource_record_type
record = dvo.resource_record_value
}
} : {}
zone_id = var.route53_zone_id
name = each.value.name
type = each.value.type
records = [each.value.record]
ttl = 60
allow_overwrite = true
}
resource "aws_acm_certificate_validation" "graphql" {
count = local.use_custom_domains ? 1 : 0
certificate_arn = aws_acm_certificate.graphql[0].arn
validation_record_fqdns = [for r in aws_route53_record.graphql_cert_validation : r.fqdn]
}
# =============================================================================
# Deploy artifacts
# =============================================================================
# Transit bucket for scripts/deploy-app.sh: some deploy artifacts (e.g. the
# @sparticuz/chromium Lambda layer) exceed the ~70MB direct-upload limit for
# PublishLayerVersion/UpdateFunctionCode and must be staged in S3 instead of
# uploaded inline. Lambda copies the content internally once published, so
# nothing here needs to persist โ force_destroy + a short expiration are
# both safe.
resource "aws_s3_bucket" "deploy_artifacts" {
bucket = "${var.project_name}-${var.environment}-deploy-artifacts"
force_destroy = true
}
resource "aws_s3_bucket_public_access_block" "deploy_artifacts" {
bucket = aws_s3_bucket.deploy_artifacts.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_server_side_encryption_configuration" "deploy_artifacts" {
bucket = aws_s3_bucket.deploy_artifacts.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "deploy_artifacts" {
bucket = aws_s3_bucket.deploy_artifacts.id
rule {
id = "expire-after-1-day"
status = "Enabled"
filter {}
expiration {
days = 1
}
}
}
# =============================================================================
# Lambda functions
# =============================================================================
module "lambda_scan_sqs_router" {
source = "./modules/lambda_function"
function_name = "${var.project_name}-${var.environment}-scan-sqs-router"
description = "Routes scan requests from the backend into the scanHtml/scanPdf FIFO queues."
runtime = "nodejs22.x"
handler = "lambda.handler"
artifact_path = "${local.artifacts_dir}/node-stub.zip"
memory_size = 256
timeout = 30
environment_variables = {
SQS_HTML_QUEUE_URL = module.sqs.scan_html_queue_url
SQS_PDF_QUEUE_URL = module.sqs.scan_pdf_queue_url
POWERTOOLS_METRICS_NAMESPACE = "${var.project_name}${var.environment}"
}
additional_policy_statements = [
{
sid = "SendToScanQueues"
actions = ["sqs:SendMessage"]
resources = [module.sqs.scan_html_queue_arn, module.sqs.scan_pdf_queue_arn]
}
]
}
module "lambda_scan_html" {
source = "./modules/lambda_function"
function_name = "${var.project_name}-${var.environment}-scan-html"
description = "Scans an HTML page with headless Chromium + axe-core."
runtime = "nodejs22.x"
handler = "lambda.handler"
artifact_path = "${local.artifacts_dir}/node-stub.zip"
memory_size = var.scan_lambda_memory_size
timeout = var.scan_lambda_timeout
environment_variables = {
SCAN_WEBHOOK_URL = local.use_custom_domains ? "https://${local.api_domain}/public/scanWebhook" : "${module.api_gateway.api_endpoint}/public/scanWebhook"
POWERTOOLS_METRICS_NAMESPACE = "${var.project_name}${var.environment}"
}
sqs_event_source = {
queue_arn = module.sqs.scan_html_queue_arn
batch_size = 5
}
}
module "lambda_verapdf_interface" {
source = "./modules/lambda_function"
function_name = "${var.project_name}-${var.environment}-verapdf-interface"
description = "Validates PDFs against veraPDF (PDF/UA) rules. Invoked synchronously by scan-pdf."
runtime = "java17"
handler = "com.equalifyuic.app.handler"
artifact_path = "${local.artifacts_dir}/java-stub.jar"
memory_size = 1024
timeout = 60
}
module "lambda_scan_pdf" {
source = "./modules/lambda_function"
function_name = "${var.project_name}-${var.environment}-scan-pdf"
description = "Scans a PDF: HTML accessibility checks + veraPDF validation via verapdf-interface."
runtime = "nodejs22.x"
handler = "lambda.handler"
artifact_path = "${local.artifacts_dir}/node-stub.zip"
memory_size = var.scan_lambda_memory_size
timeout = var.scan_lambda_timeout
environment_variables = {
SCAN_WEBHOOK_URL = local.use_custom_domains ? "https://${local.api_domain}/public/scanWebhook" : "${module.api_gateway.api_endpoint}/public/scanWebhook"
VERAPDF_FUNCTION_NAME = module.lambda_verapdf_interface.function_name
POWERTOOLS_METRICS_NAMESPACE = "${var.project_name}${var.environment}"
}
sqs_event_source = {
queue_arn = module.sqs.scan_pdf_queue_arn
batch_size = 5
}
additional_policy_statements = [
{
sid = "InvokeVerapdfInterface"
actions = ["lambda:InvokeFunction"]
resources = [module.lambda_verapdf_interface.function_arn]
}
]
}
module "lambda_crawler" {
source = "./modules/lambda_function"
function_name = "${var.project_name}-${var.environment}-crawler"
description = "Sitemap discovery for a given URL, called directly from the frontend via a Function URL."
runtime = "nodejs22.x"
handler = "lambda.handler"
artifact_path = "${local.artifacts_dir}/node-stub.zip"
memory_size = 256
timeout = 30
create_function_url = true
function_url_cors = {
allow_origins = local.use_custom_domains ? ["https://${local.frontend_domain}"] : ["*"]
allow_methods = ["POST"]
allow_headers = ["content-type"]
}
}
module "lambda_backend" {
source = "./modules/lambda_function"
function_name = local.backend_lambda_name
description = "Equalify backend API (apps/backend) โ auth, scans orchestration, GraphQL passthrough."
runtime = "nodejs22.x"
handler = "index.handler"
artifact_path = "${local.artifacts_dir}/node-stub.zip"
memory_size = var.backend_lambda_memory_size
timeout = var.backend_lambda_timeout
vpc_config = {
subnet_ids = module.networking.private_subnet_ids
security_group_ids = [module.networking.backend_lambda_security_group_id]
}
environment_variables = merge(
{
DB_USER = var.db_username
DB_HOST = module.rds.address
DB_NAME = module.rds.db_name
DB_PASSWORD = module.secrets.db_password
# apps/backend/utils/graphqlQuery.ts appends /v1/graphql itself (its
# own fallback default is the bare "https://graphql...equalifyapp.com",
# no path) โ same bare-domain convention as the frontend's
# VITE_GRAPHQL_URL, so trim it back off graphql_url the same way.
GRAPHQL_URL = trimsuffix(local.use_custom_domains ? "https://${local.graphql_domain}/v1/graphql" : module.hasura_ecs.graphql_url, "/v1/graphql")
USER_POOL_ID = module.cognito.user_pool_id
WEB_CLIENT_ID = module.cognito.web_client_id
SSO_ENABLED = var.sso_enabled ? "1" : "0"
SES_ADMIN_EMAIL = var.ses_admin_email
SQS_ROUTER_FUNCTION_NAME = module.lambda_scan_sqs_router.function_name
CRAWLER_FUNCTION_NAME = module.lambda_crawler.function_name
POWERTOOLS_METRICS_NAMESPACE = "${var.project_name}${var.environment}"
APP_URL = var.app_url != null ? var.app_url : (local.use_custom_domains ? "https://${local.frontend_domain}" : "https://${module.frontend_hosting.distribution_domain_name}")
BRAND_URL = var.brand_url
BRAND_LOGO_URL = var.brand_logo_url
WEBHOOKSECRET = module.secrets.webhook_secret
STAGING = var.environment == "staging" ? "1" : "0"
},
var.sso_enabled ? {
SSO_JWKS = local.sso_config.SSO_JWKS
SSO_CLIENT_ID = local.sso_config.SSO_CLIENT_ID
SSO_TENANT = local.sso_config.SSO_TENANT
SSO_EMAIL_DOMAINS = local.sso_config.SSO_EMAIL_DOMAINS
} : {}
)
additional_policy_statements = [
{
sid = "BedrockInvoke"
actions = ["bedrock:InvokeModel", "bedrock:ListFoundationModels"]
resources = ["*"]
},
{
sid = "SesSendEmail"
actions = ["ses:SendEmail", "ses:SendRawEmail"]
resources = ["*"]
},
{
sid = "CognitoAdmin"
actions = [
"cognito-idp:AdminCreateUser",
"cognito-idp:AdminSetUserPassword",
"cognito-idp:AdminGetUser",
"cognito-idp:AdminUpdateUserAttributes",
"cognito-idp:AdminDeleteUser",
"cognito-idp:AdminInitiateAuth",
"cognito-idp:ListUsers",
]
resources = [module.cognito.user_pool_arn]
},
{
sid = "InvokeScanSqsRouter"
actions = ["lambda:InvokeFunction"]
resources = [module.lambda_scan_sqs_router.function_arn]
},
{
# crawlUrl.ts invokes this synchronously to discover URLs from a
# site's sitemap for the /audits/build flow.
sid = "InvokeCrawler"
actions = ["lambda:InvokeFunction"]
resources = [module.lambda_crawler.function_arn]
},
{
sid = "PutMetrics"
actions = ["cloudwatch:PutMetricData"]
resources = ["*"]
},
{
# getSystemStats.ts reads back scan-duration/count metrics for the
# admin System tab โ PutMetrics above only covers the Lambdas
# publishing their own metrics, not this Lambda reading them back.
sid = "ReadMetrics"
actions = ["cloudwatch:GetMetricStatistics"]
resources = ["*"]
},
]
}
# =============================================================================
# API Gateway (HTTP API) โ fronts the backend Lambda
# =============================================================================
module "api_gateway" {
source = "./modules/api_gateway"
project_name = var.project_name
environment = var.environment
backend_lambda_function_name = module.lambda_backend.function_name
backend_lambda_invoke_arn = module.lambda_backend.invoke_arn
domain_name = local.use_custom_domains ? local.api_domain : null
acm_certificate_arn = local.use_custom_domains ? aws_acm_certificate_validation.api[0].certificate_arn : null
}
resource "aws_route53_record" "api" {
count = local.use_custom_domains ? 1 : 0
zone_id = var.route53_zone_id
name = local.api_domain
type = "A"
alias {
name = module.api_gateway.custom_domain_target
zone_id = module.api_gateway.custom_domain_hosted_zone_id
evaluate_target_health = false
}
}
# =============================================================================
# Hasura (ECS Fargate)
# =============================================================================
module "hasura_ecs" {
source = "./modules/hasura_ecs"
project_name = var.project_name
environment = var.environment
vpc_id = module.networking.vpc_id
public_subnet_ids = module.networking.public_subnet_ids
private_subnet_ids = module.networking.private_subnet_ids
alb_security_group_id = module.networking.alb_security_group_id
task_security_group_id = module.networking.hasura_task_security_group_id
hasura_image = var.hasura_image
database_url = "postgresql://${var.db_username}:${module.secrets.db_password}@${module.rds.address}:${module.rds.port}/${module.rds.db_name}"
admin_secret = module.secrets.hasura_admin_secret
aws_region = var.aws_region
user_pool_id = module.cognito.user_pool_id
web_client_id = module.cognito.web_client_id
cpu = var.hasura_cpu
memory = var.hasura_memory
desired_count = var.hasura_desired_count
domain_name = local.use_custom_domains ? local.graphql_domain : null
acm_certificate_arn = local.use_custom_domains ? aws_acm_certificate_validation.graphql[0].certificate_arn : null
cors_allowed_origins = local.use_custom_domains ? "https://${local.frontend_domain}" : "*"
}
resource "aws_route53_record" "graphql" {
count = local.use_custom_domains ? 1 : 0
zone_id = var.route53_zone_id
name = local.graphql_domain
type = "A"
alias {
name = module.hasura_ecs.alb_dns_name
zone_id = module.hasura_ecs.alb_zone_id
evaluate_target_health = true
}
}
# =============================================================================
# Frontend hosting (S3 + CloudFront)
# =============================================================================
module "frontend_hosting" {
source = "./modules/frontend_hosting"
project_name = var.project_name
environment = var.environment
domain_name = local.use_custom_domains ? local.frontend_domain : null
acm_certificate_arn = local.use_custom_domains ? aws_acm_certificate_validation.frontend[0].certificate_arn : null
}
resource "aws_route53_record" "frontend" {
count = local.use_custom_domains ? 1 : 0
zone_id = var.route53_zone_id
name = local.frontend_domain
type = "A"
alias {
name = module.frontend_hosting.distribution_domain_name
zone_id = "Z2FDTNDATAQYW2" # CloudFront's fixed global hosted zone ID
evaluate_target_health = false
}
}
# =============================================================================
# Monitoring
# =============================================================================
module "monitoring" {
source = "./modules/monitoring"
project_name = var.project_name
environment = var.environment
alarm_email = var.alarm_email
lambda_function_names = [
module.lambda_scan_sqs_router.function_name,
module.lambda_scan_html.function_name,
module.lambda_scan_pdf.function_name,
module.lambda_verapdf_interface.function_name,
module.lambda_crawler.function_name,
module.lambda_backend.function_name,
]
rds_instance_id = module.rds.db_instance_id
alb_arn_suffix = module.hasura_ecs.alb_arn_suffix
target_group_arn_suffix = module.hasura_ecs.target_group_arn_suffix
ecs_cluster_name = module.hasura_ecs.cluster_name
ecs_service_name = module.hasura_ecs.service_name
}