SAR-Lambda-Janitor icon indicating copy to clipboard operation
SAR-Lambda-Janitor copied to clipboard

feat: clean unused lambda layers

Open JonaC22 opened this issue 4 years ago • 6 comments

Hi,

I was using your serverless app and found it very helpful.

I think that it would be great to also clean up unused layer versions like it does with functions, hope you find this helpful too.

Thanks!

JonaC22 avatar Mar 09 '20 18:03 JonaC22

@JonaC22 thanks for doing this, let me take a look

theburningmonk avatar Mar 16 '20 19:03 theburningmonk

@JonaC22 hey, sorry it took me a bit longer to find time to actually go through your PR. I think it looks fine, but I'm wondering if it's enough to keep only layer versions that are referenced by a function alias - e.g. if we say we're gonna keep the X most recent versions of functions, then these functions can reference layers that are no longer referenced by a function alias and are therefore subject to deletion.

And there're some optimizations we can do, since aliases (apart from $LATEST) ultimately point to a version, so for each function we just need to check:

  • the versions we decide to keep
  • the $LATEST alias

to find all the layer versions that we need to keep, and delete the rest

theburningmonk avatar Mar 25 '20 19:03 theburningmonk

Hi @theburningmonk thank you for reviewing this.

Maybe the paremeter LayerVersionsToKeep should be set to 0 by default, it would delete all layer versions that are not referenced as you suggest, then leave the possibility to change that parameter in case someone need most recent versions too.

What do you think?

Thanks!

JonaC22 avatar Mar 25 '20 19:03 JonaC22

Why not keep LayerVersionsToKeep as it is - literally "we won't delete the most recent N versions", but older versions will be deleted if not referenced by a Lambda function version/$LATEST. That'll be consistent with how it handles Lambda function versions.

theburningmonk avatar Mar 26 '20 00:03 theburningmonk

@theburningmonk Ok, great! Sorry I misunderstood what you said. I will change the code to skip the lambda layers referenced by version instead of by alias

Thanks!

JonaC22 avatar Mar 28 '20 22:03 JonaC22

Hi @theburningmonk I've pushed the changes, can you please review it again? Thanks!

JonaC22 avatar Mar 31 '20 15:03 JonaC22