aws-sam-cli
aws-sam-cli copied to clipboard
Allow multiple CodeUris for a function
Most of my SAM applications have a handful of functions and overall contain a small amount of code. I have started putting that code all together and deploying it to each function (that is, all functions use the same CodeUri), distinguishing code paths between functions by different handlers in the code. I opened #1236 because the SAM CLI should build the code once and copy the built code for every function.
I'd like to go beyond this "one zip file per application" approach by having some separate code per function, but also allowing a lightweight way of sharing some code between functions in the same application (i.e., template). Layers seem like the wrong abstraction for this; they are useful when the lifecycle of the code they contain is independent of the lifecycle of the code in the functions that use them, but in this case, the lifecycle of the function code and the shared code is one and the same.
How I'd like to enable this code sharing is by having multiple CodeUris in my SAM template. sam build
would produce the zip as the overlay of the results, so the packaged template would still end up with a single (S3) CodeUri. I'm not sure whether I want the build step to happen separately or after the overlay; my primary goal in choosing one or the other would be reducing build times.
Using this functionality, when building my application, I'd have directories for each of my functions, and a shared directory (again, these are all tightly coupled). Developing in Python, I'd add these directories my PYTHONPATH
, so in development my IDE would still understand the code correctly even though it is laid out differently than in the zip file. I would be responsible for making sure one directory wasn't clobbering another in the overlay process.
With new image based Packagetypes, you could specify multiple handlers per function and all the code is present in the same image too. Obviously it does not solve the original usecase, but wanted to document this as another approach that can be taken.
This would be great! we currently bundle src/shared
into node_modules
w Architect and I could remove some code if SAM supported this sort of flow.
Here's how we do it to ensure there's no clobbering: https://arc.codes/docs/en/guides/developer-experience/sharing-code (and no we don't wan to deal with layers, layer versioning and the coldstart, etc to achieve this)
I have encountered this use case and I believe this would be a very useful feature Currently I want to have shared code between my lambdas. To share that code between all lambdas, I have to package all lambda code together and call different handlers. Not ideal but not talking huge sizes since they are python lambdas but this proposed solution would be so much tidier