grada-run 0.49.10 → 0.50.0

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.
@@ -73,7 +73,7 @@ jobs:
73
73
 
74
74
  # --- 3. INFRASTRUCTURE SYNC (The New Marketplace Action) ---
75
75
  - name: Apply Infrastructure Changes
76
- uses: grada-run/grada-action@12227808ae4caafaec1041319645586a1132fc57 # v1
76
+ uses: grada-run/grada-action@9df2e2c12314b6165e62f0e615edb01d6a81c40b # v1
77
77
  with:
78
78
  aws-region: ${{ env.AWS_REGION }}
79
79
  role-to-assume: arn:aws:iam::{{AWS_ACCOUNT_ID}}:role/{{DEPLOY_ROLE_NAME}}
@@ -80,6 +80,9 @@ jobs:
80
80
  # start tasks on an unpushed tag), but the repository itself is
81
81
  # Terraform-managed — so it is bootstrapped with a targeted apply
82
82
  # first. Idempotent on existing stacks.
83
+ - name: Setup Terraform
84
+ uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
85
+
83
86
  - name: Bootstrap ECR repository
84
87
  working-directory: ./terraform
85
88
  run: |
@@ -141,7 +144,7 @@ jobs:
141
144
  # the tag exists. A gate failure below can therefore never strand
142
145
  # the service on an unpullable revision.
143
146
  - name: Apply Infrastructure Changes
144
- uses: grada-run/grada-action@12227808ae4caafaec1041319645586a1132fc57 # v1
147
+ uses: grada-run/grada-action@9df2e2c12314b6165e62f0e615edb01d6a81c40b # v1
145
148
  with:
146
149
  aws-region: ${{ env.AWS_REGION }}
147
150
  role-to-assume: arn:aws:iam::{{AWS_ACCOUNT_ID}}:role/{{DEPLOY_ROLE_NAME}}
@@ -48,6 +48,9 @@ jobs:
48
48
  terraform_wrapper: false
49
49
 
50
50
  - name: Terraform Init
51
+ # Without creds there is nothing to init against: skip with the
52
+ # notice above instead of failing red on the S3 backend.
53
+ if: steps.aws-creds.outcome == 'success'
51
54
  working-directory: ./terraform
52
55
  run: terraform init -input=false
53
56
 
@@ -192,3 +195,71 @@ jobs:
192
195
  --arg summary "$SUMMARY" \
193
196
  '{text: (":warning: Infrastructure drift detected in " + $repo + "\n" + $url + "\n```" + $summary + "```")}' > /tmp/drift-slack.json
194
197
  curl -sS -X POST -H 'Content-Type: application/json' -d @/tmp/drift-slack.json "$SLACK_WEBHOOK_URL"
