@elumixor/devkit 0.12.5 → 0.12.6

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/README.md CHANGED
@@ -276,10 +276,28 @@ devkit cloud status # what is live, with calls, errors and p95 for
276
276
  devkit cloud logs --follow # CloudWatch logs for the stage
277
277
  devkit cloud deploys <project> # the last ten deploys
278
278
  devkit cloud rollback <project> # back to the previous deploy, in seconds
279
+ devkit cloud infra apply # Terraform in infra/, mapped outputs into each stage's env
279
280
  devkit cloud domains add <project> <host> # cert, alias, Cloudflare DNS, route; later deploys keep it
280
281
  devkit cloud destroy <project> --stage development
281
282
  ```
282
283
 
284
+ Databases and anything else the platform doesn't make itself stay declared in Terraform. Point the
285
+ target at the module and name which outputs become which env vars per stage:
286
+
287
+ ```json
288
+ "terraform": {
289
+ "dir": "infra",
290
+ "env": {
291
+ "production": { "DATABASE_URL": "production_database_url" },
292
+ "development": { "DATABASE_URL": "develop_database_url" }
293
+ }
294
+ }
295
+ ```
296
+
297
+ `devkit cloud infra plan` shows the changes, `apply` makes them and copies the outputs into SSM,
298
+ `sync` only copies. The next deploy reads them like any other env var; CI needs no Terraform
299
+ credentials. Resources the function itself must call (a queue, a table) also need `permissions`.
300
+
283
301
  Env vars live in SSM at `/dk/<project>/<stage>/<KEY>` and are copied into the function at deploy,
284
302
  and into the build's environment, so `VITE_*` values reach the frontend in CI too. In the function,
285
303
  `import { waitUntil } from "@elumixor/devkit/runtime"` keeps the invocation alive after the response
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elumixor/devkit",
3
- "version": "0.12.5",
3
+ "version": "0.12.6",
4
4
  "description": "Dev orchestrator for bun monorepos — free ports, run apps side-by-side, open the browser when ready",
5
5
  "author": "elumixor",
6
6
  "license": "ISC",
package/src/bin/devkit.ts CHANGED
@@ -31,6 +31,7 @@ const { values, positionals } = parseArgs({
31
31
  follow: { type: "boolean", default: false },
32
32
  to: { type: "string" },
33
33
  email: { type: "string" },
34
+ yes: { type: "boolean", short: "y", default: false },
34
35
  version: { type: "boolean", short: "v", default: false },
35
36
  help: { type: "boolean", short: "h", default: false },
36
37
  },
@@ -118,6 +119,7 @@ try {
118
119
  follow: values.follow,
119
120
  to: values.to,
120
121
  email: values.email,
122
+ yes: values.yes,
121
123
  });
122
124
  break;
123
125
  }
package/src/cloud/cli.ts CHANGED
@@ -2,6 +2,7 @@ import { bootstrap } from "./bootstrap.ts";
2
2
  import { destroy } from "./commands/destroy.ts";
3
3
  import { domains } from "./commands/domains.ts";
4
4
  import { env } from "./commands/env.ts";
5
+ import { infra } from "./commands/infra.ts";
5
6
  import { init } from "./commands/init.ts";
6
7
  import { logs } from "./commands/logs.ts";
7
8
  import { rollback } from "./commands/rollback.ts";
@@ -14,6 +15,7 @@ export interface CloudFlags {
14
15
  follow?: boolean;
15
16
  to?: string;
16
17
  email?: string;
18
+ yes?: boolean;
17
19
  }
18
20
 
19
21
  export const CLOUD_USAGE = `usage: devkit cloud bootstrap [--email x] once per account: buckets, edge, roles, budgets
@@ -23,6 +25,7 @@ export const CLOUD_USAGE = `usage: devkit cloud bootstrap [--email x] once
23
25
  devkit cloud logs [project] [--stage s] [--since 1h] [--grep x] [--follow]
24
26
  devkit cloud rollback <project> [--stage s] [--to <deployId>]
25
27
  devkit cloud env ls|pull|push|set|rm [--stage s]
28
+ devkit cloud infra plan|apply|sync [--yes] Terraform resources, outputs into stage env
26
29
  devkit cloud domains ls|add|rm <project> [host] [--stage s]
27
30
  devkit cloud destroy <project> --stage s`;
28
31
 
