Aws-cdk: Allow single files as Lambda code assets

Created on 9 Oct 2019  路  5Comments  路  Source: aws/aws-cdk

Currently, a Lambda code asset can either be a directory that is zipped and provided through the asset mechanism, or inline code that is limited to 4 kilobytes and supports only Python and Node.js. Inline code cannot be directly specified from a file, but it must first be read as string in the language runtime used, without the help of CDK. It would be nice to be able to specify a single file, that would automatically get added to a zip and be provided through the asset mechanism.

Use Case

Many Lambda functions are more complex than 4096 bytes, but still exist in a single file without any dependencies. It would be very convenient to be able to specify these as assets directly, instead of having to create multiple directories containing only that single file. Especially with layers, single file functions are very common.

Proposed Solution

Either allow the normal fromAsset function to automatically zip single files:

lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler.py'))

Or provide a separate function, such as fromSingleAsset or fromAssetZip. The current magic of directories being automatically zipped and single files being automatically used without transformation is a bit confusing. Could also consider creating fromAssetDir, fromAssetFile and fromAssetZip to make it very explicit what the intent is.

Other

  • [x] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

@aws-cdassets feature-request

Most helpful comment

I like the idea of deprecating fromAsset and making explicit sugar functions fromAssetDirectories(...dir: string[]), fromAssetFiles(...files: string[]) and fromAssetZipFile(zip: string).

All 5 comments

Hey @nakedible,

Thank you for submitting a request! This sounds like a reasonable feature to implement. Changing the functionality of fromAsset will likely introduce breaking changes, and therefore the path would likely be an additional function.

If you want to implement this, please feel free to put in a PR. Otherwise, someone will update this issue when any changes are made.

Since #4473 has landed, which has support for richer .dockerignore patterns, there is no need for this anymore. This is very simply achieved by:

      code: lambda.Code.fromAsset(".", {exclude: ["**", "!index.py"]}),

You can consider adding this example somewhere if you wish, or just closing this outright.

This is very cool! Thanks. Maybe we can add a little sugar that will make this easier to use.

I like the idea of deprecating fromAsset and making explicit sugar functions fromAssetDirectories(...dir: string[]), fromAssetFiles(...files: string[]) and fromAssetZipFile(zip: string).

Was this page helpful?
0 / 5 - 0 ratings