grada-run 0.49.11 → 0.50.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/alerts.js +283 -59
- package/src/commands/db/migrate.js +1 -1
- package/src/commands/diagnose.js +2 -2
- package/src/commands/gc.js +8 -2
- package/src/commands/rollback.js +2 -2
- package/src/commands/status.js +85 -7
- package/src/data/action-pins.js +1 -1
- package/src/utils/generator.js +19 -0
- package/src/utils/golden-signals.js +4 -4
- package/src/utils/prompts.js +6 -0
- package/src/utils/rds.js +7 -5
- package/src/utils/resolvers.js +18 -3
- package/templates/github/deploy-lambda.yml +1 -1
- package/templates/github/deploy-static.yml +1 -1
- package/templates/github/deploy.yml +1 -1
- package/templates/github/drift.yml +68 -0
- package/templates/github/preview.yml +131 -0
- package/templates/terraform/addons/bedrock.tf +3 -3
- package/templates/terraform/addons/cron-lambda.tf +23 -3
- package/templates/terraform/addons/cron.tf +23 -3
- package/templates/terraform/addons/s3.tf +6 -0
- package/templates/terraform/addons/ses.tf +44 -1
- package/templates/terraform/addons/sqs.tf +12 -3
- package/templates/terraform/database-aurora-postgresql.tf +46 -4
- package/templates/terraform/database-mysql.tf +32 -3
- package/templates/terraform/database.tf +32 -3
- package/templates/terraform/main-lambda.tf +13 -5
- package/templates/terraform/main.tf +16 -5
- package/templates/terraform/oidc.tf +132 -29
|
@@ -11,9 +11,20 @@
|
|
|
11
11
|
# in application code (the payload below carries the configured command), or
|
|
12
12
|
# point the schedule at a dedicated function.
|
|
13
13
|
|
|
14
|
+
locals {
|
|
15
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
16
|
+
# names hash-suffix instead of failing validation. Mirrors
|
|
17
|
+
# safeSuffixedName in resolvers.js.
|
|
18
|
+
scheduler_role_safe_name = length("${local.app_name}-cron-scheduler-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 20)}-${substr(md5(local.app_name), 0, 5)}-cron-scheduler-role" : "${local.app_name}-cron-scheduler-role"
|
|
19
|
+
schedule_full_name = "${local.app_name}-cron-{{CRON_NAME}}"
|
|
20
|
+
schedule_safe_name = length(local.schedule_full_name) > 64 ? "${substr(local.schedule_full_name, 0, 64 - 1 - 5)}-${substr(md5(local.schedule_full_name), 0, 5)}" : local.schedule_full_name
|
|
21
|
+
schedule_group_safe_name = length("${local.app_name}-crons") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 6)}-${substr(md5(local.app_name), 0, 5)}-crons" : "${local.app_name}-crons"
|
|
22
|
+
scheduler_policy_safe_name = length("${local.app_name}-cron-scheduler-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 22)}-${substr(md5(local.app_name), 0, 5)}-cron-scheduler-policy" : "${local.app_name}-cron-scheduler-policy"
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
# --- IAM Role assumed by EventBridge Scheduler ---
|
|
15
26
|
resource "aws_iam_role" "scheduler_cron_role" {
|
|
16
|
-
name =
|
|
27
|
+
name = local.scheduler_role_safe_name
|
|
17
28
|
permissions_boundary = local.preview_boundary_arn
|
|
18
29
|
assume_role_policy = jsonencode({
|
|
19
30
|
Version = "2012-10-17"
|
|
@@ -31,7 +42,7 @@ resource "aws_iam_role" "scheduler_cron_role" {
|
|
|
31
42
|
|
|
32
43
|
# --- Least-privilege permission to invoke the function ---
|
|
33
44
|
resource "aws_iam_role_policy" "scheduler_cron_policy" {
|
|
34
|
-
name =
|
|
45
|
+
name = local.scheduler_policy_safe_name
|
|
35
46
|
role = aws_iam_role.scheduler_cron_role.id
|
|
36
47
|
policy = jsonencode({
|
|
37
48
|
Version = "2012-10-17"
|
|
@@ -48,9 +59,18 @@ resource "aws_iam_role_policy" "scheduler_cron_policy" {
|
|
|
48
59
|
})
|
|
49
60
|
}
|
|
50
61
|
|
|
62
|
+
# --- Schedule group (failure metrics scope) ---
|
|
63
|
+
# Scheduler metrics dimension by group only (no per-schedule dimension),
|
|
64
|
+
# so the project's schedules get their own group instead of the shared
|
|
65
|
+
# default one — the alerts.tf failure alarms stay project-scoped.
|
|
66
|
+
resource "aws_scheduler_schedule_group" "crons" {
|
|
67
|
+
name = local.schedule_group_safe_name
|
|
68
|
+
}
|
|
69
|
+
|
|
51
70
|
# --- Scheduled invocation (single schedule per project) ---
|
|
52
71
|
resource "aws_scheduler_schedule" "cron" {
|
|
53
|
-
name =
|
|
72
|
+
name = local.schedule_safe_name
|
|
73
|
+
group_name = aws_scheduler_schedule_group.crons.name
|
|
54
74
|
schedule_expression = "{{SCHEDULE_EXPRESSION}}"
|
|
55
75
|
schedule_expression_timezone = "{{SCHEDULE_TIMEZONE}}"
|
|
56
76
|
# Sleep guard: stays disabled while grada sleep's auto-vars are active
|
|
@@ -5,9 +5,20 @@
|
|
|
5
5
|
# Billing: $0/mo fixed baseline (first 14M EventBridge Scheduler
|
|
6
6
|
# invocations/mo free); billed only for Fargate seconds while the task runs.
|
|
7
7
|
|
|
8
|
+
locals {
|
|
9
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
10
|
+
# names hash-suffix instead of failing validation. Mirrors
|
|
11
|
+
# safeSuffixedName in resolvers.js.
|
|
12
|
+
scheduler_role_safe_name = length("${local.app_name}-cron-scheduler-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 20)}-${substr(md5(local.app_name), 0, 5)}-cron-scheduler-role" : "${local.app_name}-cron-scheduler-role"
|
|
13
|
+
schedule_full_name = "${local.app_name}-cron-{{CRON_NAME}}"
|
|
14
|
+
schedule_safe_name = length(local.schedule_full_name) > 64 ? "${substr(local.schedule_full_name, 0, 64 - 1 - 5)}-${substr(md5(local.schedule_full_name), 0, 5)}" : local.schedule_full_name
|
|
15
|
+
schedule_group_safe_name = length("${local.app_name}-crons") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 6)}-${substr(md5(local.app_name), 0, 5)}-crons" : "${local.app_name}-crons"
|
|
16
|
+
scheduler_policy_safe_name = length("${local.app_name}-cron-scheduler-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 22)}-${substr(md5(local.app_name), 0, 5)}-cron-scheduler-policy" : "${local.app_name}-cron-scheduler-policy"
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
# --- IAM Role assumed by EventBridge Scheduler ---
|
|
9
20
|
resource "aws_iam_role" "scheduler_cron_role" {
|
|
10
|
-
name =
|
|
21
|
+
name = local.scheduler_role_safe_name
|
|
11
22
|
permissions_boundary = local.preview_boundary_arn
|
|
12
23
|
assume_role_policy = jsonencode({
|
|
13
24
|
Version = "2012-10-17"
|
|
@@ -25,7 +36,7 @@ resource "aws_iam_role" "scheduler_cron_role" {
|
|
|
25
36
|
|
|
26
37
|
# --- Least-privilege permission to launch the scheduled task ---
|
|
27
38
|
resource "aws_iam_role_policy" "scheduler_cron_policy" {
|
|
28
|
-
name =
|
|
39
|
+
name = local.scheduler_policy_safe_name
|
|
29
40
|
role = aws_iam_role.scheduler_cron_role.id
|
|
30
41
|
policy = jsonencode({
|
|
31
42
|
Version = "2012-10-17"
|
|
@@ -55,9 +66,18 @@ resource "aws_iam_role_policy" "scheduler_cron_policy" {
|
|
|
55
66
|
})
|
|
56
67
|
}
|
|
57
68
|
|
|
69
|
+
# --- Schedule group (failure metrics scope) ---
|
|
70
|
+
# Scheduler metrics dimension by group only (no per-schedule dimension),
|
|
71
|
+
# so the project's schedules get their own group instead of the shared
|
|
72
|
+
# default one — the alerts.tf failure alarms stay project-scoped.
|
|
73
|
+
resource "aws_scheduler_schedule_group" "crons" {
|
|
74
|
+
name = local.schedule_group_safe_name
|
|
75
|
+
}
|
|
76
|
+
|
|
58
77
|
# --- Scheduled task (single schedule per project) ---
|
|
59
78
|
resource "aws_scheduler_schedule" "cron" {
|
|
60
|
-
name =
|
|
79
|
+
name = local.schedule_safe_name
|
|
80
|
+
group_name = aws_scheduler_schedule_group.crons.name
|
|
61
81
|
schedule_expression = "{{SCHEDULE_EXPRESSION}}"
|
|
62
82
|
schedule_expression_timezone = "{{SCHEDULE_TIMEZONE}}"
|
|
63
83
|
# Sleep guard: stays disabled while grada sleep's auto-vars are active
|
|
@@ -12,6 +12,12 @@ resource "aws_s3_bucket" "storage" {
|
|
|
12
12
|
# within S3's 63-character limit. Underscores are replaced because S3 names
|
|
13
13
|
# must be DNS-compatible (lowercase, hyphens only).
|
|
14
14
|
bucket = "${trimsuffix(substr(replace(lower(local.app_name), "_", "-"), 0, 42), "-")}-storage-${data.aws_caller_identity.current.account_id}"
|
|
15
|
+
# Preview teardowns destroy raw (no CLI drain step runs there), so
|
|
16
|
+
# preview buckets self-clean — versions and lone delete markers
|
|
17
|
+
# included. Prod (default workspace) stays protected. Caveat: manual
|
|
18
|
+
# `terraform workspace new <name>` workspaces match too (accepted —
|
|
19
|
+
# manual-name hazards are tracked separately).
|
|
20
|
+
force_destroy = terraform.workspace != "default"
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
resource "aws_s3_bucket_public_access_block" "storage" {
|
|
@@ -10,6 +10,13 @@
|
|
|
10
10
|
# --- SES Domain Identity ---
|
|
11
11
|
# Account-wide singletons per domain/region: scoped to the default
|
|
12
12
|
# (production) workspace so PR preview teardowns never delete them.
|
|
13
|
+
locals {
|
|
14
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
15
|
+
# names hash-suffix instead of failing validation (config sets
|
|
16
|
+
# cap at 64, policies at 128). Mirrors safeSuffixedName.
|
|
17
|
+
config_set_safe_name = length("${local.app_name}-ses-events") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 11)}-${substr(md5(local.app_name), 0, 5)}-ses-events" : "${local.app_name}-ses-events"
|
|
18
|
+
send_policy_safe_name = length("${local.app_name}-ses-send") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 9)}-${substr(md5(local.app_name), 0, 5)}-ses-send" : "${local.app_name}-ses-send"
|
|
19
|
+
}
|
|
13
20
|
resource "aws_ses_domain_identity" "ses" {
|
|
14
21
|
count = terraform.workspace == "default" ? 1 : 0
|
|
15
22
|
domain = "{{SES_DOMAIN}}"
|
|
@@ -27,10 +34,42 @@ resource "aws_ses_domain_mail_from" "ses" {
|
|
|
27
34
|
behavior_on_mx_failure = "UseDefaultValue"
|
|
28
35
|
}
|
|
29
36
|
|
|
37
|
+
# --- Feedback channel ---
|
|
38
|
+
# Bounces and complaints publish to CloudWatch (dashboard + alarms)
|
|
39
|
+
# instead of vanishing. The v2 sender identity sets this set as its
|
|
40
|
+
# default, so app mail needs no per-send configuration; addresses
|
|
41
|
+
# under the verified domain above verify automatically.
|
|
42
|
+
resource "aws_ses_configuration_set" "main" {
|
|
43
|
+
count = terraform.workspace == "default" ? 1 : 0
|
|
44
|
+
name = local.config_set_safe_name
|
|
45
|
+
reputation_metrics_enabled = true
|
|
46
|
+
sending_enabled = true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
resource "aws_ses_event_destination" "cloudwatch" {
|
|
50
|
+
count = terraform.workspace == "default" ? 1 : 0
|
|
51
|
+
name = "cloudwatch"
|
|
52
|
+
configuration_set_name = aws_ses_configuration_set.main[0].name
|
|
53
|
+
enabled = true
|
|
54
|
+
matching_types = ["bounce", "complaint", "send"]
|
|
55
|
+
|
|
56
|
+
cloudwatch_destination {
|
|
57
|
+
default_value = "default"
|
|
58
|
+
dimension_name = "ses:configuration-set"
|
|
59
|
+
value_source = "messageTag"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
resource "aws_sesv2_email_identity" "sender" {
|
|
64
|
+
count = terraform.workspace == "default" ? 1 : 0
|
|
65
|
+
email_identity = "{{SES_FROM_EMAIL}}"
|
|
66
|
+
configuration_set_name = aws_ses_configuration_set.main[0].name
|
|
67
|
+
}
|
|
68
|
+
|
|
30
69
|
{{SES_ROUTE53_RECORDS_BLOCK}}
|
|
31
70
|
# --- Least-Privilege IAM Policy for the ECS Task Role ---
|
|
32
71
|
resource "aws_iam_role_policy" "ses_send" {
|
|
33
|
-
name =
|
|
72
|
+
name = local.send_policy_safe_name
|
|
34
73
|
role = aws_iam_role.task_role.id
|
|
35
74
|
policy = jsonencode({
|
|
36
75
|
Version = "2012-10-17"
|
|
@@ -64,6 +103,10 @@ output "ses_dkim_tokens" {
|
|
|
64
103
|
value = terraform.workspace == "default" ? aws_ses_domain_dkim.ses[0].dkim_tokens : []
|
|
65
104
|
}
|
|
66
105
|
|
|
106
|
+
output "ses_configuration_set_name" {
|
|
107
|
+
value = terraform.workspace == "default" ? aws_ses_configuration_set.main[0].name : null
|
|
108
|
+
}
|
|
109
|
+
|
|
67
110
|
output "ses_spf_record" {
|
|
68
111
|
value = "v=spf1 include:amazonses.com ~all"
|
|
69
112
|
}
|
|
@@ -5,16 +5,25 @@
|
|
|
5
5
|
# Billing: no fixed hourly instance cost (usage-billed). The first 1M SQS
|
|
6
6
|
# requests/mo are free, then $0.40 per million requests.
|
|
7
7
|
|
|
8
|
+
locals {
|
|
9
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
10
|
+
# names hash-suffix instead of failing validation (queues cap at
|
|
11
|
+
# 80, policies at 128). Mirrors safeSuffixedName in resolvers.js.
|
|
12
|
+
queue_safe_name = length("${local.app_name}-queue") > 80 ? "${substr(local.app_name, 0, 80 - 1 - 5 - 6)}-${substr(md5(local.app_name), 0, 5)}-queue" : "${local.app_name}-queue"
|
|
13
|
+
dlq_safe_name = length("${local.app_name}-dlq") > 80 ? "${substr(local.app_name, 0, 80 - 1 - 5 - 4)}-${substr(md5(local.app_name), 0, 5)}-dlq" : "${local.app_name}-dlq"
|
|
14
|
+
queue_policy_safe_name = length("${local.app_name}-queue-sqs-access") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 17)}-${substr(md5(local.app_name), 0, 5)}-queue-sqs-access" : "${local.app_name}-queue-sqs-access"
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
# --- Dead-Letter Queue ---
|
|
9
18
|
resource "aws_sqs_queue" "dlq" {
|
|
10
|
-
name =
|
|
19
|
+
name = local.dlq_safe_name
|
|
11
20
|
message_retention_seconds = 1209600
|
|
12
21
|
sqs_managed_sse_enabled = true
|
|
13
22
|
}
|
|
14
23
|
|
|
15
24
|
# --- Primary Queue ---
|
|
16
25
|
resource "aws_sqs_queue" "main" {
|
|
17
|
-
name =
|
|
26
|
+
name = local.queue_safe_name
|
|
18
27
|
visibility_timeout_seconds = 30
|
|
19
28
|
message_retention_seconds = 345600
|
|
20
29
|
receive_wait_time_seconds = 20
|
|
@@ -27,7 +36,7 @@ resource "aws_sqs_queue" "main" {
|
|
|
27
36
|
|
|
28
37
|
# --- Least-Privilege IAM Policy for the ECS Task Role ---
|
|
29
38
|
resource "aws_iam_role_policy" "queue_sqs_access" {
|
|
30
|
-
name =
|
|
39
|
+
name = local.queue_policy_safe_name
|
|
31
40
|
role = aws_iam_role.task_role.id
|
|
32
41
|
policy = jsonencode({
|
|
33
42
|
Version = "2012-10-17"
|
|
@@ -19,16 +19,54 @@ resource "aws_security_group" "rds" {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
# 2b. TLS enforcement: the cluster floats its engine version by
|
|
23
|
+
# design, so resolve the regional default at plan time and derive
|
|
24
|
+
# the matching family — no pin, no mismatch, ever. (default_only
|
|
25
|
+
# spans every major's default, so latest pins the lookup down to
|
|
26
|
+
# one; if AWS ever defaults new clusters elsewhere, the family
|
|
27
|
+
# mismatch fails loud at apply instead of enforcing silently
|
|
28
|
+
# wrong.) Pending reboot like the instance groups.
|
|
29
|
+
data "aws_rds_engine_version" "default" {
|
|
30
|
+
engine = "aurora-postgresql"
|
|
31
|
+
default_only = true
|
|
32
|
+
latest = true
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
locals {
|
|
36
|
+
aurora_major = split(".", data.aws_rds_engine_version.default.version_actual)[0]
|
|
37
|
+
|
|
38
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
39
|
+
# names hash-suffix instead of failing validation (identifiers and
|
|
40
|
+
# database names cap at 63, policies at 128). Mirrors
|
|
41
|
+
# safeSuffixedName in resolvers.js.
|
|
42
|
+
cluster_safe_name = length("${local.app_name}-db-cluster") > 63 ? "${substr(local.app_name, 0, 63 - 1 - 5 - 11)}-${substr(md5(local.app_name), 0, 5)}-db-cluster" : "${local.app_name}-db-cluster"
|
|
43
|
+
instance_safe_name = length("${local.app_name}-db-instance-1") > 63 ? "${substr(local.app_name, 0, 63 - 1 - 5 - 14)}-${substr(md5(local.app_name), 0, 5)}-db-instance-1" : "${local.app_name}-db-instance-1"
|
|
44
|
+
database_name_base = replace(local.app_name, "-", "")
|
|
45
|
+
database_name_safe_name = length(local.database_name_base) > 63 ? "${substr(local.database_name_base, 0, 58)}${substr(md5(local.database_name_base), 0, 5)}" : local.database_name_base
|
|
46
|
+
secret_policy_safe_name = length("${local.app_name}-rds-secret-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 18)}-${substr(md5(local.app_name), 0, 5)}-rds-secret-policy" : "${local.app_name}-rds-secret-policy"
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
resource "aws_rds_cluster_parameter_group" "ssl" {
|
|
50
|
+
name = "${local.app_name}-aurorapg${local.aurora_major}"
|
|
51
|
+
family = "aurora-postgresql${local.aurora_major}"
|
|
52
|
+
|
|
53
|
+
parameter {
|
|
54
|
+
name = "rds.force_ssl"
|
|
55
|
+
value = "1"
|
|
56
|
+
apply_method = "pending-reboot"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
22
60
|
# 3. The Aurora PostgreSQL Serverless v2 Cluster (scale-to-zero)
|
|
23
61
|
resource "aws_rds_cluster" "postgres" {
|
|
24
|
-
cluster_identifier =
|
|
62
|
+
cluster_identifier = local.cluster_safe_name
|
|
25
63
|
engine = "aurora-postgresql"
|
|
26
64
|
|
|
27
65
|
# engine_version is intentionally omitted: AWS retires Aurora minor versions
|
|
28
66
|
# (e.g. 16.4 in us-east-2), so new clusters take the regional default.
|
|
29
67
|
# Strip dashes for the database name: Aurora database_name must begin with a
|
|
30
68
|
# letter and contain only alphanumeric characters (e.g. my-project -> myproject)
|
|
31
|
-
database_name =
|
|
69
|
+
database_name = local.database_name_safe_name
|
|
32
70
|
master_username = "dbadmin"
|
|
33
71
|
|
|
34
72
|
# AWS automatically creates and manages the secret in Secrets Manager!
|
|
@@ -37,6 +75,10 @@ resource "aws_rds_cluster" "postgres" {
|
|
|
37
75
|
db_subnet_group_name = aws_db_subnet_group.main.name
|
|
38
76
|
vpc_security_group_ids = [aws_security_group.rds.id]
|
|
39
77
|
|
|
78
|
+
# TLS enforcement (see the parameter group above). In-place update,
|
|
79
|
+
# never a replacement.
|
|
80
|
+
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.ssl.name
|
|
81
|
+
|
|
40
82
|
skip_final_snapshot = true
|
|
41
83
|
storage_encrypted = true
|
|
42
84
|
|
|
@@ -73,7 +115,7 @@ variable "grada_allow_data_loss" {
|
|
|
73
115
|
}
|
|
74
116
|
|
|
75
117
|
resource "aws_rds_cluster_instance" "postgres" {
|
|
76
|
-
identifier =
|
|
118
|
+
identifier = local.instance_safe_name
|
|
77
119
|
cluster_identifier = aws_rds_cluster.postgres.id
|
|
78
120
|
instance_class = "db.serverless"
|
|
79
121
|
engine = aws_rds_cluster.postgres.engine
|
|
@@ -84,7 +126,7 @@ resource "aws_rds_cluster_instance" "postgres" {
|
|
|
84
126
|
|
|
85
127
|
# 4. IAM Permission for RDS Master Password Secret
|
|
86
128
|
resource "aws_iam_role_policy" "rds_secret_access" {
|
|
87
|
-
name =
|
|
129
|
+
name = local.secret_policy_safe_name
|
|
88
130
|
role = aws_iam_role.execution_role.id
|
|
89
131
|
policy = jsonencode({
|
|
90
132
|
Version = "2012-10-17"
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
# 0. Length-safe names: short names render byte-identically; over-cap
|
|
2
|
+
# names hash-suffix instead of failing validation (identifiers cap
|
|
3
|
+
# at 63, database names at 64, policies at 128). Mirrors
|
|
4
|
+
# safeSuffixedName in resolvers.js.
|
|
5
|
+
locals {
|
|
6
|
+
identifier_safe_name = length("${local.app_name}-db") > 63 ? "${substr(local.app_name, 0, 63 - 1 - 5 - 3)}-${substr(md5(local.app_name), 0, 5)}-db" : "${local.app_name}-db"
|
|
7
|
+
db_name_base = replace(local.app_name, "-", "")
|
|
8
|
+
db_name_safe_name = length(local.db_name_base) > 64 ? "${substr(local.db_name_base, 0, 59)}${substr(md5(local.db_name_base), 0, 5)}" : local.db_name_base
|
|
9
|
+
secret_policy_safe_name = length("${local.app_name}-rds-secret-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 18)}-${substr(md5(local.app_name), 0, 5)}-rds-secret-policy" : "${local.app_name}-rds-secret-policy"
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
# 1. Database Subnet Group (isolated subnets live in network.tf, shared
|
|
2
13
|
# with ElastiCache so every data-plane group stays off public subnets)
|
|
3
14
|
resource "aws_db_subnet_group" "main" {
|
|
@@ -19,9 +30,24 @@ resource "aws_security_group" "rds" {
|
|
|
19
30
|
}
|
|
20
31
|
}
|
|
21
32
|
|
|
33
|
+
# 2b. TLS enforcement: clients connect with TLS (see the
|
|
34
|
+
# database-connections guide), so require it server-side. Pending
|
|
35
|
+
# reboot: existing trees enforce at the next reboot, never as a
|
|
36
|
+
# surprise mid-apply restart (see the SSL upgrade runbook).
|
|
37
|
+
resource "aws_db_parameter_group" "ssl" {
|
|
38
|
+
name = "${local.app_name}-mysql80"
|
|
39
|
+
family = "mysql8.0"
|
|
40
|
+
|
|
41
|
+
parameter {
|
|
42
|
+
name = "require_secure_transport"
|
|
43
|
+
value = "ON"
|
|
44
|
+
apply_method = "pending-reboot"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
22
48
|
# 3. The MySQL Instance
|
|
23
49
|
resource "aws_db_instance" "postgres" {
|
|
24
|
-
identifier =
|
|
50
|
+
identifier = local.identifier_safe_name
|
|
25
51
|
engine = "mysql"
|
|
26
52
|
engine_version = "8.0"
|
|
27
53
|
instance_class = "db.t4g.micro"
|
|
@@ -36,9 +62,12 @@ resource "aws_db_instance" "postgres" {
|
|
|
36
62
|
maintenance_window = "sun:04:00-sun:05:00"
|
|
37
63
|
storage_encrypted = true
|
|
38
64
|
|
|
65
|
+
# TLS enforcement (see the parameter group above).
|
|
66
|
+
parameter_group_name = aws_db_parameter_group.ssl.name
|
|
67
|
+
|
|
39
68
|
# Strip dashes for the database name: RDS MySQL db_name must begin with a
|
|
40
69
|
# letter and contain only alphanumeric characters (e.g. my-project -> myproject)
|
|
41
|
-
db_name =
|
|
70
|
+
db_name = local.db_name_safe_name
|
|
42
71
|
username = "dbadmin"
|
|
43
72
|
|
|
44
73
|
# AWS automatically creates and manages the secret in Secrets Manager!
|
|
@@ -66,7 +95,7 @@ variable "grada_allow_data_loss" {
|
|
|
66
95
|
|
|
67
96
|
# 4. IAM Permission for RDS Master Password Secret
|
|
68
97
|
resource "aws_iam_role_policy" "rds_secret_access" {
|
|
69
|
-
name =
|
|
98
|
+
name = local.secret_policy_safe_name
|
|
70
99
|
role = aws_iam_role.execution_role.id
|
|
71
100
|
policy = jsonencode({
|
|
72
101
|
Version = "2012-10-17"
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
# 0. Length-safe names: short names render byte-identically; over-cap
|
|
2
|
+
# names hash-suffix instead of failing validation (identifiers and
|
|
3
|
+
# database names cap at 63, policies at 128). Mirrors
|
|
4
|
+
# safeSuffixedName in resolvers.js.
|
|
5
|
+
locals {
|
|
6
|
+
identifier_safe_name = length("${local.app_name}-db") > 63 ? "${substr(local.app_name, 0, 63 - 1 - 5 - 3)}-${substr(md5(local.app_name), 0, 5)}-db" : "${local.app_name}-db"
|
|
7
|
+
db_name_base = replace(local.app_name, "-", "_")
|
|
8
|
+
db_name_safe_name = length(local.db_name_base) > 63 ? "${substr(local.db_name_base, 0, 57)}_${substr(md5(local.db_name_base), 0, 5)}" : local.db_name_base
|
|
9
|
+
secret_policy_safe_name = length("${local.app_name}-rds-secret-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 18)}-${substr(md5(local.app_name), 0, 5)}-rds-secret-policy" : "${local.app_name}-rds-secret-policy"
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
# 1. Database Subnet Group (isolated subnets live in network.tf, shared
|
|
2
13
|
# with ElastiCache so every data-plane group stays off public subnets)
|
|
3
14
|
resource "aws_db_subnet_group" "main" {
|
|
@@ -19,9 +30,24 @@ resource "aws_security_group" "rds" {
|
|
|
19
30
|
}
|
|
20
31
|
}
|
|
21
32
|
|
|
33
|
+
# 2b. TLS enforcement: clients connect with TLS (see the
|
|
34
|
+
# database-connections guide), so require it server-side. Pending
|
|
35
|
+
# reboot: existing trees enforce at the next reboot, never as a
|
|
36
|
+
# surprise mid-apply restart (see the SSL upgrade runbook).
|
|
37
|
+
resource "aws_db_parameter_group" "ssl" {
|
|
38
|
+
name = "${local.app_name}-postgres16"
|
|
39
|
+
family = "postgres16"
|
|
40
|
+
|
|
41
|
+
parameter {
|
|
42
|
+
name = "rds.force_ssl"
|
|
43
|
+
value = "1"
|
|
44
|
+
apply_method = "pending-reboot"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
22
48
|
# 3. The PostgreSQL Instance
|
|
23
49
|
resource "aws_db_instance" "postgres" {
|
|
24
|
-
identifier =
|
|
50
|
+
identifier = local.identifier_safe_name
|
|
25
51
|
engine = "postgres"
|
|
26
52
|
engine_version = "16"
|
|
27
53
|
instance_class = "db.t4g.micro"
|
|
@@ -36,8 +62,11 @@ resource "aws_db_instance" "postgres" {
|
|
|
36
62
|
maintenance_window = "sun:04:00-sun:05:00"
|
|
37
63
|
storage_encrypted = true
|
|
38
64
|
|
|
65
|
+
# TLS enforcement (see the parameter group above).
|
|
66
|
+
parameter_group_name = aws_db_parameter_group.ssl.name
|
|
67
|
+
|
|
39
68
|
# Clean up dashes for the database name (e.g. my-project -> my_project)
|
|
40
|
-
db_name =
|
|
69
|
+
db_name = local.db_name_safe_name
|
|
41
70
|
username = "dbadmin"
|
|
42
71
|
|
|
43
72
|
# AWS automatically creates and manages the secret in Secrets Manager!
|
|
@@ -65,7 +94,7 @@ variable "grada_allow_data_loss" {
|
|
|
65
94
|
|
|
66
95
|
# 4. IAM Permission for RDS Master Password Secret
|
|
67
96
|
resource "aws_iam_role_policy" "rds_secret_access" {
|
|
68
|
-
name =
|
|
97
|
+
name = local.secret_policy_safe_name
|
|
69
98
|
role = aws_iam_role.execution_role.id
|
|
70
99
|
policy = jsonencode({
|
|
71
100
|
Version = "2012-10-17"
|
|
@@ -40,11 +40,19 @@ locals {
|
|
|
40
40
|
|
|
41
41
|
# Full application name: project + workspace + collision suffix.
|
|
42
42
|
app_name = "{{PROJECT_NAME}}${local.env_suffix}${local.grada_resource_suffix}"
|
|
43
|
+
|
|
44
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
45
|
+
# names hash-suffix instead of failing IAM validation. Mirrors
|
|
46
|
+
# safeSuffixedName in resolvers.js.
|
|
47
|
+
task_role_safe_name = length("${local.app_name}-task-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 10)}-${substr(md5(local.app_name), 0, 5)}-task-role" : "${local.app_name}-task-role"
|
|
48
|
+
execution_role_safe_name = length("${local.app_name}-execution-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 16)}-${substr(md5(local.app_name), 0, 5)}-execution-role" : "${local.app_name}-execution-role"
|
|
49
|
+
fn_safe_name = length("${local.app_name}-fn") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 3)}-${substr(md5(local.app_name), 0, 5)}-fn" : "${local.app_name}-fn"
|
|
50
|
+
secrets_policy_safe_name = length("${local.app_name}-app-secrets-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 19)}-${substr(md5(local.app_name), 0, 5)}-app-secrets-policy" : "${local.app_name}-app-secrets-policy"
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
# --- CloudWatch Logs ---
|
|
46
54
|
resource "aws_cloudwatch_log_group" "app" {
|
|
47
|
-
name = "/aws/lambda/${local.
|
|
55
|
+
name = "/aws/lambda/${local.fn_safe_name}"
|
|
48
56
|
retention_in_days = 30
|
|
49
57
|
}
|
|
50
58
|
|
|
@@ -85,7 +93,7 @@ data "aws_iam_policy_document" "ecs_trust" {
|
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
resource "aws_iam_role" "execution_role" {
|
|
88
|
-
name =
|
|
96
|
+
name = local.execution_role_safe_name
|
|
89
97
|
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
90
98
|
permissions_boundary = local.preview_boundary_arn
|
|
91
99
|
}
|
|
@@ -110,7 +118,7 @@ data "aws_iam_policy_document" "lambda_task_trust" {
|
|
|
110
118
|
}
|
|
111
119
|
|
|
112
120
|
resource "aws_iam_role" "task_role" {
|
|
113
|
-
name =
|
|
121
|
+
name = local.task_role_safe_name
|
|
114
122
|
assume_role_policy = data.aws_iam_policy_document.lambda_task_trust.json
|
|
115
123
|
permissions_boundary = local.preview_boundary_arn
|
|
116
124
|
}
|
|
@@ -127,7 +135,7 @@ resource "aws_iam_role_policy_attachment" "lambda_vpc_access" {
|
|
|
127
135
|
# reach Secrets Manager directly; VPC-attached functions should prefer
|
|
128
136
|
# plain environment variables injected with `grada add`).
|
|
129
137
|
resource "aws_iam_role_policy" "app_secrets_access" {
|
|
130
|
-
name =
|
|
138
|
+
name = local.secrets_policy_safe_name
|
|
131
139
|
role = aws_iam_role.task_role.id
|
|
132
140
|
policy = jsonencode({
|
|
133
141
|
Version = "2012-10-17"
|
|
@@ -143,7 +151,7 @@ resource "aws_iam_role_policy" "app_secrets_access" {
|
|
|
143
151
|
|
|
144
152
|
# --- Lambda Function (container image + Web Adapter) ---
|
|
145
153
|
resource "aws_lambda_function" "app" {
|
|
146
|
-
function_name =
|
|
154
|
+
function_name = local.fn_safe_name
|
|
147
155
|
role = aws_iam_role.task_role.arn
|
|
148
156
|
package_type = "Image"
|
|
149
157
|
# Lambda requires an image in a private same-account ECR repository.
|
|
@@ -50,6 +50,17 @@ locals {
|
|
|
50
50
|
|
|
51
51
|
# Full application name: project + workspace + collision suffix.
|
|
52
52
|
app_name = "{{PROJECT_NAME}}${local.env_suffix}${local.grada_resource_suffix}"
|
|
53
|
+
|
|
54
|
+
# Length-safe names: short names render byte-identically; over-cap
|
|
55
|
+
# names hash-suffix (deterministic md5 stem) instead of truncating
|
|
56
|
+
# into collisions (ALB/TG) or failing validation (IAM). Mirrors
|
|
57
|
+
# safeSuffixedName in resolvers.js — stem is always
|
|
58
|
+
# cap - 1 - 5 - len(suffix). Existing trees never rename.
|
|
59
|
+
alb_safe_name = length("${local.app_name}-alb") > 32 ? "${substr(local.app_name, 0, 32 - 1 - 5 - 4)}-${substr(md5(local.app_name), 0, 5)}-alb" : "${local.app_name}-alb"
|
|
60
|
+
tg_safe_name = length("${local.app_name}-tg") > 32 ? "${substr(local.app_name, 0, 32 - 1 - 5 - 3)}-${substr(md5(local.app_name), 0, 5)}-tg" : "${local.app_name}-tg"
|
|
61
|
+
task_role_safe_name = length("${local.app_name}-task-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 10)}-${substr(md5(local.app_name), 0, 5)}-task-role" : "${local.app_name}-task-role"
|
|
62
|
+
execution_role_safe_name = length("${local.app_name}-execution-role") > 64 ? "${substr(local.app_name, 0, 64 - 1 - 5 - 16)}-${substr(md5(local.app_name), 0, 5)}-execution-role" : "${local.app_name}-execution-role"
|
|
63
|
+
exec_policy_safe_name = length("${local.app_name}-ecs-exec-policy") > 128 ? "${substr(local.app_name, 0, 128 - 1 - 5 - 16)}-${substr(md5(local.app_name), 0, 5)}-ecs-exec-policy" : "${local.app_name}-ecs-exec-policy"
|
|
53
64
|
}
|
|
54
65
|
|
|
55
66
|
# --- CloudWatch Logs ---
|
|
@@ -94,7 +105,7 @@ data "aws_iam_policy_document" "ecs_trust" {
|
|
|
94
105
|
}
|
|
95
106
|
|
|
96
107
|
resource "aws_iam_role" "execution_role" {
|
|
97
|
-
name =
|
|
108
|
+
name = local.execution_role_safe_name
|
|
98
109
|
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
99
110
|
permissions_boundary = local.preview_boundary_arn
|
|
100
111
|
}
|
|
@@ -107,7 +118,7 @@ resource "aws_iam_role_policy_attachment" "execution_role_policy" {
|
|
|
107
118
|
# --- IAM: Task Role ---
|
|
108
119
|
# Allows your application code running INSIDE the container to access AWS services
|
|
109
120
|
resource "aws_iam_role" "task_role" {
|
|
110
|
-
name =
|
|
121
|
+
name = local.task_role_safe_name
|
|
111
122
|
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
112
123
|
permissions_boundary = local.preview_boundary_arn
|
|
113
124
|
}
|
|
@@ -115,7 +126,7 @@ resource "aws_iam_role" "task_role" {
|
|
|
115
126
|
# --- IAM: ECS Exec (SSM) permissions for the task role ---
|
|
116
127
|
# Required for `npx grada-run exec` — lets Fargate open an SSM session.
|
|
117
128
|
resource "aws_iam_role_policy" "ecs_exec" {
|
|
118
|
-
name =
|
|
129
|
+
name = local.exec_policy_safe_name
|
|
119
130
|
role = aws_iam_role.task_role.id
|
|
120
131
|
policy = jsonencode({
|
|
121
132
|
Version = "2012-10-17"
|
|
@@ -206,7 +217,7 @@ resource "aws_ecs_task_definition" "app" {
|
|
|
206
217
|
# --- Application Load Balancer ---
|
|
207
218
|
# trivy:ignore:AVD-AWS-0053 - This ALB is intended to be publicly facing behind CloudFront
|
|
208
219
|
resource "aws_lb" "main" {
|
|
209
|
-
name =
|
|
220
|
+
name = local.alb_safe_name
|
|
210
221
|
load_balancer_type = "application"
|
|
211
222
|
security_groups = [aws_security_group.alb.id]
|
|
212
223
|
subnets = aws_subnet.public[*].id
|
|
@@ -214,7 +225,7 @@ resource "aws_lb" "main" {
|
|
|
214
225
|
}
|
|
215
226
|
|
|
216
227
|
resource "aws_lb_target_group" "app" {
|
|
217
|
-
name =
|
|
228
|
+
name = local.tg_safe_name
|
|
218
229
|
port = {{PORT}}
|
|
219
230
|
protocol = "HTTP"
|
|
220
231
|
vpc_id = aws_vpc.main.id
|