You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have an API Gateway SAM project that has handlers running on both nodejs18.x and python3.10 runtimes. Most of our NodeJS handlers use the same code and differ only in the entrypoint (handler). In order to keep the deployment package small, we use a layer for all of our declared dependencies. The code layout looks like this:
.
├── nodejs
│ ├── dependencies
│ │ └── package.json <-- all runtime dependencies under "dependencies"
│ │ └── package-lock.json
│ └── src
│ └── package.json <-- empty "dependencies"; "devDependencies" is a copy of dependencies/package.json#dependencies
│ ├── package-lock.json
│ └── handlers
│ └── my-handler.js
└── python
├── dependencies
│ └── requirements.txt <-- all dependencies
└── src
└── requirements.txt <-- duplicate of dependencies/requirements.txt
└── handlers
└── my-other-handler.py
In the template, we have AWS::Serverless::LayerVersion resources pointing at nodejs/dependencies and python/dependencies with the appropriate build methods, and each AWS::Serverless::Function's resource includes the appropriate layer.
This works automatically for the NodeJS stuff – devDependencies install locally but are left out of the build, leaving the layer to make it work.
I have to use a little trickery to get the Python function to work propertly – I use the nodejs18.x build method for the function, so that the build doesn't try to pip install. (I could also use makefile and write a Makefile that does a straight copy of the source directory to the build target directory, but this is easier.)
What I'm wondering is if there's an easier way to accomplish this, or whether such a thing would be a good feature to add to the roadmap. Basically a metadata property of the AWS::Serverless::Function resource that would tell it, “use the builder to create the dependencies, but then move the installed dependencies (the node_modules or python directory or whatever, depending on the builder) to a layer and attach that layer to this function on deploy.” It would certainly make our increasingly complicated template more maintainable.
I get the feeling that sam sync is already doing something like that under the hood with its AwsSamAutoDependencyLayerNestedStack but I can't quite tell.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
We have an API Gateway SAM project that has handlers running on both
nodejs18.xandpython3.10runtimes. Most of our NodeJS handlers use the same code and differ only in the entrypoint (handler). In order to keep the deployment package small, we use a layer for all of our declared dependencies. The code layout looks like this:In the template, we have
AWS::Serverless::LayerVersionresources pointing atnodejs/dependenciesandpython/dependencieswith the appropriate build methods, and eachAWS::Serverless::Function's resource includes the appropriate layer.This works automatically for the NodeJS stuff –
devDependenciesinstall locally but are left out of the build, leaving the layer to make it work.I have to use a little trickery to get the Python function to work propertly – I use the
nodejs18.xbuild method for the function, so that the build doesn't try topip install. (I could also usemakefileand write a Makefile that does a straight copy of the source directory to the build target directory, but this is easier.)What I'm wondering is if there's an easier way to accomplish this, or whether such a thing would be a good feature to add to the roadmap. Basically a metadata property of the
AWS::Serverless::Functionresource that would tell it, “use the builder to create the dependencies, but then move the installed dependencies (thenode_modulesorpythondirectory or whatever, depending on the builder) to a layer and attach that layer to this function on deploy.” It would certainly make our increasingly complicated template more maintainable.I get the feeling that
sam syncis already doing something like that under the hood with itsAwsSamAutoDependencyLayerNestedStackbut I can't quite tell.Beta Was this translation helpful? Give feedback.
All reactions