198
+
199
+ # Pre-deploy snapshots accumulate per push with no retention: collect
200
+ # them (plus orphaned images, log groups, and EIPs) on the same daily
201
+ # schedule. A separate job so its failures can't trip the drift
202
+ # check-failed logic (that keys on the plan job's outputs) — gc gets
203
+ # its own failure label instead.
204
+ gc:
205
+ name: Collect garbage snapshots and orphans
206
+ runs-on: ubuntu-latest
207
+ timeout-minutes: 15
208
+
209
+ steps:
210
+ - name: Checkout Code
211
+ uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
212
+
213
+ - name: Configure AWS Credentials
214
+ id: aws-creds
215
+ uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4
216
+ continue-on-error: true
217
+ with:
218
+ role-to-assume: {{ROLE_ARN}}
219
+ aws-region: ${{ env.AWS_REGION }}
220
+
221
+ - name: Interpret credential failure
222
+ if: steps.aws-creds.outcome == 'failure'
223
+ run: |
224
+ echo "::notice::AWS credential assumption failed — skipping garbage collection this run."
225
+
226
+ - name: Collect garbage
227
+ id: gc
228
+ if: steps.aws-creds.outcome == 'success'
229
+ run: npx --yes grada-run gc --headless --yes --region ${{ env.AWS_REGION }}
230
+
231
+ # gc failures need a human (a stuck snapshot pattern rots silently),
232
+ # but under their own label — never the drift labels above. A green
233
+ # run closes the stale issue; cancelled/skipped runs stay silent.
234
+ - name: Report gc result
235
+ if: always() && steps.aws-creds.outcome == 'success'
236
+ uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
237
+ with:
238
+ script: |
239
+ const { owner, repo } = context.repo;
240
+ const label = 'grada-gc-failed';
241
+ const outcome = '${{ steps.gc.outcome }}';
242
+ const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${context.runId}`;
243
+ try {
244
+ await github.rest.issues.getLabel({ owner, repo, name: label });
245
+ } catch (error) {
246
+ if (error.status === 404) {
247
+ await github.rest.issues.createLabel({ owner, repo, name: label, color: 'd93f0b', description: 'Scheduled garbage collection failed' });
248
+ } else {
249
+ throw error;
250
+ }
251
+ }
252
+ const { data: open } = await github.rest.issues.listForRepo({ owner, repo, state: 'open', labels: label });
253
+ const existing = open[0];
254
+ if (outcome === 'failure') {
255
+ const body = `## 🧹 Garbage Collection Failed\n\nThe scheduled gc run errored on \`${new Date().toISOString()}\` ([workflow run](${runUrl})) — snapshots and orphans are accumulating.\n\nFix the run (credentials, permissions, gc output above) and the issue closes on the next green run.`;
256
+ if (existing) {
257
+ await github.rest.issues.createComment({ owner, repo, issue_number: existing.number, body: `gc still failing on \`${new Date().toISOString()}\` ([run](${runUrl})).` });
258
+ await github.rest.issues.update({ owner, repo, issue_number: existing.number, body });
259
+ } else {
260
+ await github.rest.issues.create({ owner, repo, title: '🧹 Garbage Collection Failed', labels: [label], body });
261
+ }
262
+ } else if (outcome === 'success' && existing) {
263
+ await github.rest.issues.createComment({ owner, repo, issue_number: existing.number, body: `✅ Garbage collection healthy again on \`${new Date().toISOString()}\` ([run](${runUrl})); closing.` });
264
+ await github.rest.issues.update({ owner, repo, issue_number: existing.number, state: 'closed' });
265
+ }
@@ -21,6 +21,9 @@ env:
21
21
  ECR_REPOSITORY: {{PROJECT_NAME}}{{RESOURCE_SUFFIX}}-repo
22
22
  WORKSPACE: pr-${{ github.event.pull_request.number }}
23
23
  CF_COMMENT: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-cdn
24
+ PREVIEW_CLUSTER: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-cluster
25
+ PREVIEW_SERVICE: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-service
26
+ PREVIEW_TASK_FAMILY: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-task
24
27
 
25
28
  permissions:
26
29
  id-token: write
@@ -99,6 +102,72 @@ jobs:
99
102
  PREVIEW_URL=$(terraform output -raw cloudfront_url)
100
103
  echo "PREVIEW_URL=$PREVIEW_URL" >> $GITHUB_ENV
101
104
 
