Michael Lorton
Michael Lorton

Reputation: 44376

My Serverless deploy package is over 100 MB and the SLS command refuses to deploy it

Although my app is fairly simple, it was deploying at 107Mb gzipped, and then I made some change that made it so large it wouldn’t deploy at all. What do I do?

Upvotes: 1

Views: 2336

Answers (1)

Michael Lorton
Michael Lorton

Reputation: 44376

1. Reduce the size of your node_modules directory

Anything you don’t need — goes. Delete your node_modules/ directory and reinstall it to be sure it’s clean.

2. Really distinguish between dependencies and dev-dependencies

Anything you don’t need a run-time, mark as a dev-dependency, and put

package:
  excludeDevDependencies: true

in your serverless.yml. Things you don’t need a run-time include compilers, type-libraries, and... Node.

3. Wait, Node?

Yeah, if you are like me, and prefer to develop in Node/Express and then deploy to Serverless, remember that Lambda runs its own version on Node, so your Node is a dev-dependency. Ditto for things that only Node uses, like ts-node and body-parser.

4. Include rather than exclude

The serverless.yml should include the stuff you need, not exclude the stuff you don’t. I write in Typescript (and you should too) and set my tsconfig.json to write everything to build/, so my entire package statement in serverless.yml is

package:
  excludeDevDependencies: true
  include:
    - build/**

Don’t add node_modules/** — the deployment process does that automatically and if you include it explicitly, you will defeat the excludeDevDependencies setting.

5. Check your work

You can practice by doing the following:

sls package
du -m -d 1 .serverless

The first statement quickly makes up the package, and then stops; the second lists the megabytes used by each artifact. If there is a .zip file that looks too big, unzip it and then use du more to investigate what is taking up all the room.

By using these techniques, I got the 107Mb package down to 10Mb.

Upvotes: 3

Related Questions