I am using a serverless app to handle my api call
I am using google.auth.GoogleAuth
and google.androidpublisher
from googleapis
package
There is a limitation in the code size which can be uploaded to lambda and googleapis
is 115 MB
, this is huge !!!
I found an article telling that calling packages like that const {androidpublisher_v3} = require('googleapis/build/src/apis/androidpublisher')
will reduce the package size but no.
Running sls package
I always have 115MB of googleapis
Is there any way to reduce that ? I was thinking to code the calls by myself but this is a few hours of work
Take a look at https://github.com/floydspace/serverless-esbuild. Once setup, it will do the tree-shaking to reduce your code size by including only the code your Lambda actually imports.
So if you mention const {androidpublisher_v3} = require('googleapis/build/src/apis/androidpublisher')
in your code, esbuild
will include only the parts of the googleapis
package that you need for the AndroiPublisher
module.
Pros: universal way, automatic;
Cons: some packages with native code might break and require exclusion; slower
node_modules
When you use yarn package manager instead of npm, you can provide an exclusion list of paths you want to clean up automatically from your node_modules
during installation.
I wrote an in-depth article about this topic: https://itnext.io/3x-smaller-lambda-artifacts-by-removing-junk-from-node-modules-2b50780ca1f5
In essence, you create a .yarnclean
file in your repository with the following content:
**/googleapis/build/src/apis/compute
**/googleapis/build/src/apis/dfareporting
**/googleapis/build/src/apis/displayvideo
**/googleapis/build/src/apis/healthcare
**/googleapis/build/src/apis/dialogflow
**/googleapis/build/src/apis/retail
**/googleapis/build/src/apis/securitycenter
# ... more rules to follow
Continue the list of folders that you don't need in your Lambda.
Specifically for googleapis
package, ~70% of the artifact size could be removed, by removing typings from the package.
You need TypeScript types only during development, but not in Lambda runtime.
So you can add this code to your CI pipeline before making an artifact.
I was able to reduce the size of the code from 111 MB to 20 MB just by executing the following snippet alone:
npx del-cli \
"node_modules/**/@types/**" \
"node_modules/**/*.d.ts" \
"node_modules/**/.yarn-integrity" \
"node_modules/**/.bin"
Hope it helps!