Skip to content

Commit

Permalink
feat(nextjs-cdk-construct): add runtime options to regeneration (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathisobadia committed Nov 20, 2021
1 parent d024734 commit dd69323
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
38 changes: 21 additions & 17 deletions packages/serverless-components/nextjs-cdk-construct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ export class NextJSLambdaEdge extends cdk.Construct {
...(props.s3Props || {})
});

this.edgeLambdaRole = new Role(this, "NextEdgeLambdaRole", {
assumedBy: new CompositePrincipal(
new ServicePrincipal("lambda.amazonaws.com"),
new ServicePrincipal("edgelambda.amazonaws.com")
),
managedPolicies: [
ManagedPolicy.fromManagedPolicyArn(
this,
"NextApiLambdaPolicy",
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
)
]
});

const hasISRPages = Object.keys(this.prerenderManifest.routes).some(
(key) =>
typeof this.prerenderManifest.routes[key].initialRevalidateSeconds ===
Expand Down Expand Up @@ -116,11 +130,15 @@ export class NextJSLambdaEdge extends cdk.Construct {
"RegenerationFunction",
{
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_14_X,
timeout: Duration.seconds(30),
code: lambda.Code.fromAsset(
path.join(this.props.serverlessBuildOutDir, "regeneration-lambda")
)
),
runtime:
toLambdaOption("regenerationLambda", props.runtime) ??
lambda.Runtime.NODEJS_14_X,
memorySize: toLambdaOption("regenerationLambda", props.memory) ?? undefined,
timeout:
toLambdaOption("regenerationLambda", props.timeout) ?? Duration.seconds(30)
}
);

Expand All @@ -129,20 +147,6 @@ export class NextJSLambdaEdge extends cdk.Construct {
);
}

this.edgeLambdaRole = new Role(this, "NextEdgeLambdaRole", {
assumedBy: new CompositePrincipal(
new ServicePrincipal("lambda.amazonaws.com"),
new ServicePrincipal("edgelambda.amazonaws.com")
),
managedPolicies: [
ManagedPolicy.fromManagedPolicyArn(
this,
"NextApiLambdaPolicy",
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
)
]
});

this.defaultNextLambda = new lambda.Function(this, "NextLambda", {
functionName: toLambdaOption("defaultLambda", props.name),
description: `Default Lambda@Edge for Next CloudFront distribution`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { Duration, StackProps } from "@aws-cdk/core";

export type LambdaOption<T> =
| T
| { defaultLambda?: T; apiLambda?: T; imageLambda?: T };
| {
defaultLambda?: T;
apiLambda?: T;
imageLambda?: T;
regenerationLambda?: T;
};

export interface Props extends StackProps {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import { LambdaOption } from "../props";

export const toLambdaOption = <T extends unknown>(
key: "defaultLambda" | "apiLambda" | "imageLambda",
key: "defaultLambda" | "apiLambda" | "imageLambda" | "regenerationLambda",
option?: LambdaOption<T>
): T | undefined => {
if (
typeof option !== "object" ||
!(
"defaultLambda" in option ||
"apiLambda" in option ||
"imageLambda" in option
"imageLambda" in option ||
"regenerationLambda" in option
)
) {
return option as T | undefined;
Expand Down

0 comments on commit dd69323

Please sign in to comment.