Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
"type": "node",
"cwd": "${workspaceRoot}/test/cdk-esm"
},
{
"name": "LLDebugger - CDK nested",
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
"args": ["../../src/lldebugger.ts", "-c environment=test"],
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal",
"type": "node",
"cwd": "${workspaceRoot}/test/cdk-nested"
},
{
"name": "LLDebugger - CDK nested - observability",
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
"args": ["../../src/lldebugger.ts", "-c environment=test", "-o"],
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal",
"type": "node",
"cwd": "${workspaceRoot}/test/cdk-nested"
},
{
"name": "LLDebugger - SLS basic",
"program": "${workspaceRoot}/node_modules/tsx/dist/cli.mjs",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ If you have a new feature idea, please create and issue.

- [Ben Moses](https://github.com/benjymoses)
- [Kristian Dreher](https://www.linkedin.com/in/kristiandreher)
- [Hugo Lewenhaupt](https://www.linkedin.com/in/hugo-lewenhaupt-84751289)
- [Roger Chi](https://rogerchi.com/)
- [Sebastian / avocadomaster](https://github.com/avocadomaster)
- [Sebastian Bille](https://blog.sebastianbille.com)
Expand Down
31 changes: 30 additions & 1 deletion package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"test": "npm run build && RUN_TEST_FROM_CLI=true vitest run && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run",
"test-cdk-basic": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/cdk-basic.test.ts",
"test-cdk-basic-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/cdk-basic.test.ts",
"test-cdk-nested": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/cdk-nested.test.ts",
"test-cdk-nested-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/cdk-nested.test.ts",
"test-cdk-esm": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/cdk-esm.test.ts",
"test-cdk-esm-observable": "npm run build && RUN_TEST_FROM_CLI=true OBSERVABLE_MODE=true vitest run test/cdk-esm.test.ts",
"test-sls-basic": "npm run build && RUN_TEST_FROM_CLI=true vitest run test/sls-basic.test.ts",
Expand Down Expand Up @@ -148,6 +150,7 @@
"src/extension/*",
"test",
"test/cdk-basic",
"test/cdk-nested",
"test/cdk-esm",
"test/cdk-config",
"test/sls-basic",
Expand Down
28 changes: 26 additions & 2 deletions src/cloudFormation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,48 @@ async function getLambdasInStack(
Array<{
lambdaName: string;
logicalId: string;
stackName: string;
}>
> {
const response = await getCloudFormationResources(
stackName,
awsConfiguration,
);

const lambdaResources = response?.filter(
(resource) => resource.ResourceType === 'AWS::Lambda::Function',
);

return (
const nestedStacks = response?.filter(
(resource) => resource.ResourceType === 'AWS::CloudFormation::Stack',
);

const lambdas =
lambdaResources?.map((resource) => {
return {
lambdaName: resource.PhysicalResourceId!,
logicalId: resource.LogicalResourceId!,
stackName: stackName,
};
}) ?? []
}) ?? [];

const lambdasInNestedStacks = await Promise.all(
(nestedStacks ?? []).map(async (nestedStack) => {
if (!nestedStack.PhysicalResourceId) return [];

const lambdasInNestedStack = await getLambdasInStack(
nestedStack.PhysicalResourceId,
awsConfiguration,
);

return lambdasInNestedStack;
}),
);

const flattenedLambdasInNestedStacks = lambdasInNestedStacks.flat();

const allLambdas = [...lambdas, ...flattenedLambdasInNestedStacks];
return allLambdas;
}

export const CloudFormation = {
Expand Down
64 changes: 46 additions & 18 deletions src/frameworks/cdkFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,67 @@ export class CdkFramework implements IFramework {
JSON.stringify(lambdasInCdk, null, 2),
);

//get all stack names
const stackNames = [
...new Set( // unique
lambdasInCdk.map((lambda) => {
return lambda.stackName;
}),
),
];
const cdkTokenRegex = /^\${Token\[TOKEN\.\d+\]}$/;

const stackNamesDuplicated = lambdasInCdk.map((lambda) => {
if (cdkTokenRegex.test(lambda.stackName)) {
return lambda.rootStackName;
} else {
return lambda.stackName;
}
});

const stackNames = [...new Set(stackNamesDuplicated)];

Logger.verbose(
`[CDK] Found the following stacks in CDK: ${stackNames.join(', ')}`,
);

const lambdasDeployed = (
await Promise.all(
stackNames.map(async (stackName) => {
const lambdasInStackPromise = CloudFormation.getLambdasInStack(
const lambdasInStack = await CloudFormation.getLambdasInStack(
stackName,
awsConfiguration,
);
const lambdasMetadataPromise =
this.getLambdaCdkPathFromTemplateMetadata(
stackName,
awsConfiguration,
);

const lambdasInStack = await lambdasInStackPromise;
const stackAndNestedStackNames = [
...new Set(lambdasInStack.map((l) => l.stackName)),
];

const lambdasMetadata = (
await Promise.all(
stackAndNestedStackNames.map((stackOrNestedStackName) =>
this.getLambdaCdkPathFromTemplateMetadata(
stackOrNestedStackName,
awsConfiguration,
),
),
)
).flat();

Logger.verbose(
`[CDK] Found Lambda functions in the stack ${stackName}:`,
JSON.stringify(lambdasInStack, null, 2),
);
const lambdasMetadata = await lambdasMetadataPromise;

Logger.verbose(
`[CDK] Found Lambda functions in the stack ${stackName} in the template metadata:`,
JSON.stringify(lambdasMetadata, null, 2),
);

return lambdasInStack.map((lambda) => {
const lambdasPhysicalResourceIds = lambdasInStack.map((lambda) => {
return {
lambdaName: lambda.lambdaName,
cdkPath: lambdasMetadata.find(
(lm) => lm.logicalId === lambda.logicalId,
(lm) =>
lm.logicalId === lambda.logicalId &&
lm.stackName === lambda.stackName,
)?.cdkPath,
};
});

return lambdasPhysicalResourceIds;
}),
)
).flat();
Expand Down Expand Up @@ -183,6 +200,7 @@ export class CdkFramework implements IFramework {
Array<{
logicalId: string;
cdkPath: string;
stackName: string;
}>
> {
const cfTemplate = await CloudFormation.getCloudFormationStackTemplate(
Expand All @@ -200,8 +218,10 @@ export class CdkFramework implements IFramework {
return {
logicalId: key,
cdkPath: resource.Metadata['aws:cdk:path'],
stackName: stackName,
};
});

return lambdas;
}

Expand Down Expand Up @@ -308,9 +328,14 @@ export class CdkFramework implements IFramework {
`;
global.lambdas = global.lambdas ?? [];

let rootStack = this.stack;
while (rootStack.nestedStackParent) {
rootStack = rootStack.nestedStackParent;
}
const lambdaInfo = {
//cdkPath: this.node.defaultChild?.node.path ?? this.node.path,
stackName: this.stack.stackName,
rootStackName: rootStack.stackName,
codePath: props.entry,
code: props.code,
node: this.node,
Expand All @@ -320,6 +345,7 @@ export class CdkFramework implements IFramework {

// console.log("CDK INFRA: ", {
// stackName: lambdaInfo.stackName,
// rootStackName: lambdaInfo.rootStackName,
// codePath: lambdaInfo.codePath,
// code: lambdaInfo.code,
// handler: lambdaInfo.handler,
Expand Down Expand Up @@ -490,6 +516,7 @@ export class CdkFramework implements IFramework {
return {
cdkPath: lambda.cdkPath,
stackName: lambda.stackName,
rootStackName: lambda.rootStackName,
packageJsonPath,
codePath,
handler,
Expand Down Expand Up @@ -572,6 +599,7 @@ export class CdkFramework implements IFramework {
return lambdas as {
cdkPath: string;
stackName: string;
rootStackName: string;
codePath?: string;
code: {
path?: string;
Expand Down
1 change: 1 addition & 0 deletions src/frameworks/cdkFrameworkWorker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ parentPort.on('message', async (data) => {
handler: lambda.handler,
stackName: lambda.stackName,
codePath: lambda.codePath,
rootStackName: lambda.rootStackName,
code: {
path: lambda.code?.path,
},
Expand Down
Loading
Loading