Skip to content

Commit 08d8239

Browse files
Improve messages and logging
1 parent f4606b9 commit 08d8239

File tree

2 files changed

+217
-140
lines changed

2 files changed

+217
-140
lines changed

src/infraDeploy.ts

Lines changed: 109 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ async function deleteLayerVersion(
307307
*/
308308
async function analyzeRemovePolicyFromLambdaRole(functionName: string) {
309309
try {
310+
Logger.verbose(
311+
`[Function ${functionName}] Analyzing policy removal from Lambda role`,
312+
);
313+
310314
const getFunctionResponse = await getLambdaClient().send(
311315
new GetFunctionCommand({
312316
FunctionName: functionName,
@@ -327,10 +331,17 @@ async function analyzeRemovePolicyFromLambdaRole(functionName: string) {
327331
);
328332
}
329333

334+
Logger.verbose(`[Function ${functionName}] Found role: ${roleName}`);
335+
330336
const existingPolicy = await createPolicyDocument(roleName);
331337

338+
const needToRemovePolicy = !!existingPolicy;
339+
Logger.verbose(
340+
`[Function ${functionName}] Policy ${needToRemovePolicy ? 'needs to be removed' : 'not found to remove'} from role ${roleName}`,
341+
);
342+
332343
return {
333-
needToRemovePolicy: !!existingPolicy,
344+
needToRemovePolicy,
334345
roleName,
335346
};
336347
} catch (error: any) {
@@ -355,6 +366,7 @@ async function removePolicyFromLambdaRole(roleName: string) {
355366
PolicyName: inlinePolicyName,
356367
}),
357368
);
369+
Logger.verbose(`[Role ${roleName}] Policy removed successfully`);
358370
} catch (error: any) {
359371
throw new Error(`Failed to remove policy from the role ${roleName}.`, {
360372
cause: error,
@@ -369,6 +381,8 @@ async function removePolicyFromLambdaRole(roleName: string) {
369381
*/
370382
async function createPolicyDocument(roleName: string) {
371383
try {
384+
Logger.verbose(`[Role ${roleName}] Checking for existing policy document`);
385+
372386
const policy = await getIAMClient().send(
373387
new GetRolePolicyCommand({
374388
RoleName: roleName,
@@ -377,15 +391,20 @@ async function createPolicyDocument(roleName: string) {
377391
);
378392

379393
if (policy.PolicyDocument) {
394+
Logger.verbose(`[Role ${roleName}] Found existing policy document`);
380395
const policyDocument = JSON.parse(
381396
decodeURIComponent(policy.PolicyDocument),
382397
);
383398
return policyDocument;
384399
} else {
400+
Logger.verbose(`[Role ${roleName}] No policy document found`);
385401
return undefined;
386402
}
387403
} catch (error: any) {
388404
if (error.name === 'NoSuchEntityException') {
405+
Logger.verbose(
406+
`[Role ${roleName}] Policy does not exist (NoSuchEntityException)`,
407+
);
389408
return undefined;
390409
} else {
391410
throw new Error(
@@ -400,14 +419,22 @@ async function createPolicyDocument(roleName: string) {
400419
* Deploy the infrastructure
401420
*/
402421
async function applyAddingInfra(changes: InfraAddingChanges) {
422+
Logger.verbose(
423+
'Starting infrastructure deployment for adding Lambda Live Debugger',
424+
);
425+
403426
let layerVersionArn: string;
404427

405428
if (changes.deployLayer) {
429+
Logger.verbose('Deploying new layer version');
406430
layerVersionArn = await deployLayer();
407431
} else {
408432
if (!changes.existingLayerVersionArn) {
409433
throw new Error('Expected existing layer ARN but none provided.');
410434
}
435+
Logger.verbose(
436+
`Using existing layer version: ${changes.existingLayerVersionArn}`,
437+
);
411438
layerVersionArn = changes.existingLayerVersionArn;
412439
}
413440

@@ -448,12 +475,17 @@ async function applyAddingInfra(changes: InfraAddingChanges) {
448475
}
449476

450477
/**
451-
* Get the planed infrastructure changes including removal from filtered functions
478+
* Get the planned infrastructure changes including removal from filtered functions
452479
*/
453480
async function getInfraChangesForAdding(): Promise<InfraAddingChanges> {
481+
Logger.verbose(
482+
'Analyzing infrastructure changes for adding Lambda Live Debugger',
483+
);
484+
454485
const existingLayer = await findExistingLayerVersion();
455486

456487
const configLambdasAll = Configuration.getLambdasAll();
488+
457489
const configLambdasUpdate = configLambdasAll.filter(
458490
(l) => !(l.filteredOut === true),
459491
);
@@ -533,12 +565,26 @@ async function getInfraChangesForAdding(): Promise<InfraAddingChanges> {
533565
*/
534566
async function analyzeRemoveLambda(functionName: string) {
535567
try {
536-
const { environmentVariables, ddlLayerArns } =
537-
await getLambdaConfiguration(functionName);
568+
const {
569+
environmentVariables,
570+
ddlLayerArns,
571+
otherLayerArns,
572+
initialTimeout,
573+
} = await getLambdaConfiguration(functionName);
538574

539575
const needToRemoveLayer = ddlLayerArns.length > 0;
540576
let needToRemoveEnvironmentVariables = false;
541577

578+
if (needToRemoveLayer) {
579+
Logger.verbose(
580+
`[Function ${functionName}] Lambda Live Debugger layer(s) detected: ${ddlLayerArns.join(', ')}. Marked for removal.`,
581+
);
582+
} else {
583+
Logger.verbose(
584+
`[Function ${functionName}] No Lambda Live Debugger layer(s) to remove.`,
585+
);
586+
}
587+
542588
const ddlEnvironmentVariables = getEnvironmentVariablesForDebugger({
543589
// set dummy data, so we just get the list of environment variables
544590
functionName: 'xxx',
@@ -555,13 +601,16 @@ async function analyzeRemoveLambda(functionName: string) {
555601
}
556602
}
557603

558-
const removal = {
559-
needToRemove: needToRemoveLayer || needToRemoveEnvironmentVariables,
560-
};
561-
if (removal.needToRemove) {
562-
const config = await getLambdaConfiguration(functionName);
604+
Logger.verbose(
605+
`[Function ${functionName}] ${needToRemoveEnvironmentVariables ? 'Removing environment variables' : 'No environment variables to remove'}. Existing environment variables: `,
606+
JSON.stringify(ddlEnvironmentVariables, null, 2),
607+
);
608+
609+
const needToRemove = needToRemoveLayer || needToRemoveEnvironmentVariables;
610+
611+
if (needToRemove) {
563612
const initialExecWrapper =
564-
config.environmentVariables.LLD_INITIAL_AWS_LAMBDA_EXEC_WRAPPER;
613+
environmentVariables.LLD_INITIAL_AWS_LAMBDA_EXEC_WRAPPER;
565614
const ddlEnvironmentVariables = getEnvironmentVariablesForDebugger({
566615
functionName: 'xxx',
567616
timeout: 0,
@@ -570,7 +619,7 @@ async function analyzeRemoveLambda(functionName: string) {
570619
});
571620

572621
// Remove LLD environment variables
573-
const cleanedEnvironmentVariables = { ...config.environmentVariables };
622+
const cleanedEnvironmentVariables = { ...environmentVariables };
574623
for (const [key] of Object.entries(ddlEnvironmentVariables)) {
575624
if (key === 'AWS_LAMBDA_EXEC_WRAPPER') {
576625
if (cleanedEnvironmentVariables[key] === lldWrapperPath) {
@@ -588,9 +637,9 @@ async function analyzeRemoveLambda(functionName: string) {
588637

589638
return {
590639
functionName,
591-
layers: config.otherLayerArns,
640+
layers: otherLayerArns,
592641
environmentVariables: cleanedEnvironmentVariables,
593-
timeout: config.initialTimeout,
642+
timeout: initialTimeout,
594643
};
595644
}
596645
return undefined;
@@ -605,14 +654,20 @@ async function analyzeRemoveLambda(functionName: string) {
605654
* Get the planned removal changes
606655
*/
607656
async function getInfraChangesForRemoving(): Promise<InfraRemovalChanges> {
657+
Logger.verbose(
658+
'Analyzing infrastructure changes for removing Lambda Live Debugger',
659+
);
660+
661+
const allLambdas = Configuration.getLambdasAll();
662+
608663
const lambdasToRemovePromise = Promise.all(
609-
Configuration.getLambdasAll().map(async (func) => {
664+
allLambdas.map(async (func) => {
610665
return analyzeRemoveLambda(func.functionName);
611666
}),
612667
);
613668

614669
const rolesToRemovePromise = Promise.all(
615-
Configuration.getLambdasAll().map(async (func) => {
670+
allLambdas.map(async (func) => {
616671
const roleRemoval = await analyzeRemovePolicyFromLambdaRole(
617672
func.functionName,
618673
);
@@ -638,7 +693,8 @@ async function getInfraChangesForRemoving(): Promise<InfraRemovalChanges> {
638693
* Remove the infrastructure
639694
*/
640695
async function applyRemoveInfra(changes: InfraRemovalChanges) {
641-
Logger.verbose('Removing Lambda Live Debugger infrastructure.');
696+
Logger.verbose('Starting infrastructure removal');
697+
642698
const promises: Promise<void>[] = [];
643699

644700
for (const lambdaData of changes.lambdasToRemove) {
@@ -710,7 +766,7 @@ async function getLambdaConfiguration(functionName: string) {
710766
*/
711767
async function addLayerToLambda(lambdaData: InfraLambdaUpdate) {
712768
Logger.verbose(
713-
`[Function ${lambdaData.functionName}] Adding Lambda Live Debugger layer and environment variables`,
769+
`[Function ${lambdaData.functionName}] Adding layer and environment variables`,
714770
);
715771
try {
716772
await updateLambda(lambdaData);
@@ -727,7 +783,7 @@ async function addLayerToLambda(lambdaData: InfraLambdaUpdate) {
727783
*/
728784
async function removeLayerFromLambda(lambdaData: InfraLambdaUpdate) {
729785
Logger.verbose(
730-
`[Function ${lambdaData.functionName}] Removing Lambda Live Debugger layer and environment variables`,
786+
`[Function ${lambdaData.functionName}] Removing layer and environment variables`,
731787
);
732788
try {
733789
await updateLambda(lambdaData);
@@ -766,41 +822,38 @@ async function analyzeLambdaAdd(
766822
functionName: string,
767823
existingLayerVersionArn: string | undefined,
768824
) {
825+
const { environmentVariables, ddlLayerArns, otherLayerArns, initialTimeout } =
826+
await getLambdaConfiguration(functionName);
827+
769828
if (!existingLayerVersionArn) {
770-
const config = await getLambdaConfiguration(functionName);
771829
const ddlEnvironmentVariables = getEnvironmentVariablesForDebugger({
772830
functionName,
773-
timeout: config.initialTimeout,
831+
timeout: initialTimeout,
774832
verbose: Configuration.config.verbose,
775833
initialExecWrapper:
776-
config.environmentVariables.AWS_LAMBDA_EXEC_WRAPPER !== lldWrapperPath
777-
? config.environmentVariables.AWS_LAMBDA_EXEC_WRAPPER
834+
environmentVariables.AWS_LAMBDA_EXEC_WRAPPER !== lldWrapperPath
835+
? environmentVariables.AWS_LAMBDA_EXEC_WRAPPER
778836
: undefined,
779837
});
780838

839+
Logger.verbose(
840+
`[Function ${functionName}] Layer does not exist at all, need to add it and attach to the function`,
841+
);
842+
781843
return {
782844
functionName,
783-
layers: config.otherLayerArns,
845+
layers: otherLayerArns,
784846
environmentVariables: {
785-
...config.environmentVariables,
847+
...environmentVariables,
786848
...ddlEnvironmentVariables,
787849
},
788-
timeout: Math.max(config.initialTimeout, 300),
850+
timeout: Math.max(initialTimeout, 300),
789851
};
790852
} else {
791-
const layerVersionArn = existingLayerVersionArn;
792-
793853
let needToUpdate: boolean = false;
794854

795-
const {
796-
environmentVariables,
797-
ddlLayerArns,
798-
otherLayerArns,
799-
initialTimeout,
800-
} = await getLambdaConfiguration(functionName);
801-
802855
// check if layer is already attached
803-
if (!ddlLayerArns?.find((arn) => arn === layerVersionArn)) {
856+
if (!ddlLayerArns?.find((arn) => arn === existingLayerVersionArn)) {
804857
needToUpdate = true;
805858
Logger.verbose(
806859
`[Function ${functionName}] Layer not attached to the function`,
@@ -812,9 +865,14 @@ async function analyzeLambdaAdd(
812865
}
813866

814867
// check if layers with the wrong version are attached
815-
if (!needToUpdate && ddlLayerArns.find((arn) => arn !== layerVersionArn)) {
868+
if (
869+
!needToUpdate &&
870+
ddlLayerArns.find((arn) => arn !== existingLayerVersionArn)
871+
) {
816872
needToUpdate = true;
817-
Logger.verbose('Layer with the wrong version attached to the function');
873+
Logger.verbose(
874+
`[Function ${functionName}] Layer with the wrong version attached to the function`,
875+
);
818876
}
819877

820878
// support for multiple internal Lambda extensions
@@ -850,7 +908,7 @@ async function analyzeLambdaAdd(
850908
return needToUpdate
851909
? {
852910
functionName,
853-
layers: [layerVersionArn, ...otherLayerArns],
911+
layers: [existingLayerVersionArn, ...otherLayerArns],
854912
environmentVariables: {
855913
...environmentVariables,
856914
...ddlEnvironmentVariables,
@@ -887,6 +945,10 @@ async function addPolicyToRole(roleName: string) {
887945
* @returns
888946
*/
889947
async function analyzeLambdaRoleAdd(functionName: string) {
948+
Logger.verbose(
949+
`[Function ${functionName}] Analyzing role for policy attachment`,
950+
);
951+
890952
const getFunctionResponse = await getLambdaClient().send(
891953
new GetFunctionCommand({
892954
FunctionName: functionName,
@@ -895,7 +957,7 @@ async function analyzeLambdaRoleAdd(functionName: string) {
895957
const roleArn = getFunctionResponse.Configuration?.Role;
896958
if (!roleArn) {
897959
throw new Error(
898-
`Failed to retrieve the role ARN for Lambda ${functionName}.`,
960+
`[Function ${functionName}] Failed to retrieve the role ARN.`,
899961
);
900962
}
901963

@@ -904,10 +966,12 @@ async function analyzeLambdaRoleAdd(functionName: string) {
904966

905967
if (!roleName) {
906968
throw new Error(
907-
`Failed to extract role name from role ARN: ${roleArn} for lambda ${functionName}.`,
969+
`[Function ${functionName}] Failed to extract role name from role ARN: ${roleArn}.`,
908970
);
909971
}
910972

973+
Logger.verbose(`[Function ${functionName}] Found role: ${roleName}`);
974+
911975
const existingPolicy = await createPolicyDocument(roleName);
912976

913977
let addPolicy: boolean = true;
@@ -919,10 +983,14 @@ async function analyzeLambdaRoleAdd(functionName: string) {
919983
`[Function ${functionName}] Policy already attached to the role ${roleName}`,
920984
);
921985
addPolicy = false;
986+
} else {
987+
Logger.verbose(
988+
`[Function ${functionName}] Different policy found on role ${roleName}, will update`,
989+
);
922990
}
923991
} else {
924992
Logger.verbose(
925-
`[Function ${functionName}] Policy not attached to the role ${roleName}`,
993+
`[Function ${functionName}] No policy found on role ${roleName}, will attach`,
926994
);
927995
}
928996
return { addPolicy, roleName };

0 commit comments

Comments
 (0)