1.1.0 • Published 2 years ago
@apimda/npm-layer-version v1.1.0
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.jsonCreating 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
LayerVersionconstruct:
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 NodejsFunctions:
layerVersion: the underlying CDKLayerVersionrepresenting 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.