105
+ # The image tag is static per PR (:pr-N), so a re-push changes no
106
+ # Terraform input — without these steps the service keeps running
107
+ # the first push's image. Mirror the deploy workflow: register a
108
+ # digest-pinned revision (immutable, so concurrent pushes can't
109
+ # swap the image mid-rollout), force the rollout, and verify it
110
+ # stuck.
111
+ - name: Register preview task definition revision
112
+ id: register-preview-task-def
113
+ if: steps.aws-creds.outcome == 'success'
114
+ env:
115
+ ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
116
+ run: |
117
+ DIGEST=$(aws ecr describe-images \
118
+ --repository-name ${{ env.ECR_REPOSITORY }} \
119
+ --image-ids imageTag=${{ env.WORKSPACE }} \
120
+ --query 'imageDetails[0].imageDigest' \
121
+ --output text \
122
+ --region ${{ env.AWS_REGION }})
123
+ IMAGE_URI=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}@${DIGEST}
124
+ TASK_DEF=$(aws ecs describe-task-definition \
125
+ --task-definition ${{ env.PREVIEW_TASK_FAMILY }} \
126
+ --region ${{ env.AWS_REGION }} \
127
+ --query 'taskDefinition' \
128
+ --output json)
129
+ NEW_TASK_DEF=$(echo "$TASK_DEF" | jq \
130
+ --arg IMAGE "$IMAGE_URI" \
131
+ '.containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt)')
132
+ NEW_TASK_ARN=$(aws ecs register-task-definition \
133
+ --region ${{ env.AWS_REGION }} \
134
+ --cli-input-json "$NEW_TASK_DEF" \
135
+ --query 'taskDefinition.taskDefinitionArn' \
136
+ --output text)
137
+ echo "task-arn=$NEW_TASK_ARN" >> $GITHUB_OUTPUT
138
+
139
+ - name: Force preview deployment
140
+ if: steps.aws-creds.outcome == 'success'
141
+ run: |
142
+ aws ecs update-service \
143
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
144
+ --service ${{ env.PREVIEW_SERVICE }} \
145
+ --task-definition ${{ steps.register-preview-task-def.outputs.task-arn }} \
146
+ --force-new-deployment \
147
+ --region ${{ env.AWS_REGION }}
148
+
149
+ # update-service returns before the rollout finishes: wait for
150
+ # stability, then verify PRIMARY actually runs the registered
151
+ # revision — a circuit-breaker rollback re-stabilizes on the OLD
152
+ # revision, which the wait alone would report green.
153
+ - name: Wait for preview rollout
154
+ if: steps.aws-creds.outcome == 'success'
155
+ run: |
156
+ aws ecs wait services-stable \
157
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
158
+ --services ${{ env.PREVIEW_SERVICE }} \
159
+ --region ${{ env.AWS_REGION }}
160
+ PRIMARY_TD=$(aws ecs describe-services \
161
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
162
+ --services ${{ env.PREVIEW_SERVICE }} \
163
+ --query 'services[0].deployments[?status==`PRIMARY`].taskDefinition' \
164
+ --output text \
165
+ --region ${{ env.AWS_REGION }})
166
+ if [ "$PRIMARY_TD" != "${{ steps.register-preview-task-def.outputs.task-arn }}" ]; then
167
+ echo "Preview rollout of ${{ steps.register-preview-task-def.outputs.task-arn }} did not stick (PRIMARY runs $PRIMARY_TD) — the deployment failed or rolled back." >&2
168
+ exit 1
169
+ fi
170
+
102
171
  # Every push redeploys the preview: clear its CDN so new HTML shows
103
172
  # immediately instead of serving stale pages until TTL expiry.
104
173
  - name: Invalidate CloudFront
@@ -7,10 +7,10 @@
7
7
  # chosen model in the AWS Bedrock console first.
8
8
 
9
9
  # --- Least-Privilege IAM Policy for the ECS Task Role ---
10
- # Scoped to the chosen model in the deploy region: the
10
+ # Scoped to the chosen model in the deploy region and account: the
11
11
  # foundation-model ARN names the model exactly, and the
12
12
  # inference-profile line stays (profile-style model IDs invoke through
13
- # profiles) but pinned to the region.
13
+ # profiles) but pinned to the region and the caller's account.
14
14
  resource "aws_iam_role_policy" "ai_bedrock_access" {
15
15
  name = "${local.app_name}-ai-bedrock-access"
16
16
  role = aws_iam_role.task_role.id
@@ -25,7 +25,7 @@ resource "aws_iam_role_policy" "ai_bedrock_access" {
25
25
  ]
26
26
  Resource = [
27
27
  "arn:aws:bedrock:{{REGION}}::foundation-model/{{BEDROCK_MODEL_ID}}",
28
- "arn:aws:bedrock:{{REGION}}:*:inference-profile/*"
28
+ "arn:aws:bedrock:{{REGION}}:${data.aws_caller_identity.current.account_id}:inference-profile/*"
29
29
  ]
30
30
  }
