How to Ignore directories and files for a deployment


#1

Hi!

Problem: my zipped deployment folder that contains my python code is too big!
I looked inside and there’s stuff in there that should be ignored according to my .gitignore

Here is my .gitignore

image

Here is the contents of my .gitignore

Here is the contents of the (too-big) zipped folder that is deployed to S3
(I DL it from S3 and unzipped the contents)

image

I should note that my service project is nested one level below another service project, like so…
-root
—main.project.with.own.deployment
------my.project.with.big.zip
---------.gitignore
---------handler.py
--------etc…

This is the command I’m using to deploy

sudo sls deploy -f numpy

Thanks!


#2

Serverless does not pay attention to .gitignore. You have a few options for you to take that I use myself.

  1. You can add options to the serverless.yml to indicate what to exclude and what to include. You can read about that at https://serverless.com/framework/docs/providers/aws/guide/packaging/

  2. What I do most recently is use gulp to build a deployment folder containing all my files/folders. Then within that folder is what I actually deploy. This allows me to have more processes in place for CI/CD.


#3

Perfect. Thanks so much!


#4

We a single serverless.yml that contains a bunch of resources and 4 python lambdas. Each lambda needed a different set of python libraries and pre-compiled binaries. I found the best way to hald this was to “package individually” and exclude everything:

package:
  individually: true
  exclude:
    - ".*/**"

Then in each lambda, include only the binaries/libs I needed. e.g.,

functions:
  DocPdfSplit:
    handler: doc_pdf_split.s3created
    package:
      include:
        - doc_pdf_split.py
        - lib.py
        - ghostscript-9.20/**
    events:
      - s3:
          bucket: ${self:custom.s3_name}
          event: s3:ObjectCreated:*

and

  PageTxtCombine:
    handler: page_txt_combine.s3created
    package:
      include:
        - page_txt_combine.py
        - lib.py
        - elasticsearch/**
        - urllib3/**
    events:
      - s3:
          bucket: ${self:custom.s3_name}
          event: s3:ObjectCreated:*