Skip to content

Commit 7d28806

Browse files
fix: #138 Inline code (#141)
1 parent e4cbd2d commit 7d28806

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/frameworks/cdkFramework.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -476,29 +476,40 @@ export class CdkFramework implements IFramework {
476476
.then(() => true)
477477
.catch(() => false),
478478
);
479-
480-
if (!codePath) {
481-
throw new Error(
482-
`Code file not found for Lambda function ${lambda.code.path}`,
483-
);
484-
}
485479
}
486480

487-
const packageJsonPath = await findPackageJson(codePath);
488-
Logger.verbose(`[CDK] package.json path: ${packageJsonPath}`);
481+
let packageJsonPath: string | undefined;
482+
if (codePath) {
483+
packageJsonPath = await findPackageJson(codePath);
484+
Logger.verbose(`[CDK] package.json path: ${packageJsonPath}`);
485+
}
489486

490487
return {
491488
cdkPath: lambda.cdkPath,
492489
stackName: lambda.stackName,
493490
packageJsonPath,
494-
codePath,
491+
codePath: codePath,
495492
handler,
496493
bundling: lambda.bundling,
497494
};
498495
}),
499496
);
500497

501-
return list;
498+
const filteredList = list.filter((lambda) => lambda.codePath);
499+
const noCodeList = list.filter((lambda) => !lambda.codePath);
500+
501+
if (noCodeList.length > 0) {
502+
Logger.warn(
503+
`[CDK] For the following Lambda functions the code file could not be determined and they will be ignored. Inline code is not supported.\n - ${noCodeList
504+
.map((l) => `${l.cdkPath}`)
505+
.join('\n - ')}`,
506+
);
507+
}
508+
509+
return filteredList.map((lambda) => ({
510+
...lambda,
511+
codePath: lambda.codePath!,
512+
}));
502513
}
503514

504515
/**

test/cdk-basic/lib/cdk-basic-stack.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ export class CdkbasicStack extends cdk.Stack {
4646
},
4747
);
4848

49+
const functionInlineCode = new lambda.Function(this, 'TestInlineCode', {
50+
runtime: lambda.Runtime.NODEJS_22_X,
51+
handler: 'index.handler',
52+
code: lambda.Code.fromInline(`
53+
exports.handler = async (event) => {
54+
const response = {
55+
statusCode: 200,
56+
body: JSON.stringify({
57+
message: 'Hello from inline Lambda function!',
58+
timestamp: new Date().toISOString(),
59+
event: event
60+
}),
61+
};
62+
};
63+
`),
64+
logRetention: log.RetentionDays.ONE_DAY,
65+
});
66+
4967
new cdk.CfnOutput(this, 'FunctionNameTestTsCommonJs', {
5068
value: functionTestTsCommonJs.functionName,
5169
});
@@ -57,5 +75,9 @@ export class CdkbasicStack extends cdk.Stack {
5775
new cdk.CfnOutput(this, 'FunctionNameTestJsCommonJs', {
5876
value: functionTestJsCommonJs.functionName,
5977
});
78+
79+
new cdk.CfnOutput(this, 'FunctionNameTestInlineCode', {
80+
value: functionInlineCode.functionName,
81+
});
6082
}
6183
}

0 commit comments

Comments
 (0)