mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* feat(cli): lambda deploy works from the npm install, no repo checkout Each release builds the Lambda handler zip on Linux, attaches it to the GitHub release, and pins its URL and sha256 (plus the SAM template) in @hyperframes/aws-lambda before npm publish. Outside a checkout, lambda deploy downloads that zip, refuses it on a sha256 mismatch, and deploys it with the packaged template. With neither a checkout nor a pinned zip it fails up front with one message naming every way out. * fix(cli): lambda deploy test mocks the pinned-handler reader, comments fit the checks * test(cli): sam tests deploy from a SAM directory, not the repo root * chore(aws-lambda): raise the package size budget for the pinned handler template Measured with pnpm@10.17.1 pack on Linux. Release state (prebuilt/ written by pin-handler-zip): 77,307 packed / 285,654 unpacked. CI state (no prebuilt/): 70,104 / 253,390. Packed is the check's 5% headroom over the release size. Unpacked is 292,000 (2.2% over release) because a 5% budget would put the CI pack under the 85% ratchet. * test(cli): anchor the docs link check in the lambda deploy fallback test * fix(cli): lambda deploy logs the verified handler sha256 and keeps a presigned query out of errors * fix(cli): a redirect-limit download error keeps a signed query out of its message * test(cli): the redirect-limit leak test types its rejection * docs(aws-lambda): header comments cut to what the code cannot say * chore(aws-lambda): size budget fits both the CI and the release package
142 lines
5.0 KiB
TypeScript
142 lines
5.0 KiB
TypeScript
/**
|
|
* `hyperframes lambda deploy` — build the handler ZIP (or, outside a
|
|
* checkout, fetch the adapter's pinned prebuilt one), sam-deploy the
|
|
* Phase 6a SAM template, and persist the stack outputs locally so the
|
|
* other lambda subcommands don't need re-derive them.
|
|
*
|
|
* Idempotent: re-running points at the same stack name and SAM resolves
|
|
* the changeset to a no-op when nothing changed.
|
|
*/
|
|
|
|
import { spawnSync } from "node:child_process";
|
|
import { existsSync } from "node:fs";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { c } from "../../ui/colors.js";
|
|
import { stagePrebuiltHandler } from "./prebuiltHandler.js";
|
|
import { fetchStackOutputs, locateSamTemplate, samDeploy } from "./sam.js";
|
|
import { repoRoot } from "./repoRoot.js";
|
|
import { DEFAULT_STACK_NAME, writeStackOutputs } from "./state.js";
|
|
|
|
export interface DeployArgs {
|
|
stackName: string;
|
|
region: string;
|
|
awsProfile?: string;
|
|
/** -1 removes the reservation. Omitted: a redeploy keeps it; a new stack reserves none. */
|
|
reservedConcurrency?: number;
|
|
chromeSource: "sparticuz" | "chrome-headless-shell";
|
|
lambdaMemoryMb: number;
|
|
/** Override the handler-ZIP rebuild step. Useful for CI runs that already built it. */
|
|
skipBuild: boolean;
|
|
}
|
|
|
|
const DEFAULT_REGION = "us-east-1";
|
|
const DEFAULT_MEMORY_MB = 10240;
|
|
|
|
export async function runDeploy(args: Partial<DeployArgs> = {}): Promise<void> {
|
|
const resolved: DeployArgs = {
|
|
stackName: args.stackName ?? DEFAULT_STACK_NAME,
|
|
region: args.region ?? process.env.AWS_REGION ?? DEFAULT_REGION,
|
|
awsProfile: args.awsProfile ?? process.env.AWS_PROFILE,
|
|
reservedConcurrency: args.reservedConcurrency,
|
|
chromeSource: args.chromeSource ?? "sparticuz",
|
|
lambdaMemoryMb: args.lambdaMemoryMb ?? DEFAULT_MEMORY_MB,
|
|
skipBuild: args.skipBuild ?? false,
|
|
};
|
|
|
|
const samDir = await prepareSamDir(repoRoot(), resolved);
|
|
|
|
console.log(c.dim(`→ sam deploy (stack=${resolved.stackName} region=${resolved.region})`));
|
|
samDeploy({
|
|
samDir,
|
|
stackName: resolved.stackName,
|
|
region: resolved.region,
|
|
awsProfile: resolved.awsProfile,
|
|
reservedConcurrency: resolved.reservedConcurrency,
|
|
lambdaMemoryMb: resolved.lambdaMemoryMb,
|
|
chromeSource: resolved.chromeSource,
|
|
});
|
|
|
|
console.log(c.dim("→ Reading stack outputs"));
|
|
const outputs = fetchStackOutputs({
|
|
stackName: resolved.stackName,
|
|
region: resolved.region,
|
|
awsProfile: resolved.awsProfile,
|
|
});
|
|
|
|
const statePath = writeStackOutputs({
|
|
stackName: resolved.stackName,
|
|
region: resolved.region,
|
|
bucketName: outputs.bucketName,
|
|
stateMachineArn: outputs.stateMachineArn,
|
|
functionName: outputs.functionName,
|
|
lambdaMemoryMb: resolved.lambdaMemoryMb,
|
|
reservedConcurrency: outputs.reservedConcurrency,
|
|
deployedAt: new Date().toISOString(),
|
|
});
|
|
|
|
if (resolved.reservedConcurrency === -1) {
|
|
console.log(
|
|
c.warn(
|
|
"Removed the stack's reserved concurrency; each render now sizes itself to the account.",
|
|
),
|
|
);
|
|
} else if (
|
|
resolved.reservedConcurrency === undefined &&
|
|
outputs.reservedConcurrency !== undefined
|
|
) {
|
|
console.log(
|
|
c.warn(
|
|
`Kept the stack's reserved concurrency of ${outputs.reservedConcurrency}. Pass --concurrency=N to change it.`,
|
|
),
|
|
);
|
|
}
|
|
|
|
console.log();
|
|
console.log(c.success("Stack deployed."));
|
|
console.log(` ${c.dim("Bucket:")} ${outputs.bucketName}`);
|
|
console.log(` ${c.dim("State machine:")} ${outputs.stateMachineArn}`);
|
|
console.log(` ${c.dim("Function:")} ${outputs.functionName}`);
|
|
console.log(` ${c.dim("State file:")} ${resolve(statePath)}`);
|
|
console.log();
|
|
console.log(c.dim(`Render with: hyperframes lambda render <project-dir>`));
|
|
}
|
|
|
|
/** Checkout: build the ZIP from source. Otherwise: stage the adapter's pinned prebuilt ZIP. */
|
|
async function prepareSamDir(root: string | undefined, args: DeployArgs): Promise<string> {
|
|
if (!root) {
|
|
if (args.chromeSource !== "sparticuz") {
|
|
throw new Error(
|
|
"[lambda deploy] The prebuilt handler bundles sparticuz Chrome; --chrome-source=chrome-headless-shell needs a hyperframes checkout.",
|
|
);
|
|
}
|
|
return stagePrebuiltHandler();
|
|
}
|
|
const samDir = dirname(locateSamTemplate(root));
|
|
if (!args.skipBuild) {
|
|
console.log(c.dim("→ Building handler ZIP"));
|
|
buildHandlerZip(root);
|
|
} else {
|
|
const zip = join(root, "packages", "aws-lambda", "dist", "handler.zip");
|
|
if (!existsSync(zip)) {
|
|
throw new Error(
|
|
`--skip-build set but ${zip} does not exist. Run \`bun run --cwd packages/aws-lambda build:zip\` first or drop --skip-build.`,
|
|
);
|
|
}
|
|
}
|
|
return samDir;
|
|
}
|
|
|
|
function buildHandlerZip(root: string): void {
|
|
// bun run --cwd packages/aws-lambda build:zip
|
|
const result = spawnSync(
|
|
"bun",
|
|
["run", "--cwd", join(root, "packages", "aws-lambda"), "build:zip"],
|
|
{ stdio: "inherit" },
|
|
);
|
|
if (result.status !== 0) {
|
|
throw new Error(
|
|
`[lambda deploy] handler ZIP build exited with code ${result.status ?? "unknown"}`,
|
|
);
|
|
}
|
|
}
|