CDK Lambda allows us to specify the files included in lambda through AssetOptions#exclude, which by default accepts an array of glob pattern. But if you want to specify directories in whitelist strategy (as in “exclude all files except this and that”), it’s bit tricky as of now (Jul 2021).

Given the files system below,

  .
  ├── index.ts
  ├── lib
  │   └── my_lib.ts
  ├── ignored_file
  └── ignored_dir
       └── whatever

“excluding all files except index.ts” can be done like this, which is fine:

  exclude: ['**', '!index.py']

However,

“excluding all files except index.ts and lib/*" needs to be specified exactly like the following:

  exclude: ['**', '!index.py', '!lib', '!lib/*']

Nothing like below works so far:

  // !!!these won't work!!!
  exclude: ['**', '!index.py', '!lib']
  exclude: ['**', '!index.py', '!lib/*']
  exclude: ['**', '!index.py', '!lib/', '!lib/*'] // '!lib/' with trailing slash won't work

This should be a CDK’s bug and will hopefully be fixed sometime.

related issues and comments: