Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: Added warm feature to all SsrSite constructs #2996

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 24 additions & 20 deletions packages/sst/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,30 @@ await esbuild.build({

// Move support packages that need to be transpiled
await Promise.all(
["bootstrap-metadata-function", "custom-resources", "script-function"].map(
(dir) =>
esbuild.build({
keepNames: true,
bundle: true,
platform: "node",
target: "esnext",
format: "esm",
entryPoints: [`./support/${dir}/index.ts`],
banner: {
js: [
`import { createRequire as topLevelCreateRequire } from 'module';`,
`const require = topLevelCreateRequire(import.meta.url);`,
].join(""),
},
outExtension: {
".js": ".mjs",
},
outdir: `./dist/support/${dir}/`,
})
[
"bootstrap-metadata-function",
"custom-resources",
"script-function",
"warmer-function",
].map((dir) =>
esbuild.build({
keepNames: true,
bundle: true,
platform: "node",
target: "esnext",
format: "esm",
entryPoints: [`./support/${dir}/index.ts`],
banner: {
js: [
`import { createRequire as topLevelCreateRequire } from 'module';`,
`const require = topLevelCreateRequire(import.meta.url);`,
].join(""),
},
outExtension: {
".js": ".mjs",
},
outdir: `./dist/support/${dir}/`,
})
)
);

Expand Down
63 changes: 0 additions & 63 deletions packages/sst/src/constructs/NextjsSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export interface NextjsSiteProps extends Omit<SsrSiteProps, "nodejs"> {
*/
memorySize?: number | Size;
};
/**
* The number of server functions to keep warm. This option is only supported for the regional mode.
* @default Server function is not kept warm
*/
warm?: number;
cdk?: SsrSiteProps["cdk"] & {
revalidation?: Pick<FunctionProps, "vpc" | "vpcSubnets">;
};
Expand Down Expand Up @@ -233,64 +228,6 @@ export class NextjsSite extends SsrSite {
return fn;
}

private createWarmer() {
const { warm, edge } = this.props;
if (!warm) return;

if (warm && edge) {
throw new Error(
`Warming is currently supported only for the regional mode.`
);
}

if (!this.serverLambdaForRegional) return;

// Create warmer function
const warmer = new CdkFunction(this, "WarmerFunction", {
description: "Next.js warmer",
code: Code.fromAsset(
path.join(this.props.path, ".open-next/warmer-function")
),
runtime: Runtime.NODEJS_18_X,
handler: "index.handler",
timeout: CdkDuration.minutes(15),
memorySize: 1024,
environment: {
FUNCTION_NAME: this.serverLambdaForRegional.functionName,
CONCURRENCY: warm.toString(),
},
});
this.serverLambdaForRegional.grantInvoke(warmer);

// Create cron job
new Rule(this, "WarmerRule", {
schedule: Schedule.rate(CdkDuration.minutes(5)),
targets: [new LambdaFunction(warmer, { retryAttempts: 0 })],
});

// Create custom resource to prewarm on deploy
const stack = Stack.of(this) as Stack;
const policy = new Policy(this, "PrewarmerPolicy", {
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["lambda:InvokeFunction"],
resources: [warmer.functionArn],
}),
],
});
stack.customResourceHandler.role?.attachInlinePolicy(policy);
const resource = new CustomResource(this, "Prewarmer", {
serviceToken: stack.customResourceHandler.functionArn,
resourceType: "Custom::FunctionInvoker",
properties: {
version: Date.now().toString(),
functionName: warmer.functionName,
},
});
resource.node.addDependency(policy);
}

protected createCloudFrontDistributionForRegional(): Distribution {
/**
* Next.js requests
Expand Down
67 changes: 67 additions & 0 deletions packages/sst/src/constructs/SsrSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
import { AwsCliLayer } from "aws-cdk-lib/lambda-layer-awscli";
import { S3Origin, HttpOrigin } from "aws-cdk-lib/aws-cloudfront-origins";
import { CloudFrontTarget } from "aws-cdk-lib/aws-route53-targets";
import { Rule, Schedule } from "aws-cdk-lib/aws-events";
import { LambdaFunction } from "aws-cdk-lib/aws-events-targets";

import { App } from "./App.js";
import { Stack } from "./Stack.js";
Expand Down Expand Up @@ -244,6 +246,12 @@ export interface SsrSiteProps {
* @default false
*/
waitForInvalidation?: boolean;

/**
* The number of server functions to keep warm. This option is only supported for the regional mode.
* @default Server function is not kept warm
*/
warm?: number;
cdk?: {
/**
* Allows you to override default id for this construct.
Expand Down Expand Up @@ -387,6 +395,9 @@ export abstract class SsrSite extends Construct implements SSTConstruct {
// Connect Custom Domain to CloudFront Distribution
this.createRoute53Records();

// Setup Function Warmer
this.createWarmer();

useDeferredTasks().add(async () => {
// Build app
this.buildApp();
Expand Down Expand Up @@ -880,6 +891,62 @@ export abstract class SsrSite extends Construct implements SSTConstruct {
server?.role?.attachInlinePolicy(policy);
}

private createWarmer() {
const { warm, edge } = this.props;
if (!warm) return;

if (warm && edge) {
throw new Error(
`Warming is currently supported only for the regional mode.`
);
}

if (!this.serverLambdaForRegional) return;

// Create warmer function
const warmer = new CdkFunction(this, "WarmerFunction", {
description: "Server handler warmer",
code: Code.fromAsset(path.join(__dirname, "../support/warmer-function")),
runtime: Runtime.NODEJS_18_X,
handler: "index.handler",
timeout: CdkDuration.minutes(15),
memorySize: 1024,
environment: {
FUNCTION_NAME: this.serverLambdaForRegional.functionName,
CONCURRENCY: warm.toString(),
},
});
this.serverLambdaForRegional.grantInvoke(warmer);

// Create cron job
new Rule(this, "WarmerRule", {
schedule: Schedule.rate(CdkDuration.minutes(5)),
targets: [new LambdaFunction(warmer, { retryAttempts: 0 })],
});

// Create custom resource to prewarm on deploy
const stack = Stack.of(this) as Stack;
const policy = new Policy(this, "PrewarmerPolicy", {
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["lambda:InvokeFunction"],
resources: [warmer.functionArn],
}),
],
});
stack.customResourceHandler.role?.attachInlinePolicy(policy);
const resource = new CustomResource(this, "Prewarmer", {
serviceToken: stack.customResourceHandler.functionArn,
resourceType: "Custom::FunctionInvoker",
properties: {
version: Date.now().toString(),
functionName: warmer.functionName,
},
});
resource.node.addDependency(policy);
}

/////////////////////
// CloudFront Distribution
/////////////////////
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/stacks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export async function load(input: string, shallow?: boolean) {
filename: "sst.config.ts",
plugins: [ts],
});
if (!ast)
throw new Error(
"Your babel config is not defined and interpreted as `null`, please check your config."
);
babel.traverse(ast, {
ObjectMethod(path) {
const { key } = path.node;
Expand Down
76 changes: 76 additions & 0 deletions packages/sst/support/warmer-function/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
import type { Context } from "aws-lambda";
const lambda = new LambdaClient({});
const FUNCTION_NAME = process.env.FUNCTION_NAME!;
const CONCURRENCY = parseInt(process.env.CONCURRENCY!);

function generateUniqueId() {
return Math.random().toString(36).slice(2, 8);
}

export interface WarmerEvent {
type: "warmer";
warmerId: string;
index: number;
concurrency: number;
delay: number;
}

export interface WarmerResponse {
serverId: string;
}

export async function handler(_event: any, context: Context) {
const warmerId = `warmer-${generateUniqueId()}`;
console.log({
event: "warmer invoked",
functionName: FUNCTION_NAME,
concurrency: CONCURRENCY,
warmerId,
});

// Warm
const ret = await Promise.all(
Array.from({ length: CONCURRENCY }, (_v, i) => i).map((i) => {
try {
return lambda.send(
new InvokeCommand({
FunctionName: FUNCTION_NAME,
InvocationType: "RequestResponse",
Payload: Buffer.from(
JSON.stringify({
type: "warmer",
warmerId,
index: i,
concurrency: CONCURRENCY,
delay: 75,
} satisfies WarmerEvent)
),
})
);
} catch (e) {
console.error(`failed to warm up #${i}`, e);
// ignore error
}
})
);

// Print status
const warmedServerIds: string[] = [];
ret.forEach((r, i) => {
if (r?.StatusCode !== 200 || !r?.Payload) {
console.error(`failed to warm up #${i}:`, r?.Payload?.toString());
return;
}
const payload = JSON.parse(
Buffer.from(r.Payload).toString()
) as WarmerResponse;
warmedServerIds.push(payload.serverId);
});
console.error({
event: "warmer result",
sent: CONCURRENCY,
success: warmedServerIds.length,
uniqueServersWarmed: [...new Set(warmedServerIds)].length,
});
}