@apimda/npm-layer-version
v1.1.0
Published
[![version](https://img.shields.io/npm/v/@apimda/npm-layer-version.svg?style=flat-square)](https://www.npmjs.com/package/@apimda/npm-layer-version)
Downloads
7
Readme
NpmLayerVersion
NpmLayerVersion
is a CDK construct to create a custom lambda layer from an NPM package.json file.
Usage
Add the packages to be included in a package.json
, and the construct will make sure they're up-to-date before every deployment (i.e. npm install
), and generate the LayerVersion to use in your CDK stacks.
Directory Structure
NpmLayerVersion
requires the following directory structure, as specified by AWS Lambda layer path configuration
<root>
|- <custom code>
|- nodejs
|- package.json
|- package-lock.json
Creating an NpmLayerVersion
You can create an NpmLayerVersion
with the following NpmLayerVersionProps
:
- Path to the directory structure of the layer, relative to your
tsconfig.json
- Custom LayerVersionProps to pass to the underlying CDK
LayerVersion
construct:
const layer = new NpmLayerVersion(this, 'DependencyLayer', {
layerPath: 'src/deploy/layer',
layerVersionProps: {
removalPolicy: UserApiStack.removalPolicy,
compatibleArchitectures: [lambda.Architecture.ARM_64],
compatibleRuntimes: [lambda.Runtime.NODEJS_16_X]
}
});
Using with NodejsFunction
NpmLayerVersion
provides two properties to be used when creating NodejsFunction
s:
layerVersion
: the underlying CDKLayerVersion
representing the lambda layer itselfpackagedDependencies
: list of dependencies that were packaged.
The example below shows how to use this with NodejsFunctionProps:
const lambdaProps: NodejsFunctionProps = {
architecture: lambda.Architecture.ARM_64,
runtime: lambda.Runtime.NODEJS_16_X,
bundling: {
minify: false,
target: 'node16',
externalModules: layer.packagedDependencies // don't bundle layer dependencies in lambda
},
layers: [layer.layerVersion] // use lambda layer
};
Contributing
We're happy to accept contributions! Please open an issue before sending a PR to discuss proposed changes.