@@ -46,6 +49,8 @@ export function runCloud(args: readonly string[], flags: CloudFlags) {
46
49
  return rollback(rest[0], flags.stage, flags.to);
47
50
  case "env":
48
51
  return env(rest[0], rest.slice(1), flags.stage);
52
+ case "infra":
53
+ return infra(rest[0], { yes: flags.yes });
49
54
  case "domains":
50
55
  return domains(rest[0], rest[1], rest[2], flags.stage);
51
56
  case "destroy":
@@ -1,7 +1,7 @@
1
1
  import { bold, dim } from "../../ansi.ts";
2
2
  import { CloudError, log } from "../aws.ts";
3
3
  import { deleteRecord } from "../reconcile/cloudflare.ts";
4
- import { needsDns, reconcileAliases } from "../reconcile/edge.ts";
4
+ import { needsDns, pointDns, reconcileAliases } from "../reconcile/edge.ts";
5
5
  import { deleteRoute, getRoute, putRoutes } from "../reconcile/kvs.ts";
6
6
  import { readRegistry, updateRegistryHostname } from "../records.ts";
7
7
  import { targetByName } from "./context.ts";
@@ -23,8 +23,9 @@ export async function addDomain(project: string, host: string, stageFlag?: strin
23
23
  const { target, recorded } = await targetByName(project, stageFlag);
24
24
  const route = await getRoute(recorded.hostnames[0] as string);
25
25
  if (!route) throw new CloudError(`No route record for ${recorded.hostnames[0]} — deploy once first`);
26
- await reconcileAliases([host]);
26
+ const { changed } = await reconcileAliases([host]);
27
27
  await putRoutes({ [host]: route });
28
+ await pointDns([host], changed);
28
29
  await updateRegistryHostname(target, host, true);
29
30
  return `https://${host} now serves ${project} ${target.stage} (new hostnames reach every edge within ~1 min)`;
30
31
  }
@@ -0,0 +1,54 @@
1
+ import { join } from "node:path";
2
+ import { $ } from "bun";
3
+ import { CloudError, log } from "../aws.ts";
4
+ import { writeStageEnv } from "../env.ts";
5
+ import { projectName, Target } from "../project.ts";
6
+ import { awsOptions } from "./context.ts";
7
+
8
+ const USAGE = "usage: devkit cloud infra plan|apply|sync [--yes]";
9
+
10
+ interface TerraformOutput {
11
+ value: unknown;
12
+ sensitive: boolean;
13
+ }
14
+
15
+ /**
16
+ * `devkit cloud infra`: the project's Terraform module. `plan` shows changes, `apply` makes them and
17
+ * then syncs, `sync` only copies the configured outputs into each stage's env (SSM).
18
+ */
19
+ export async function infra(action: string | undefined, flags: { yes?: boolean }, root = process.cwd()) {
20
+ const options = awsOptions(root);
21
+ const config = options.terraform;
22
+ if (!config) throw new CloudError('No "terraform" in the aws target options — add { dir, env } first');
23
+ const dir = join(root, config.dir ?? "infra");
24
+ const terraform = (...args: string[]) => run(["terraform", `-chdir=${dir}`, ...args]);
25
+
26
+ if (action !== "plan" && action !== "apply" && action !== "sync") throw new CloudError(USAGE);
27
+ await terraform("init", "-input=false");
28
+ if (action === "plan") return terraform("plan", "-input=false");
29
+ if (action === "apply") await terraform("apply", "-input=false", ...(flags.yes ? ["-auto-approve"] : []));
30
+
31
+ const outputs = JSON.parse(await $`terraform -chdir=${dir} output -json`.quiet().text()) as Record<
32
+ string,
33
+ TerraformOutput
34
+ >;
35
+ const project = await projectName(options, root);
36
+ for (const [stage, mapping] of Object.entries(config.env)) {
37
+ const env = Object.fromEntries(
38
+ Object.entries(mapping).map(([key, output]) => {
39
+ const found = outputs[output];
40
+ if (!found) throw new CloudError(`Terraform has no output "${output}" (wanted for ${stage} ${key})`);
41
+ return [key, typeof found.value === "string" ? found.value : JSON.stringify(found.value)];
42
+ }),
43
+ );
44
+ await writeStageEnv(new Target(project, stage, options), env);
45
+ log(`${project} ${stage}: ${Object.keys(env).join(", ")} ← Terraform (next deploy picks them up)`);
46
+ }
47
+ }
48
+
49
+ /** Terraform asks for approval on stdin, so it gets the terminal. */
50
+ async function run(command: string[]) {
51
+ const child = Bun.spawn(command, { stdio: ["inherit", "inherit", "inherit"] });
52
+ const code = await child.exited;
53
+ if (code !== 0) throw new CloudError(`${command.slice(0, 2).join(" ")} ${command[2]} exited with ${code}`);
54
+ }
@@ -43,10 +43,24 @@ export interface AwsTargetOptions extends Omit<WebBuildOptions, "host"> {
43
43
  healthPath?: string;
44
44
  /** Stage name to git branch (default production=main, development=develop). */
45
45
  stages?: Record<string, string>;
46
+ /** Databases and other resources declared in Terraform, their outputs fed into the stage env. */
47
+ terraform?: TerraformConfig;
46
48
  /** Extra IAM statements for the function — an app that reads other AWS resources, like the dashboard. */
47
49
  permissions?: Permission[];
48
50
  }
49
51
 
52
+ /**
53
+ * Resources the platform doesn't make itself (a Neon database, a queue, a DNS record) live in a
54
+ * Terraform root module. `devkit cloud infra apply` applies it and copies the named outputs into
55
+ * each stage's env, where the next deploy picks them up.
56
+ */
57
+ export interface TerraformConfig {
58
+ /** The root module, relative to the repo (default `infra`). */
59
+ dir?: string;
60
+ /** Stage → env var → Terraform output name, e.g. `{ production: { DATABASE_URL: "production_database_url" } }`. */
61
+ env: Record<string, Record<string, string>>;
62
+ }
63
+
50
64
  export const DEFAULTS = { memory: 512, timeout: 120, routing: "spa" as Routing, healthPath: "/api/health" };
51
65
 
52
66
  /** A project and stage with every default filled in — what the reconciler and the commands work from. */
@@ -77,21 +77,34 @@ function zoneFor(hostname: string) {
77
77
  */
78
78
  export async function ensureRecord(name: string, type: "CNAME" | "TXT", content: string) {
79
79
  const zone = await zoneFor(name);
80
- const found = await call<CloudflareRecord[]>(
81
- "GET",
82
- `/zones/${zone}/dns_records?type=${type}&name=${encodeURIComponent(name)}`,
83
- );
84
- const existing = found[0];
80
+ const existing = await findRecord(zone, name, type);
85
81
  const body = { type, name, content, proxied: false, ttl: 300, comment: "devkit cloud" };
86
82
  if (!existing) {
87
83
  await call("POST", `/zones/${zone}/dns_records`, body);
88
84
  return "created";
89
85
  }
90
- if (existing.content.replace(/\.$/, "") === content.replace(/\.$/, "") && !existing.proxied) return "unchanged";
86
+ if (matches(existing, content)) return "unchanged";
91
87
  await call("PUT", `/zones/${zone}/dns_records/${existing.id}`, body);
92
88
  return "updated";
93
89
  }
94
90
 
91
+ async function findRecord(zone: string, name: string, type: string) {
92
+ const found = await call<CloudflareRecord[]>(
93
+ "GET",
94
+ `/zones/${zone}/dns_records?type=${type}&name=${encodeURIComponent(name)}`,
95
+ );
96
+ return found[0];
97
+ }
98
+
99
+ const matches = (record: CloudflareRecord, content: string) =>
100
+ record.content.replace(/\.$/, "") === content.replace(/\.$/, "") && !record.proxied;
101
+
102
+ /** Whether the record already exists with this content, unproxied. */
103
+ export async function recordPointsAt(name: string, type: "CNAME" | "TXT", content: string) {
104
+ const existing = await findRecord(await zoneFor(name), name, type);
105
+ return existing !== undefined && matches(existing, content);
106
+ }
107
+
95
108
  export async function deleteRecord(name: string, type: "CNAME" | "TXT") {
96
109
  const zone = await zoneFor(name);
97
110
  const found = await call<CloudflareRecord[]>(
@@ -8,7 +8,7 @@ import {
8
8
  import { CloudError, cloudfront, log, waitFor } from "../aws.ts";
9
9
  import { PLATFORM_ALIASES, WILDCARD_DOMAIN } from "../names.ts";
10
10
  import { deleteUnusedCertificates, ensureCertificate } from "./acm.ts";
11
- import { ensureRecord } from "./cloudflare.ts";
11
+ import { ensureRecord, recordPointsAt } from "./cloudflare.ts";
12
12
 
13
13
  /** How the one distribution is recognised among any others in the account. */
14
14
  export const DISTRIBUTION_COMMENT = "dk-edge";
@@ -52,7 +52,7 @@ export const needsDns = (hostname: string) => !isWildcard(hostname) && !covers(W
52
52
 
53
53
  /**
54
54
  * Adds hostnames as aliases, swapping in a certificate that covers them all when the current one
55
- * doesn't, and points each at the distribution in DNS. A hostname a wildcard alias already covers
55
+ * doesn't. DNS is left to `pointDns`, which callers run once the route exists. A hostname a wildcard alias already covers
56
56
  * (`*.desk.example.com`, listed in a config's `domains` like any host) takes no alias or certificate
57
57
  * name of its own, which keeps the certificate under ACM's name quota. Removing works in reverse.
58
58
  */
@@ -70,11 +70,7 @@ export async function reconcileAliases(add: readonly string[], remove: readonly
70
70
  ].sort();
71
71
  const unchanged = wanted.length === current.length && wanted.every((alias) => current.includes(alias));
72
72
 
73
- for (const hostname of add.filter(needsDns)) {
74
- const outcome = await ensureRecord(hostname, "CNAME", distribution.domainName);
75
- if (outcome !== "unchanged") log(` DNS ${hostname} → ${distribution.domainName} ${outcome}`);
76
- }
77
- if (unchanged) return distribution;
73
+ if (unchanged) return { ...distribution, changed: false };
78
74
 
79
75
  const certificateArn = await ensureCertificate(wanted);
80
76
  const next: DistributionConfig = {
@@ -92,7 +88,31 @@ export async function reconcileAliases(add: readonly string[], remove: readonly
92
88
  );
93
89
  log(` distribution aliases → ${wanted.join(", ")}`);
94
90
  await deleteUnusedCertificates(certificateArn);
95
- return { ...distribution, aliases: wanted };
91
+ return { ...distribution, aliases: wanted, changed: true };
92
+ }
93
+
94
+ /** How long a new route record takes to reach every edge's KeyValueStore copy. */
95
+ const ROUTE_PROPAGATION_MS = 60_000;
96
+
97
+ /**
98
+ * Points hostnames at the distribution in DNS — the cut-over step, so it runs last: after the
99
+ * certificate and alias are live on every edge (`aliasesChanged`) and the route records written.
100
+ * A hostname whose record already points here is left alone; for the rest it first waits out the
101
+ * route's propagation, so no viewer is sent to an edge that doesn't know the hostname yet.
102
+ */
103
+ export async function pointDns(hostnames: readonly string[], aliasesChanged: boolean) {
104
+ const distribution = await requireDistribution();
105
+ const pending = [];
106
+ for (const hostname of hostnames.filter(needsDns))
107
+ if (!(await recordPointsAt(hostname, "CNAME", distribution.domainName))) pending.push(hostname);
108
+ if (!pending.length) return;
109
+ if (aliasesChanged) await waitDeployed(distribution.id);
110
+ log(` waiting ${ROUTE_PROPAGATION_MS / 1000}s for routes to reach every edge before DNS`);
111
+ await Bun.sleep(ROUTE_PROPAGATION_MS);
112
+ for (const hostname of pending) {
113
+ const outcome = await ensureRecord(hostname, "CNAME", distribution.domainName);
114
+ log(` DNS ${hostname} → ${distribution.domainName} ${outcome}`);
115
+ }
96
116
  }
97
117
 
98
118
  /** A distribution change takes minutes to reach every edge; most callers don't wait, bootstrap does. */
@@ -9,7 +9,7 @@ import { type Env, ensureSecret, readParameter, readStageEnv } from "./env.ts";
9
9
  import { startGithubDeployment } from "./github.ts";
10
10
  import { EDGE_HEADER, HOST_HEADER, stage as names, platform } from "./names.ts";
11
11
  import type { Target } from "./project.ts";
12
- import { reconcileAliases } from "./reconcile/edge.ts";
12
+ import { pointDns, reconcileAliases } from "./reconcile/edge.ts";
13
13
  import { ensureFunctionRole, ensureSchedulerRole } from "./reconcile/iam.ts";
14
14
  import { putRoutes, type RouteRecord } from "./reconcile/kvs.ts";
15
15
  import {
@@ -88,7 +88,7 @@ export async function release(target: Target, options: ReleaseOptions) {
88
88
  step("reconcile");
89
89
  const [role, schedulerRole] = await Promise.all([ensureFunctionRole(target), ensureSchedulerRole()]);
90
90
  await Promise.all([ensureLogGroup(target.logGroup), ensureScheduleGroup()]);
91
- if (target.domains.length) await reconcileAliases(target.domains);
91
+ const aliasesChanged = target.domains.length ? (await reconcileAliases(target.domains)).changed : false;
92
92
  const edgeSecret = await readParameter(platform.edgeSecretParameter);
93
93
  if (!edgeSecret) throw new CloudError("No edge secret in SSM — run `devkit cloud bootstrap`");
94
94
 
@@ -157,6 +157,7 @@ export async function release(target: Target, options: ReleaseOptions) {
157
157
  mode: target.routing,
158
158
  };
159
159
  await putRoutes(Object.fromEntries(hostnames.map((host) => [host, route])));
160
+ await pointDns(target.domains, aliasesChanged);
160
161
  const schedules = await reconcileSchedules(target, alias.arn, schedulerRole.arn);
161
162
  if (schedules.kept || schedules.removed) log(` ${schedules.kept} schedules, ${schedules.removed} removed`);
162
163