Functions are bloated
See original GitHub issueDescription:
Runing sam build
produces .aws-sam/build
which has pretty much everything in it, and when you do sam deploy
it deploys the entire source code into the function itself, resulting in a bloated function and slow deployments.
I got around the bloated sam build
by writing my own builder, I learned that sam build
is actually not required by sam deploy
as long as the template.yaml
and samconfig.toml
are on the same directory as the source codes.
My folder architecture looks like this:
/ root
| - ...lots of other unrelated files
| - src
| | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
| | - functions <---- this is where all my functions are
| | | - ...sub-folders containing all functions
Before:
I run the command sam build && sam deploy
to deploy, after sam build
my tree would look like this:
/ root
| - ...lots of other unrelated files
| - .aws-sam
| | - build
| | | - login <---- this is one of my functions defined in template.yaml
| | | | - login.js
| | | | - ... all other things that the function does not really need <--- this is the problem
| | | | - dependencies <---- even this layer was somehow added here
| | - build.toml
| - src
| | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
| | - functions <---- this is where all my functions are
| | | - ...sub-folders containing all functions
Now I don’t run sam build
anymore, I run my own build and the build now looks like this:
/ root
| - ...lots of other unrelated files
| - build
| | - login.js
| | - ...allOthers.js <---- all of them are just JS files
| | - dependencies <---- the layer
| | - template.yaml
| - src
| | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
| | - functions <---- this is where all my functions are
| | | - ...sub-folders containing all functions
Now I jus do cd build && sam deploy
.
The catch is, It’s still bloated because sam will package the entire build/
folder and make that the base of the function, so when I download the login function, it would look like this:
/ root
| - login.js
| - ...allOthers.js <---- all of them are just JS files
| - dependencies <---- the layer
| - template.yaml
When in reality the function is just the JS code itself, like login.js
, and nothing else, so it SHOULD look like this:
/ root
| - login.js
| - template.yaml
FYI: The layer is a typical NodeJS layer.
/ dependencies
| - nodejs
| | - package.json
| | - node_modules
| | - yarn.lock
| | - ...all other utilities being used by the functions themselves
The convention is that the functions are self-contained, any dependencies they need MUST BE pull from the dependencies
layer, hence the name.
The deployment time was also reduced by half, what was previously 10 minutes, is now just 5 minutes.
Steps to reproduce:
sam build
sam deploy
- Go to the function and download it, you’ll see that it contains the entire project itself, or whatever is in the
files
ofpackage.json
.
Observed result:
Functions are bloated.
Expected result:
Functions SHOULD NOT BE BLOATED. It should only contain the actual function itself, i.e., login.js
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: N/A
sam --version
: SAM CLI, version 1.17.0- AWS region: N/A
Add --debug flag to command you are running
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (1 by maintainers)
You either can do it manually with Makefile-based builder:
or you can use TypeScript feature to transpile every handler separately and TypeScript will build only this file and files on which it depends (imports into itself). See “Bonus: Put only relevant code into your Lambdas functions” section at the end of my article for details on how to do that.
Wow, great! I’m super glad about such an improvement (we have much smaller set of functions).
However, there is still plenty of space for improvement. For example, current Makefile isn’t optimal and have a lot of duuplications (e.g.
npm install
is repeated for every function to install development dependencies).If you have any ideas on how to improve it further, please open issue or pull request in my example app or my cookiecutter template.