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.
@@ -1,6 +1,7 @@
1
1
  import { spawnSync } from 'child_process';
2
+ import path from 'path';
2
3
  import { ECSClient, DescribeServicesCommand } from '@aws-sdk/client-ecs';
3
- import { CloudWatchClient, DescribeAlarmsCommand } from '@aws-sdk/client-cloudwatch';
4
+ import { CloudWatchClient, DescribeAlarmsCommand, GetMetricDataCommand } from '@aws-sdk/client-cloudwatch';
4
5
  import { CloudFrontClient, ListDistributionsCommand } from '@aws-sdk/client-cloudfront';
5
6
  import color from 'picocolors';
6
7
  import { intro, outro, spinner } from '@clack/prompts';
@@ -8,8 +9,9 @@ import { trackEvent, flushTelemetry, trackSuccess, trackFailure, setActiveComman
8
9
  import { failCommand, failProjectNotInitialized, isProgrammaticCall, refuseUnknownFlags } from '../utils/command.js';
9
10
  import { parseFlags, normalizeOptions, normalizeArgv } from '../utils/args.js';
10
11
  import { handleAuthErrorBranch, resolveClient } from '../utils/aws.js';
11
- import { resolveRegion, resolveProjectName, resolveCwd, readTerraformComputeTarget, readResourceSuffix, resolveNamespacedProject } from '../utils/resolvers.js';
12
+ import { resolveRegion, resolveProjectName, resolveCwd, readTerraformComputeTarget, readResourceSuffix, resolveNamespacedProject, safeSuffixedName } from '../utils/resolvers.js';
12
13
  import { runDiagnose } from './diagnose.js';
14
+ import { detectAlertAddonFeatures } from './alerts.js';
13
15
  import { fetchGoldenSignals } from '../utils/golden-signals.js';
14
16
  import { requireManagedTree } from '../utils/terraform-metadata.js';
15
17
 
@@ -89,13 +91,15 @@ export function getLambdaHealth(configuration = {}) {
89
91
  return { state, lastUpdateStatus, healthy };
90
92
  }
91
93
 
92
- function printLambdaDashboard({ functionName, configuration, health }) {
94
+ function printLambdaDashboard({ functionName, configuration, health, cron }) {
93
95
  console.log('');
94
96
  console.log(` ${color.bold('Function:')} ${color.cyan(functionName)} ${health.state === 'Active' ? color.green('(ACTIVE)') : color.yellow(`(${health.state})`)}`);
95
97
  console.log(` ${color.bold('Last update:')} ${health.lastUpdateStatus === 'Successful' ? color.green(health.lastUpdateStatus) : color.yellow(health.lastUpdateStatus)}`);
96
98
  if (configuration.LastModified) console.log(` ${color.dim('Modified:')} ${configuration.LastModified}`);
97
99
  if (configuration.MemorySize) console.log(` ${color.dim('Memory:')} ${configuration.MemorySize} MB ${color.dim(`(timeout ${configuration.Timeout ?? '?'}s)`)}`);
98
100
  if (configuration.Code?.ImageUri) console.log(` ${color.dim('Image:')} ${configuration.Code.ImageUri.split('/').pop()}`);
101
+ const cronLine = formatCronHealthLine(cron);
102
+ if (cronLine) console.log(` ${color.bold('Cron:')} ${cronLine}`);
99
103
  console.log('');
100
104
  }
101
105
 
@@ -131,7 +135,7 @@ export async function runLambdaStatus(input = {}) {
131
135
  const namespacedProject = resolveNamespacedProject(projectName, options, cwd);
132
136
  const functionName = typeof options.functionName === 'string' && options.functionName.trim()
133
137
  ? options.functionName.trim()
134
- : `${namespacedProject}${readResourceSuffix(cwd)}-fn`;
138
+ : safeSuffixedName(`${namespacedProject}${readResourceSuffix(cwd)}`, '-fn', 64);
135
139
  const asJson = Boolean(options.json);
136
140
  const runSync = options.spawnSyncImpl || spawnSync;
137
141
 
@@ -212,6 +216,17 @@ export async function runLambdaStatus(input = {}) {
212
216
  }
213
217
 
214
218
  const health = getLambdaHealth(configuration);
219
+ // Cron run evidence, best-effort like the ECS path: detected from
220
+ // the tree, null when absent or unreachable.
221
+ let cron = null;
222
+ if (detectAlertAddonFeatures(path.join(cwd, 'terraform')).cron) {
223
+ try {
224
+ const cloudWatchClient = resolveClient(options.cloudWatchClient, CloudWatchClient, { region });
225
+ cron = await fetchCronHealth({ cloudWatchClient, scheduleGroup: safeSuffixedName(`${namespacedProject}${readResourceSuffix(cwd)}`, '-crons', 64) });
226
+ } catch {
227
+ cron = null;
228
+ }
229
+ }
215
230
  const payload = {
216
231
  function: {
217
232
  name: functionName,
@@ -224,6 +239,7 @@ export async function runLambdaStatus(input = {}) {
224
239
  },
225
240
  healthy: health.healthy,
226
241
  signals: null,
242
+ cron,
227
243
  region,
228
244
  computeTarget: 'lambda',
229
245
  };
@@ -235,7 +251,7 @@ export async function runLambdaStatus(input = {}) {
235
251
  }
236
252
 
237
253
  if (s) s.stop('Health check complete.\n');
238
- printLambdaDashboard({ functionName, configuration, health });
254
+ printLambdaDashboard({ functionName, configuration, health, cron });
239
255
 
240
256
  if (!health.healthy) {
241
257
  console.log(color.yellow(DEGRADED_MESSAGE));
@@ -389,7 +405,55 @@ function printStaticDashboard({ distribution }) {
389
405
  console.log('');
390
406
  }
391
407
 
392
- function printDashboard({ serviceName, cluster, health, signals }) {
408
+ // Cron run evidence from scheduler metrics (the Scheduler API exposes
409
+ // no execution history): latest attempt plus 24h failure sums. Throws
410
+ // naturally — callers treat it as best-effort like golden signals.
411
+ export async function fetchCronHealth({ cloudWatchClient, scheduleGroup }) {
412
+ const endTime = new Date();
413
+ const startTime = new Date(endTime.getTime() - 24 * 60 * 60 * 1000);
414
+ const queries = [
415
+ ['cronAttempts', 'InvocationAttemptCount'],
416
+ ['cronErrors', 'TargetErrorCount'],
417
+ ['cronDropped', 'InvocationDroppedCount'],
418
+ ].map(([Id, MetricName]) => ({
419
+ Id,
420
+ MetricStat: {
421
+ Metric: {
422
+ Namespace: 'AWS/Scheduler',
423
+ MetricName,
424
+ Dimensions: [{ Name: 'ScheduleGroup', Value: scheduleGroup }],
425
+ },
426
+ Period: 3600,
427
+ Stat: 'Sum',
428
+ },
429
+ }));
430
+ const resp = await cloudWatchClient.send(new GetMetricDataCommand({
431
+ StartTime: startTime,
432
+ EndTime: endTime,
433
+ ScanBy: 'TimestampDescending',
434
+ MetricDataQueries: queries,
435
+ }));
436
+ const byId = new Map((resp.MetricDataResults || []).map((r) => [r.Id, r]));
437
+ const attempts = byId.get('cronAttempts');
438
+ let lastAttemptAt = null;
439
+ if (attempts) {
440
+ const stamps = (attempts.Timestamps || [])
441
+ .map((t, i) => [new Date(t).getTime(), attempts.Values[i]])
442
+ .filter(([, value]) => value > 0)
443
+ .map(([time]) => time);
444
+ if (stamps.length > 0) lastAttemptAt = new Date(Math.max(...stamps)).toISOString();
445
+ }
446
+ const sum = (id) => (byId.get(id)?.Values || []).reduce((total, value) => total + value, 0);
447
+ return { lastAttemptAt, targetErrors24h: sum('cronErrors'), dropped24h: sum('cronDropped') };
448
+ }
449
+
450
+ function formatCronHealthLine(cron) {
451
+ if (!cron) return null;
452
+ if (!cron.lastAttemptAt) return 'no runs in the last 24h';
453
+ return `last run ${cron.lastAttemptAt} · ${cron.targetErrors24h} target errors · ${cron.dropped24h} dropped (24h)`;
454
+ }
455
+
456
+ function printDashboard({ serviceName, cluster, health, signals, cron }) {
393
457
  console.log('');
394
458
  console.log(` ${color.bold('Service:')} ${color.cyan(serviceName)} ${health.status === 'ACTIVE' ? color.green('(ACTIVE)') : color.yellow(`(${health.status})`)}`);
395
459
  console.log(` ${color.dim('Cluster:')} ${cluster}`);
@@ -412,6 +476,8 @@ function printDashboard({ serviceName, cluster, health, signals }) {
412
476
  if (db?.kind === 'cluster') console.log(` ${color.dim('DB:')} ${db.capacityAcu ?? 'n/a'} ACU · ${formatSignalCount(db.connections)} conns`);
413
477
  else if (db) console.log(` ${color.dim('DB:')} CPU ${formatSignalPercent(db.cpuPercent)} · ${formatSignalCount(db.connections)} conns`);
414
478
  }
479
+ const cronLine = formatCronHealthLine(cron);
480
+ if (cronLine) console.log(` ${color.bold('Cron:')} ${cronLine}`);
415
481
  console.log('');
416
482
  }
417
483
 
@@ -529,6 +595,17 @@ async function runStatusMain(input = {}) {
529
595
  signalsError = [`signals: ${error?.message || error}`];
530
596
  }
531
597
 
598
+ // Cron run evidence, same best-effort deal: detected from the
599
+ // tree (not the target), null when absent or unreachable.
600
+ let cron = null;
601
+ if (detectAlertAddonFeatures(path.join(cwd, 'terraform')).cron) {
602
+ try {
603
+ cron = await fetchCronHealth({ cloudWatchClient, scheduleGroup: safeSuffixedName(`${namespacedProject}${resourceSuffix}`, '-crons', 64) });
604
+ } catch {
605
+ cron = null;
606
+ }
607
+ }
608
+
532
609
  const payload = {
533
610
  service: {
534
611
  name: serviceName,
@@ -541,6 +618,7 @@ async function runStatusMain(input = {}) {
541
618
  alarms: health.alarms,
542
619
  signals,
543
620
  ...(signalsError ? { signalsError } : {}),
621
+ cron,
544
622
  healthy: health.healthy,
545
623
  region,
546
624
  };
@@ -552,7 +630,7 @@ async function runStatusMain(input = {}) {
552
630
  }
553
631
 
554
632
  if (s) s.stop('Health check complete.\n');
555
- printDashboard({ serviceName, cluster, health, signals });
633
+ printDashboard({ serviceName, cluster, health, signals, cron });
556
634
 
557
635
  if (!health.healthy) {
558
636
  console.log(color.yellow(DEGRADED_MESSAGE));
@@ -12,7 +12,7 @@ export const ACTION_PINS = {
12
12
  'aquasecurity/trivy-action': { sha: 'ed142fd0673e97e23eac54620cfb913e5ce36c25', tag: 'v0.36.0' },
13
13
  'aws-actions/amazon-ecr-login': { sha: '03f1aad4c6c7ffd436567f42f9384779290529bd', tag: 'v2' },
14
14
  'aws-actions/configure-aws-credentials': { sha: '7474bc4690e29a8392af63c5b98e7449536d5c3a', tag: 'v4' },
15
- 'grada-run/grada-action': { sha: '12227808ae4caafaec1041319645586a1132fc57', tag: 'v1' },
15
+ 'grada-run/grada-action': { sha: '9df2e2c12314b6165e62f0e615edb01d6a81c40b', tag: 'v1' },
16
16
  'hashicorp/setup-terraform': { sha: 'b9cd54a3c349d3f38e8881555d616ced269862dd', tag: 'v3' },
17
17
  };
18
18
 
@@ -140,6 +140,19 @@ export function injectLambdaAdapter(dockerContent, port) {
140
140
  // master-secret IAM policy (indexing an empty `master_user_secret` tuple fails
141
141
  // planning) and appends the `random_password` resource. No-op for ECS content
142
142
  // or content that was already converted.
143
+ const SAFE_NAME_KEYS = ['identifier_safe_name', 'db_name_base', 'db_name_safe_name', 'cluster_safe_name', 'instance_safe_name', 'database_name_base', 'database_name_safe_name'];
144
+
145
+ function realignSurvivingSafeNames(content) {
146
+ const present = SAFE_NAME_KEYS.filter((key) => new RegExp(`^\\s*${key}\\s*=`, 'm').test(content));
147
+ if (present.length === 0) return content;
148
+ const width = Math.max(...present.map((key) => key.length));
149
+ let out = content;
150
+ for (const key of present) {
151
+ out = out.replace(new RegExp(`^(\\s*)${key}\\s*=`, 'm'), (_, indent) => `${indent}${key}${' '.repeat(width - key.length + 1)}=`);
152
+ }
153
+ return out;
154
+ }
155
+
143
156
  export function convertDatabaseTfForLambda(databaseTfContent) {
144
157
  let content = String(databaseTfContent ?? '');
145
158
  if (!content.includes('manage_master_user_password = true')) return content;
@@ -151,6 +164,12 @@ export function convertDatabaseTfForLambda(databaseTfContent) {
151
164
  ` # Lambda targets use an explicit Terraform-managed password (see\n # random_password.db_password below) so the value can flow into the\n # function's DB_PASSWORD environment variable without a Secrets Manager\n # VPC endpoint.\n ${passwordAttr} = random_password.db_password.result`
152
165
  );
153
166
  content = content.replace('# 4. IAM Permission for RDS Master Password Secret\n', '');
167
+ // The policy's safe-name local dangles once the block below is gone —
168
+ // strip the line too or tflint flags an unused declaration. It was
169
+ // the longest key in its run, so re-pad the survivors to the new
170
+ // longest or the rendered file fails fmt -check.
171
+ content = content.replace(/^\s*secret_policy_safe_name\s*=.*\n/m, '');
172
+ content = realignSurvivingSafeNames(content);
154
173
  const resource = findResourceBlock(content, 'aws_iam_role_policy', 'rds_secret_access');
155
174
  if (resource) {
156
175
  const lineStart = content.lastIndexOf('\n', resource.headerIdx - 1) + 1;
@@ -4,7 +4,7 @@ import { RDSClient } from '@aws-sdk/client-rds';
4
4
  import { resolveClient } from './aws.js';
5
5
  import { normalizeOptions } from './args.js';
6
6
  import { findDbTarget, resolveDbIdentifier, resolveDbClusterIdentifier } from './rds.js';
7
- import { readResourceSuffix } from './resolvers.js';
7
+ import { readResourceSuffix, safeSuffixedName } from './resolvers.js';
8
8
 
9
9
  // Golden Signals: live CloudWatch telemetry for `grada status`.
10
10
  //
@@ -21,10 +21,10 @@ import { readResourceSuffix } from './resolvers.js';
21
21
  export const GOLDEN_SIGNALS_WINDOW_MINUTES = 15;
22
22
  export const GOLDEN_SIGNALS_PERIOD_SECONDS = 300;
23
23
 
24
- // Mirrors the generated ALB name (`substr("${app}-alb", 0, 32)` in
25
- // templates/terraform/main.tf) so lookup matches the truncated name.
24
+ // Mirrors the generated ALB name (the length-conditional in
25
+ // templates/terraform/main.tf) so lookup matches exactly, hashed or not.
26
26
  export function resolveAlbName(projectName) {
27
- return `${projectName}-alb`.slice(0, 32);
27
+ return safeSuffixedName(projectName, '-alb', 32);
28
28
  }
29
29
 
30
30
  // `arn:aws:elasticloadbalancing:<region>:<acct>:loadbalancer/app/<name>/<id>`
@@ -25,6 +25,12 @@ function resolveActualProjectName(rawName) {
25
25
  if (actualProjectName !== String(rawName ?? '').trim()) {
26
26
  console.log(color.dim(`Project name sanitized to "${actualProjectName}" for AWS resource compatibility.`));
27
27
  }
28
+ // Downstream truncation is silent (CI role heads keep ~29 chars,
29
+ // the state bucket ~42) with uniqueness preserved via the
30
+ // resource suffix — say so once at decision time instead.
31
+ if (actualProjectName.length > 29) {
32
+ console.log(color.yellow(`⚠️ "${actualProjectName}" is ${actualProjectName.length} chars: CI role names truncate to the first ~29 and the state bucket to the first ~42 (uniqueness preserved via the suffix). Shorter names stay fully readable.`));
33
+ }
28
34
  return actualProjectName;
29
35
  }
30
36
 
package/src/utils/rds.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DescribeDBInstancesCommand, DescribeDBClustersCommand } from '@aws-sdk/client-rds';
2
- import { resolveProjectName, resolveWorkspaceSuffix, readResourceSuffix } from './resolvers.js';
2
+ import { resolveProjectName, resolveWorkspaceSuffix, readResourceSuffix, safeSuffixedName } from './resolvers.js';
3
3
  import { normalizeOptions } from './args.js';
4
4
 
5
5
  // Error names the RDS API returns for a missing instance across SDK shapes.
@@ -118,24 +118,26 @@ export function isValidSnapshotId(id) {
118
118
  }
119
119
 
120
120
  // Database identifier for the current project/workspace. An explicit
121
- // `--db-identifier` override wins; otherwise `<appName>-db`.
121
+ // `--db-identifier` override wins; otherwise `<appName>-db`, hash-suffixed
122
+ // past the 63-char cap exactly like identifier_safe_name in database.tf.
122
123
  export function resolveDbIdentifier(options = {}, cwd = process.cwd()) {
123
124
  const opts = normalizeOptions(options);
124
125
  if (typeof opts.dbIdentifier === 'string' && opts.dbIdentifier.trim()) {
125
126
  return opts.dbIdentifier.trim();
126
127
  }
127
128
  const base = resolveProjectName(opts, cwd);
128
- return `${base}${resolveWorkspaceSuffix(opts, cwd)}${readResourceSuffix(cwd)}-db`;
129
+ return safeSuffixedName(`${base}${resolveWorkspaceSuffix(opts, cwd)}${readResourceSuffix(cwd)}`, '-db', 63);
129
130
  }
130
131
 
131
132
  // Aurora cluster identifier for the current project/workspace. An explicit
132
133
  // `--db-identifier` override wins (tried as both kinds); otherwise
133
- // `<appName>-db-cluster`, matching the generated cluster_identifier.
134
+ // `<appName>-db-cluster`, hash-suffixed past the 63-char cap exactly
135
+ // like cluster_safe_name in database-aurora-postgresql.tf.
134
136
  export function resolveDbClusterIdentifier(options = {}, cwd = process.cwd()) {
135
137
  const opts = normalizeOptions(options);
136
138
  if (typeof opts.dbIdentifier === 'string' && opts.dbIdentifier.trim()) {
137
139
  return opts.dbIdentifier.trim();
138
140
  }
139
141
  const base = resolveProjectName(opts, cwd);
140
- return `${base}${resolveWorkspaceSuffix(opts, cwd)}${readResourceSuffix(cwd)}-db-cluster`;
142
+ return safeSuffixedName(`${base}${resolveWorkspaceSuffix(opts, cwd)}${readResourceSuffix(cwd)}`, '-db-cluster', 63);
141
143
  }
@@ -1,6 +1,6 @@
1
1
  import fsSync from 'fs';
2
2
  import path from 'path';
3
- import { randomBytes } from 'crypto';
3
+ import { randomBytes, createHash } from 'crypto';
4
4
  import { normalizeOptions } from './args.js';
5
5
 
6
6
  export const FALLBACK_REGION = 'us-east-2';
@@ -112,6 +112,21 @@ export function readTerraformRegion(cwd = process.cwd()) {
112
112
  return match[1];
113
113
  }
114
114
 
115
+ // Length-safe AWS resource name, mirroring the template conditional
116
+ // byte-for-byte: short names render identically; over-cap names take a
117
+ // deterministic md5 stem (never a truncating collision). `cap` is the
118
+ // service limit including suffix. Doubled hyphens at the cut point are
119
+ // left alone — valid in every name pattern here, and trimming would
120
+ // need identical semantics on both sides. Both sides must stay in
121
+ // sync — the parity tests below pin the shared cases.
122
+ export function safeSuffixedName(base, suffix, cap) {
123
+ const full = `${base}${suffix}`;
124
+ if (full.length <= cap) return full;
125
+ const stemLength = Math.max(cap - 1 - 5 - suffix.length, 1);
126
+ const stem = base.slice(0, stemLength);
127
+ return `${stem}-${createHash('md5').update(base, 'utf8').digest('hex').slice(0, 5)}${suffix}`;
128
+ }
129
+
115
130
  export function resolveRegion(options = {}, cwd = process.cwd()) {
116
131
  const opts = normalizeOptions(options);
117
132
  if (typeof opts.region === 'string' && opts.region.trim()) {
@@ -317,9 +332,9 @@ export function resolveLogGroup(options = {}, cwd = process.cwd()) {
317
332
  const projectName = resolveProjectName(opts, base);
318
333
  const suffixed = `${projectName}${readResourceSuffix(base)}`;
319
334
  // Lambda functions log to /aws/lambda/<fn> (see main-lambda.tf), where
320
- // the function name is `<app>-fn`.
335
+ // the function name is `<app>-fn`, hash-suffixed past 64 chars.
321
336
  if (readTerraformComputeTarget(base) === 'lambda') {
322
- return `/aws/lambda/${suffixed}-fn`;
337
+ return `/aws/lambda/${safeSuffixedName(suffixed, '-fn', 64)}`;
323
338
  }
324
339
  return `/ecs/${suffixed}`;
325
340
  }
@@ -141,7 +141,7 @@ jobs:
141
141
  # Runs AFTER the push: the function image tags exist by the time the
142
142
  # function is created or updated.
143
143
  - name: Apply Infrastructure Changes
144
- uses: grada-run/grada-action@12227808ae4caafaec1041319645586a1132fc57 # v1
144
+ uses: grada-run/grada-action@9df2e2c12314b6165e62f0e615edb01d6a81c40b # v1
145
145
  with:
146
146
  aws-region: ${{ env.AWS_REGION }}
147
147
  role-to-assume: arn:aws:iam::{{AWS_ACCOUNT_ID}}:role/{{DEPLOY_ROLE_NAME}}
@@ -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}}
@@ -144,7 +144,7 @@ jobs:
144
144
  # the tag exists. A gate failure below can therefore never strand
145
145
  # the service on an unpullable revision.
146
146
  - name: Apply Infrastructure Changes
147
- uses: grada-run/grada-action@12227808ae4caafaec1041319645586a1132fc57 # v1
147
+ uses: grada-run/grada-action@9df2e2c12314b6165e62f0e615edb01d6a81c40b # v1
148
148
  with:
149
149
  aws-region: ${{ env.AWS_REGION }}
150
150
  role-to-assume: arn:aws:iam::{{AWS_ACCOUNT_ID}}:role/{{DEPLOY_ROLE_NAME}}
@@ -195,3 +195,71 @@ jobs:
195
195
  --arg summary "$SUMMARY" \
196
196
  '{text: (":warning: Infrastructure drift detected in " + $repo + "\n" + $url + "\n```" + $summary + "```")}' > /tmp/drift-slack.json
197
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,11 @@ 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
27
+ PREVIEW_WORKER_SERVICE: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-worker-service
28
+ PREVIEW_WORKER_TASK_FAMILY: {{PROJECT_NAME}}-pr-${{ github.event.pull_request.number }}{{RESOURCE_SUFFIX}}-worker-task
24
29
 
25
30
  permissions:
26
31
  id-token: write
@@ -99,6 +104,132 @@ jobs:
99
104
  PREVIEW_URL=$(terraform output -raw cloudfront_url)
100
105
  echo "PREVIEW_URL=$PREVIEW_URL" >> $GITHUB_ENV
101
106
 
107
+ # The image tag is static per PR (:pr-N), so a re-push changes no
108
+ # Terraform input — without these steps the service keeps running
109
+ # the first push's image. Mirror the deploy workflow: register a
110
+ # digest-pinned revision (immutable, so concurrent pushes can't
111
+ # swap the image mid-rollout), force the rollout, and verify it
112
+ # stuck.
113
+ - name: Register preview task definition revision
114
+ id: register-preview-task-def
115
+ if: steps.aws-creds.outcome == 'success'
116
+ env:
117
+ ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
118
+ run: |
119
+ DIGEST=$(aws ecr describe-images \
120
+ --repository-name ${{ env.ECR_REPOSITORY }} \
121
+ --image-ids imageTag=${{ env.WORKSPACE }} \
122
+ --query 'imageDetails[0].imageDigest' \
123
+ --output text \
124
+ --region ${{ env.AWS_REGION }})
125
+ IMAGE_URI=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}@${DIGEST}
126
+ TASK_DEF=$(aws ecs describe-task-definition \
127
+ --task-definition ${{ env.PREVIEW_TASK_FAMILY }} \
128
+ --region ${{ env.AWS_REGION }} \
129
+ --query 'taskDefinition' \
130
+ --output json)
131
+ NEW_TASK_DEF=$(echo "$TASK_DEF" | jq \
132
+ --arg IMAGE "$IMAGE_URI" \
133
+ '.containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt)')
134
+ NEW_TASK_ARN=$(aws ecs register-task-definition \
135
+ --region ${{ env.AWS_REGION }} \
136
+ --cli-input-json "$NEW_TASK_DEF" \
137
+ --query 'taskDefinition.taskDefinitionArn' \
138
+ --output text)
139
+ echo "task-arn=$NEW_TASK_ARN" >> $GITHUB_OUTPUT
140
+
141
+ - name: Force preview deployment
142
+ if: steps.aws-creds.outcome == 'success'
143
+ run: |
144
+ aws ecs update-service \
145
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
146
+ --service ${{ env.PREVIEW_SERVICE }} \
147
+ --task-definition ${{ steps.register-preview-task-def.outputs.task-arn }} \
148
+ --force-new-deployment \
149
+ --region ${{ env.AWS_REGION }}
150
+
151
+ # update-service returns before the rollout finishes: wait for
152
+ # stability, then verify PRIMARY actually runs the registered
153
+ # revision — a circuit-breaker rollback re-stabilizes on the OLD
154
+ # revision, which the wait alone would report green.
155
+ - name: Wait for preview rollout
156
+ if: steps.aws-creds.outcome == 'success'
157
+ run: |
158
+ aws ecs wait services-stable \
159
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
160
+ --services ${{ env.PREVIEW_SERVICE }} \
161
+ --region ${{ env.AWS_REGION }}
162
+ PRIMARY_TD=$(aws ecs describe-services \
163
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
164
+ --services ${{ env.PREVIEW_SERVICE }} \
165
+ --query 'services[0].deployments[?status==`PRIMARY`].taskDefinition' \
166
+ --output text \
167
+ --region ${{ env.AWS_REGION }})
168
+ if [ "$PRIMARY_TD" != "${{ steps.register-preview-task-def.outputs.task-arn }}" ]; then
169
+ 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
170
+ exit 1
171
+ fi
172
+
173
+ # The worker ships the same static :pr-N image as web, so it gets
174
+ # the same digest-pinned treatment — otherwise it would run the
175
+ # first push's code forever while web moves on. Skipped on
176
+ # workerless projects (deploy-workflow guard).
177
+ - name: Register preview worker task definition revision
178
+ id: register-preview-worker-task-def
179
+ if: hashFiles('terraform/worker.tf') != '' && steps.aws-creds.outcome == 'success'
180
+ env:
181
+ ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
182
+ run: |
183
+ DIGEST=$(aws ecr describe-images \
184
+ --repository-name ${{ env.ECR_REPOSITORY }} \
185
+ --image-ids imageTag=${{ env.WORKSPACE }} \
186
+ --query 'imageDetails[0].imageDigest' \
187
+ --output text \
188
+ --region ${{ env.AWS_REGION }})
189
+ IMAGE_URI=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}@${DIGEST}
190
+ TASK_DEF=$(aws ecs describe-task-definition \
191
+ --task-definition ${{ env.PREVIEW_WORKER_TASK_FAMILY }} \
192
+ --region ${{ env.AWS_REGION }} \
193
+ --query 'taskDefinition' \
194
+ --output json)
195
+ NEW_TASK_DEF=$(echo "$TASK_DEF" | jq \
196
+ --arg IMAGE "$IMAGE_URI" \
197
+ '.containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .compatibilities, .registeredAt, .registeredBy, .deregisteredAt)')
198
+ NEW_TASK_ARN=$(aws ecs register-task-definition \
199
+ --region ${{ env.AWS_REGION }} \
200
+ --cli-input-json "$NEW_TASK_DEF" \
201
+ --query 'taskDefinition.taskDefinitionArn' \
202
+ --output text)
203
+ echo "task-arn=$NEW_TASK_ARN" >> $GITHUB_OUTPUT
204
+
205
+ - name: Force preview worker deployment
206
+ if: hashFiles('terraform/worker.tf') != '' && steps.aws-creds.outcome == 'success'
207
+ run: |
208
+ aws ecs update-service \
209
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
210
+ --service ${{ env.PREVIEW_WORKER_SERVICE }} \
211
+ --task-definition ${{ steps.register-preview-worker-task-def.outputs.task-arn }} \
212
+ --force-new-deployment \
213
+ --region ${{ env.AWS_REGION }}
214
+
215
+ - name: Wait for preview worker rollout
216
+ if: hashFiles('terraform/worker.tf') != '' && steps.aws-creds.outcome == 'success'
217
+ run: |
218
+ aws ecs wait services-stable \
219
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
220
+ --services ${{ env.PREVIEW_WORKER_SERVICE }} \
221
+ --region ${{ env.AWS_REGION }}
222
+ PRIMARY_TD=$(aws ecs describe-services \
223
+ --cluster ${{ env.PREVIEW_CLUSTER }} \
224
+ --services ${{ env.PREVIEW_WORKER_SERVICE }} \
225
+ --query 'services[0].deployments[?status==`PRIMARY`].taskDefinition' \
226
+ --output text \
227
+ --region ${{ env.AWS_REGION }})
228
+ if [ "$PRIMARY_TD" != "${{ steps.register-preview-worker-task-def.outputs.task-arn }}" ]; then
229
+ echo "Preview worker rollout of ${{ steps.register-preview-worker-task-def.outputs.task-arn }} did not stick (PRIMARY runs $PRIMARY_TD) — the deployment failed or rolled back." >&2
230
+ exit 1
231
+ fi
232
+
102
233
  # Every push redeploys the preview: clear its CDN so new HTML shows
103
234
  # immediately instead of serving stale pages until TTL expiry.
104
235
  - 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
  ]