Skip to content

Commit

Permalink
feat: default lambda packaging to zip with option for docker
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreaves committed Sep 19, 2024
1 parent 23ff02c commit 9f9d286
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/cdk/GatsbySite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,39 @@ export class GatsbySite extends Construct {
if ("DISABLED" === options.target) return acc;

if ("LAMBDA" === options.target) {
const lambdaFunction = new lambda.DockerImageFunction(
this,
`Function-${fn.functionId}`,
{
timeout:
options.timeout ?? isSsrEngine
? cdk.Duration.seconds(30)
: cdk.Duration.minutes(1),
architecture: lambda.Architecture.X86_64,
memorySize: options.memorySize ?? isSsrEngine ? 1024 : 512,
logRetention: logs.RetentionDays.ONE_MONTH,
code: lambda.DockerImageCode.fromImageAsset(fn.functionDir, {
target: "lambda",
platform: ecrAssets.Platform.LINUX_AMD64,
}),
insightsVersion: lambda.LambdaInsightsVersion.VERSION_1_0_229_0,
...options.functionOptions,
},
);
const entryPointDir = path.dirname(fn.pathToEntryPoint);

const functionOptions: lambda.FunctionOptions = {
timeout:
options.timeout ?? isSsrEngine
? cdk.Duration.seconds(30)
: cdk.Duration.minutes(1),
architecture: lambda.Architecture.X86_64,
memorySize: options.memorySize ?? isSsrEngine ? 1024 : 512,
logRetention: logs.RetentionDays.ONE_MONTH,
insightsVersion: lambda.LambdaInsightsVersion.VERSION_1_0_229_0,
...options.functionOptions,
};

const lambdaFunction =
"DOCKER" === options.packaging
? new lambda.DockerImageFunction(
this,
`Function-${fn.functionId}`,
{
...functionOptions,
architecture: lambda.Architecture.X86_64,
code: lambda.DockerImageCode.fromImageAsset(fn.functionDir, {
platform: ecrAssets.Platform.LINUX_AMD64,
}),
},
)
: new lambda.Function(this, `Function-${fn.functionId}`, {
...functionOptions,
runtime: lambda.Runtime.NODEJS_20_X,
code: lambda.Code.fromAsset(fn.functionDir),
handler: `${entryPointDir}/handler.handler`,
});

const lambdaAlias = lambdaFunction.addAlias("current", {
...(options.provisionedConcurrentExecutions
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export type GatsbyFunctionOptionsLambda = {
* Provisioned concurrency configuration for the alias.
*/
provisionedConcurrentExecutions?: lambda.AliasOptions["provisionedConcurrentExecutions"];
/**
* If set to "ZIP", function will be packaged as a zip, so can use layers.
* If set to "DOCKER", function will be packaged as a docker image, so cannot use layers.
* @default "ZIP"
*/
packaging?: "ZIP" | "DOCKER";
};

export type GatsbyFunctionOptionsFargate = {
Expand Down

0 comments on commit 9f9d286

Please sign in to comment.