Skip to content

Commit

Permalink
fix(awscdk): AWS SDK v2 is not available for node18.x runtime
Browse files Browse the repository at this point in the history
The Node.js 18.x runtime includes AWS SDK v3. Adapt esbuild
externals to reflect that.

Same as aws/aws-cdk#22989
  • Loading branch information
jogold committed Jan 16, 2023
1 parent e9ce56a commit 198244a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
19 changes: 18 additions & 1 deletion docs/api/API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/awscdk/lambda-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class LambdaExtension extends Component {
const bundle = bundler.addBundle(options.entrypoint, {
platform: bundlerRuntime.esbuildPlatform,
target: bundlerRuntime.esbuildTarget,
externals: ["aws-sdk"],
externals: bundlerRuntime.defaultExternals,
outfile: `extensions/${name}`,
// Make the output executable because Lambda expects to run
// extensions as stand-alone programs alongside the main lambda
Expand Down
39 changes: 32 additions & 7 deletions src/awscdk/lambda-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class LambdaFunction extends Component {
const bundle = bundler.addBundle(entrypoint, {
target: runtime.esbuildTarget,
platform: runtime.esbuildPlatform,
externals: ["aws-sdk"],
externals: runtime.defaultExternals,
...options.bundlingOptions,
tsconfigPath: (project as TypeScriptProject)?.tsconfigDev?.fileName,
});
Expand Down Expand Up @@ -283,6 +283,18 @@ export class LambdaFunction extends Component {
}
}

/**
* Options for the AWS Lambda function runtime
*/
export interface LambdaRuntimeOptions {
/**
* Packages that are considered externals by default when bundling
*
* @default ['@aws-sdk/*']
*/
readonly defaultExternals?: string[];
}

/**
* The runtime for the AWS Lambda function.
*/
Expand All @@ -293,31 +305,35 @@ export class LambdaRuntime {
*/
public static readonly NODEJS_10_X = new LambdaRuntime(
"nodejs10.x",
"node10"
"node10",
{ defaultExternals: ["aws-sdk"] }
);

/**
* Node.js 12.x
*/
public static readonly NODEJS_12_X = new LambdaRuntime(
"nodejs12.x",
"node12"
"node12",
{ defaultExternals: ["aws-sdk"] }
);

/**
* Node.js 14.x
*/
public static readonly NODEJS_14_X = new LambdaRuntime(
"nodejs14.x",
"node14"
"node14",
{ defaultExternals: ["aws-sdk"] }
);

/**
* Node.js 16.x
*/
public static readonly NODEJS_16_X = new LambdaRuntime(
"nodejs16.x",
"node16"
"node16",
{ defaultExternals: ["aws-sdk"] }
);

/**
Expand All @@ -330,6 +346,8 @@ export class LambdaRuntime {

public readonly esbuildPlatform = "node";

public readonly defaultExternals: string[];

public constructor(
/**
* The Node.js runtime to use
Expand All @@ -339,6 +357,13 @@ export class LambdaRuntime {
/**
* The esbuild setting to use.
*/
public readonly esbuildTarget: string
) {}
public readonly esbuildTarget: string,

/**
* Options for this runtime.
*/
options?: LambdaRuntimeOptions
) {
this.defaultExternals = options?.defaultExternals ?? ["@aws-sdk/*"];
}
}
25 changes: 25 additions & 0 deletions test/awscdk/lambda-function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ test("runtime can be used to customize the lambda runtime and esbuild target", (
});
});

test("aws sdk v3 packages are considered external with NODEJS_18_X", () => {
const project = new TypeScriptProject({
name: "hello",
defaultReleaseBranch: "main",
});

new awscdk.LambdaFunction(project, {
entrypoint: join("src", "hello.lambda.ts"),
runtime: awscdk.LambdaRuntime.NODEJS_18_X,
cdkDeps: cdkDepsForProject(project),
});

const snapshot = Testing.synth(project);
const tasks = snapshot[".projen/tasks.json"].tasks;
expect(tasks["bundle:hello.lambda"]).toEqual({
description: "Create a JavaScript bundle from src/hello.lambda.ts",
name: "bundle:hello.lambda",
steps: [
{
exec: 'esbuild --bundle src/hello.lambda.ts --target="node18" --platform="node" --outfile="assets/hello.lambda/index.js" --tsconfig="tsconfig.dev.json" --external:@aws-sdk/*',
},
],
});
});

test("AWS SDK connection reuse", () => {
const project = new TypeScriptProject({
name: "hello",
Expand Down

0 comments on commit 198244a

Please sign in to comment.