31
31
  ]
@@ -11,9 +11,19 @@
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
+ }
23
+
14
24
  # --- IAM Role assumed by EventBridge Scheduler ---
15
25
  resource "aws_iam_role" "scheduler_cron_role" {
16
- name = "${local.app_name}-cron-scheduler-role"
26
+ name = local.scheduler_role_safe_name
17
27
  permissions_boundary = local.preview_boundary_arn
18
28
  assume_role_policy = jsonencode({
19
29
  Version = "2012-10-17"
@@ -48,9 +58,18 @@ resource "aws_iam_role_policy" "scheduler_cron_policy" {
48
58
  })
49
59
  }
50
60
 
61
+ # --- Schedule group (failure metrics scope) ---
62
+ # Scheduler metrics dimension by group only (no per-schedule dimension),
63
+ # so the project's schedules get their own group instead of the shared
64
+ # default one — the alerts.tf failure alarms stay project-scoped.
65
+ resource "aws_scheduler_schedule_group" "crons" {
66
+ name = local.schedule_group_safe_name
67
+ }
68
+
51
69
  # --- Scheduled invocation (single schedule per project) ---
52
70
  resource "aws_scheduler_schedule" "cron" {
53
- name = "${local.app_name}-cron-{{CRON_NAME}}"
71
+ name = local.schedule_safe_name
72
+ group_name = aws_scheduler_schedule_group.crons.name
54
73
  schedule_expression = "{{SCHEDULE_EXPRESSION}}"
55
74
  schedule_expression_timezone = "{{SCHEDULE_TIMEZONE}}"
56
75
  # Sleep guard: stays disabled while grada sleep's auto-vars are active
@@ -5,9 +5,19 @@
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
+ }
17
+
8
18
  # --- IAM Role assumed by EventBridge Scheduler ---
9
19
  resource "aws_iam_role" "scheduler_cron_role" {
10
- name = "${local.app_name}-cron-scheduler-role"
20
+ name = local.scheduler_role_safe_name
11
21
  permissions_boundary = local.preview_boundary_arn
12
22
  assume_role_policy = jsonencode({
13
23
  Version = "2012-10-17"
@@ -55,9 +65,18 @@ resource "aws_iam_role_policy" "scheduler_cron_policy" {
55
65
  })
56
66
  }
57
67
 
68
+ # --- Schedule group (failure metrics scope) ---
69
+ # Scheduler metrics dimension by group only (no per-schedule dimension),
70
+ # so the project's schedules get their own group instead of the shared
71
+ # default one — the alerts.tf failure alarms stay project-scoped.
72
+ resource "aws_scheduler_schedule_group" "crons" {
73
+ name = local.schedule_group_safe_name
74
+ }
75
+
58
76
  # --- Scheduled task (single schedule per project) ---
59
77
  resource "aws_scheduler_schedule" "cron" {
60
- name = "${local.app_name}-cron-{{CRON_NAME}}"
78
+ name = local.schedule_safe_name
79
+ group_name = aws_scheduler_schedule_group.crons.name
61
80
  schedule_expression = "{{SCHEDULE_EXPRESSION}}"
62
81
  schedule_expression_timezone = "{{SCHEDULE_TIMEZONE}}"
63
82
  # 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" {
@@ -27,6 +27,38 @@ resource "aws_ses_domain_mail_from" "ses" {
27
27
  behavior_on_mx_failure = "UseDefaultValue"
28
28
  }
29
29
 
30
+ # --- Feedback channel ---
31
+ # Bounces and complaints publish to CloudWatch (dashboard + alarms)
32
+ # instead of vanishing. The v2 sender identity sets this set as its
33
+ # default, so app mail needs no per-send configuration; addresses
34
+ # under the verified domain above verify automatically.
35
+ resource "aws_ses_configuration_set" "main" {
36
+ count = terraform.workspace == "default" ? 1 : 0
37
+ name = "${local.app_name}-ses-events"
38
+ reputation_metrics_enabled = true
39
+ sending_enabled = true
40
+ }
41
+
42
+ resource "aws_ses_event_destination" "cloudwatch" {
43
+ count = terraform.workspace == "default" ? 1 : 0
44
+ name = "cloudwatch"
45
+ configuration_set_name = aws_ses_configuration_set.main[0].name
46
+ enabled = true
47
+ matching_types = ["bounce", "complaint", "send"]
48
+
49
+ cloudwatch_destination {
50
+ default_value = "default"
51
+ dimension_name = "ses:configuration-set"
52
+ value_source = "messageTag"
53
+ }
54
+ }
55
+
56
+ resource "aws_sesv2_email_identity" "sender" {
57
+ count = terraform.workspace == "default" ? 1 : 0
58
+ email_identity = "{{SES_FROM_EMAIL}}"
59
+ configuration_set_name = aws_ses_configuration_set.main[0].name
60
+ }
61
+
30
62
  {{SES_ROUTE53_RECORDS_BLOCK}}
31
63
  # --- Least-Privilege IAM Policy for the ECS Task Role ---
32
64
  resource "aws_iam_role_policy" "ses_send" {
@@ -64,6 +96,10 @@ output "ses_dkim_tokens" {
64
96
  value = terraform.workspace == "default" ? aws_ses_domain_dkim.ses[0].dkim_tokens : []
65
97
  }
66
98
 
99
+ output "ses_configuration_set_name" {
100
+ value = terraform.workspace == "default" ? aws_ses_configuration_set.main[0].name : null
101
+ }
102
+
67
103
  output "ses_spf_record" {
68
104
  value = "v=spf1 include:amazonses.com ~all"
69
105
  }
@@ -19,6 +19,11 @@ resource "aws_security_group" "rds" {
19
19
  }
20
20
  }
21
21
 
22
+ # 2b. No SSL parameter group yet: this template floats its engine
23
+ # version by design, and a cluster parameter group family must match
24
+ # it exactly — forcing a family would pin the engine by side effect.
25
+ # Deferred until the engine-version decision; the instance engines
26
+ # already enforce TLS via their own groups.
22
27
  # 3. The Aurora PostgreSQL Serverless v2 Cluster (scale-to-zero)
23
28
  resource "aws_rds_cluster" "postgres" {
24
29
  cluster_identifier = "${local.app_name}-db-cluster"
@@ -19,6 +19,21 @@ resource "aws_security_group" "rds" {
19
19
  }
20
20
  }
21
21
 
22
+ # 2b. TLS enforcement: clients connect with TLS (see the
23
+ # database-connections guide), so require it server-side. Pending
24
+ # reboot: existing trees enforce at the next reboot, never as a
25
+ # surprise mid-apply restart (see the SSL upgrade runbook).
26
+ resource "aws_db_parameter_group" "ssl" {
27
+ name = "${local.app_name}-mysql80"
28
+ family = "mysql8.0"
29
+
30
+ parameter {
31
+ name = "require_secure_transport"
32
+ value = "ON"
33
+ apply_method = "pending-reboot"
34
+ }
35
+ }
36
+
22
37
  # 3. The MySQL Instance
23
38
  resource "aws_db_instance" "postgres" {
24
39
  identifier = "${local.app_name}-db"
@@ -36,6 +51,9 @@ resource "aws_db_instance" "postgres" {
36
51
  maintenance_window = "sun:04:00-sun:05:00"
37
52
  storage_encrypted = true
38
53
 
54
+ # TLS enforcement (see the parameter group above).
55
+ parameter_group_name = aws_db_parameter_group.ssl.name
56
+
39
57
  # Strip dashes for the database name: RDS MySQL db_name must begin with a
40
58
  # letter and contain only alphanumeric characters (e.g. my-project -> myproject)
41
59
  db_name = replace(local.app_name, "-", "")
@@ -19,6 +19,21 @@ resource "aws_security_group" "rds" {
19
19
  }
20
20
  }
21
21
 
22
+ # 2b. TLS enforcement: clients connect with TLS (see the
23
+ # database-connections guide), so require it server-side. Pending
24
+ # reboot: existing trees enforce at the next reboot, never as a
25
+ # surprise mid-apply restart (see the SSL upgrade runbook).
26
+ resource "aws_db_parameter_group" "ssl" {
27
+ name = "${local.app_name}-postgres16"
28
+ family = "postgres16"
29
+
30
+ parameter {
31
+ name = "rds.force_ssl"
32
+ value = "1"
33
+ apply_method = "pending-reboot"
34
+ }
35
+ }
36
+
22
37
  # 3. The PostgreSQL Instance
23
38
  resource "aws_db_instance" "postgres" {
24
39
  identifier = "${local.app_name}-db"
@@ -36,6 +51,9 @@ resource "aws_db_instance" "postgres" {
36
51
  maintenance_window = "sun:04:00-sun:05:00"
37
52
  storage_encrypted = true
38
53
 
54
+ # TLS enforcement (see the parameter group above).
55
+ parameter_group_name = aws_db_parameter_group.ssl.name
56
+
39
57
  # Clean up dashes for the database name (e.g. my-project -> my_project)
40
58
  db_name = replace(local.app_name, "-", "_")
41
59
  username = "dbadmin"
@@ -40,6 +40,12 @@ 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"
43
49
  }
44
50
 
45
51
  # --- CloudWatch Logs ---
@@ -85,7 +91,7 @@ data "aws_iam_policy_document" "ecs_trust" {
85
91
  }
86
92
 
87
93
  resource "aws_iam_role" "execution_role" {
88
- name = "${local.app_name}-execution-role"
94
+ name = local.execution_role_safe_name
89
95
  assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
90
96
  permissions_boundary = local.preview_boundary_arn
91
97
  }
@@ -110,7 +116,7 @@ data "aws_iam_policy_document" "lambda_task_trust" {
110
116
  }
111
117
 
112
118
  resource "aws_iam_role" "task_role" {
113
- name = "${local.app_name}-task-role"
119
+ name = local.task_role_safe_name
114
120
  assume_role_policy = data.aws_iam_policy_document.lambda_task_trust.json
115
121
  permissions_boundary = local.preview_boundary_arn
116
122
  }
@@ -50,6 +50,16 @@ 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"
53
63
  }
54
64
 
55
65
  # --- CloudWatch Logs ---
@@ -94,7 +104,7 @@ data "aws_iam_policy_document" "ecs_trust" {
94
104
  }
95
105
 
96
106
  resource "aws_iam_role" "execution_role" {
97
- name = "${local.app_name}-execution-role"
107
+ name = local.execution_role_safe_name
98
108
  assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
99
109
  permissions_boundary = local.preview_boundary_arn
100
110
  }
@@ -107,7 +117,7 @@ resource "aws_iam_role_policy_attachment" "execution_role_policy" {
107
117
  # --- IAM: Task Role ---
108
118
  # Allows your application code running INSIDE the container to access AWS services
109
119
  resource "aws_iam_role" "task_role" {
110
- name = "${local.app_name}-task-role"
120
+ name = local.task_role_safe_name
111
121
  assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
112
122
  permissions_boundary = local.preview_boundary_arn
113
123
  }
@@ -206,7 +216,7 @@ resource "aws_ecs_task_definition" "app" {
206
216
  # --- Application Load Balancer ---
207
217
  # trivy:ignore:AVD-AWS-0053 - This ALB is intended to be publicly facing behind CloudFront
208
218
  resource "aws_lb" "main" {
209
- name = substr("${local.app_name}-alb", 0, 32)
219
+ name = local.alb_safe_name
210
220
  load_balancer_type = "application"
211
221
  security_groups = [aws_security_group.alb.id]
212
222
  subnets = aws_subnet.public[*].id
@@ -214,7 +224,7 @@ resource "aws_lb" "main" {
214
224
  }
215
225
 
216
226
  resource "aws_lb_target_group" "app" {
217
- name = substr("${local.app_name}-tg", 0, 32)
227
+ name = local.tg_safe_name
218
228
  port = {{PORT}}
219
229
  protocol = "HTTP"
220
230
  vpc_id = aws_vpc.main.id
@@ -49,7 +49,9 @@ locals {
49
49
  # ${prod_prefix}-* pattern matches prod only. Anchoring also keeps
50
50
  # the boundary under the 6144-char managed-policy quota that broke
51
51
  # tier1 E2E at a 24-char prefix — prefer one anchored wildcard over
52
- # several exact ARNs when adding entries.
52
+ # several exact ARNs when adding entries. Headroom is now single
53
+ # digits at 32-char prefixes: the next IAM addition must economize
54
+ # (shorter Sids, fewer verbs, or slimmer patterns) before extending.
53
55
  guardrail_compute_abuse_resources = ["*"]
54
56
  guardrail_snapshot_sharing_resources = ["*"]
55
57
  guardrail_state_destruction_resources = [
@@ -110,6 +112,37 @@ locals {
110
112
  guardrail_app_role_resources = [
111
113
  "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${local.prod_prefix}-*-role",
112
114
  ]
115
+ # Data-plane confinement: prod data ARNs shared by the
116
+ # DenyProdAppData twins. One statement is exact here, not
117
+ # lazy — every denied action only ever matches its own service's
118
+ # ARNs in a real call, so the cross-product cannot over-match.
119
+ guardrail_app_data_resources = [
120
+ "arn:aws:secretsmanager:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:secret:${local.prod_prefix}-secrets-*",
121
+ "arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${local.prod_prefix}-*",
122
+ ]
123
+ # Lifecycle twin of the above, deliberately carve-free: no bounded
124
+ # role ever stops or reboots a database (sleep/wake and db commands
125
+ # run on user credentials), and one fewer condition keeps the pair
126
+ # under the 6144-char boundary quota.
127
+ guardrail_db_lifecycle_resources = [
128
+ "arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:db:${local.prod_prefix}-db*",
129
+ "arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster:${local.prod_prefix}-db-cluster",
130
+ ]
131
+ # Carve-out principal for the app-data twins: the exact prod task
132
+ # role ARN (workers send/receive prod queues). Singular on purpose:
133
+ # execution resolves taskdef secrets via GetSecretValue, which no
134
+ # statement here denies (plan refresh requires it — provider-proven),
135
+ # and nothing bounded writes secrets — so nothing else needs
136
+ # carving. Exact ARN, never a wildcard: a backdoor role could match
137
+ # a pattern by name since CreateRole sets its own trust inline.
138
+ # String-built, so static trees (no roles at all) stay valid. The
139
+ # scheduler is intentionally absent: RunTask plus PassRole only.
140
+ # Length-conditional like the role itself: over-cap trees hash the
141
+ # task name, and the carve-out must track the rendered name exactly
142
+ # (a stale plain ARN would silently uncarve workers).
143
+ guardrail_task_role_principals = [
144
+ length("${local.prod_prefix}-task-role") > 64 ? "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${substr(local.prod_prefix, 0, 64 - 1 - 5 - 10)}-${substr(md5(local.prod_prefix), 0, 5)}-task-role" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${local.prod_prefix}-task-role",
145
+ ]
113
146
  }
114
147
 
115
148
  # Deploy role FIRST: readTerraformOidcTrust anchors on this block so re-init
@@ -430,6 +463,43 @@ resource "aws_iam_role_policy" "preview_guardrails" {
430
463
  Action = ["iam:AttachRolePolicy", "iam:DetachRolePolicy", "iam:PutRolePolicy", "iam:DeleteRolePolicy", "iam:UpdateAssumeRolePolicy", "iam:DeleteRole"]
431
464
  Resource = local.guardrail_app_role_resources
432
465
  },
466
+ {
467
+ # Preview CI must never write prod secrets or touch prod queue
468
+ # traffic. GetSecretValue stays allowed: plan refreshes the
469
+ # version resource with preview credentials, and the provider
470
+ # reads it via GetSecretValue (provider source verified) — a
471
+ # deny would break every PR plan. Reads stay an accepted
472
+ # residual (see below), as does DeleteMessage, which needs a
473
+ # Receive handle to call: with Receive denied it is unreachable.
474
+ # The carve-out is mirrored here as a no-op (the preview role
475
+ # is never a task role) so the twins stay identical.
476
+ # Accepted residual: GetSecretValue + prod state reads (plan
477
+ # refresh requires both; state additionally exposes adopted
478
+ # live values), snapshot export/restore, Bedrock/SES spend
479
+ # verbs, DynamoDB data verbs, and RDS Start — follow-ups, not
480
+ # oversights.
481
+ Sid = "DenyProdAppData"
482
+ Effect = "Deny"
483
+ Action = [
484
+ "secretsmanager:Put*", "secretsmanager:Update*",
485
+ "sqs:Send*", "sqs:Receive*",
486
+ ]
487
+ Resource = local.guardrail_app_data_resources
488
+ Condition = {
489
+ StringNotEquals = {
490
+ "aws:PrincipalArn" = local.guardrail_task_role_principals
491
+ }
492
+ }
493
+ },
494
+ {
495
+ # ...nor stop or reboot prod databases. Deliberately
496
+ # carve-free: no bounded role ever calls these (sleep/wake
497
+ # and db commands run on user credentials).
498
+ Sid = "DenyProdDbLifecycle"
499
+ Effect = "Deny"
500
+ Action = ["rds:Stop*", "rds:Reboot*"]
501
+ Resource = local.guardrail_db_lifecycle_resources
502
+ },
433
503
  ]
434
504
  })
435
505
  }
@@ -584,6 +654,29 @@ resource "aws_iam_policy" "preview_boundary" {
584
654
  Action = ["iam:AttachRolePolicy", "iam:DetachRolePolicy", "iam:PutRolePolicy", "iam:DeleteRolePolicy", "iam:UpdateAssumeRolePolicy", "iam:DeleteRole"]
585
655
  Resource = local.guardrail_app_role_resources
586
656
  },
657
+ {
658
+ # Backdoor twin of DenyProdAppData: roles born under preview
659
+ # credentials get no prod vault writes or queue traffic either.
660
+ # The exact-ARN carve-out keeps workers sending/receiving.
661
+ Sid = "BoundaryDenyAppData"
662
+ Effect = "Deny"
663
+ Action = [
664
+ "secretsmanager:Put*", "secretsmanager:Update*",
665
+ "sqs:Send*", "sqs:Receive*",
666
+ ]
667
+ Resource = local.guardrail_app_data_resources
668
+ Condition = {
669
+ StringNotEquals = {
670
+ "aws:PrincipalArn" = local.guardrail_task_role_principals
671
+ }
672
+ }
673
+ },
674
+ {
675
+ Sid = "BoundaryDenyDbLifecycle"
676
+ Effect = "Deny"
677
+ Action = ["rds:Stop*", "rds:Reboot*"]
678
+ Resource = local.guardrail_db_lifecycle_resources
679
+ },
587
680
  {
588
681
  # Carved PassRole may only pass to the ECS tasks
589
682
  # service (mirrors the scheduler policy's own condition).