Serverless-application-model: Add possibility for to exclude not needed files from deployment package

Created on 19 Feb 2018  路  4Comments  路  Source: aws/serverless-application-model

Hello here is my project structure:

    -Common
        -common.js //Global module which i'm using in all functions
    -Func1
        -index.js
    -Func2
        -index.js
    -template.yaml

And here is template.yaml content:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
      Func1:
        Type: 'AWS::Serverless::Function'
        Properties:
          Handler: Func1/index.handler
          Runtime: nodejs6.10
          MemorySize: 512
          Timeout: 10
      Func2:
        Type: 'AWS::Serverless::Function'
        Properties:
          Handler: Func2/index.handler
          Runtime: nodejs6.10
          MemorySize: 512
          Timeout: 10

When i deploy for example Func2, result package contain all folders inside application, instead only Func2. Is it possible to configure through yaml file, what files will included in result package?
For example if i deploy Func2 i want to see in package next:

    -common.js
-Func2
    -index.js

Most helpful comment

FYI for anyone that comes across this. The new sam build command uses npm to package up the build folder (for nodejs projects), so you can use .npmignore to tell it which files should be excluded from the build.

All 4 comments

The local package logic actually exists in the AWS CLI (aws cloudformation package). You can open a ticket there, but they're unlikely to be able to help. Your Func2 needs to know about Common, so they must both exist in the ZIP. If you really want lightweight bundles, try something like WebPack for Node.js

FYI for anyone that comes across this. The new sam build command uses npm to package up the build folder (for nodejs projects), so you can use .npmignore to tell it which files should be excluded from the build.

In addition to the above .npmignore tip, file property of package.json also works with sam build. .npmignore is a blacklist, files property is a whitelist.

https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

If, given the structure of your project, you find .npmignore to be a maintenance headache, you might instead try populating the files property of package.json, which is an array of file or directory names that should be included in your package. Sometimes a whitelist is easier to manage than a blacklist.

@talawahtech I tried this, but I can still see the files inside the ./aws-sam/build folder after sam build has finished. Does this not work locally or am I missing something?

Was this page helpful?
0 / 5 - 0 ratings