Skip to content

Commit

Permalink
fix(AWS Layers): Support references to external layers
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Aug 11, 2021
1 parent d53c80d commit ca0656f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'AWS Kafka',
'AWS Kinesis',
'AWS Lambda',
'AWS Layers',
'AWS Local Invocation',
'AWS MSK',
'AWS S3',
Expand Down
10 changes: 6 additions & 4 deletions lib/plugins/aws/package/compile/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const crypto = require('crypto');
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const log = require('@serverless/utils/log');
const ServerlessError = require('../../../../serverless-error');
const deepSortObjectByKey = require('../../../../utils/deepSortObjectByKey');
const getHashForFilePath = require('../lib/getHashForFilePath');
Expand Down Expand Up @@ -735,11 +736,12 @@ function extractLayerConfigurationsFromFunction(functionProperties, cfTemplate)
functionProperties.Layers.forEach((potentialLocalLayerObject) => {
if (potentialLocalLayerObject.Ref) {
const configuration = cfTemplate.Resources[potentialLocalLayerObject.Ref];

if (!configuration) {
throw new ServerlessError(
`Could not find reference to layer: ${potentialLocalLayerObject.Ref}. Ensure that you are referencing layer defined in your service.`,
'LAMBDA_LAYER_REFERENCE_NOT_FOUND'
);
if (process.env.SLS_DEBUG) {
log(`Could not find reference to layer: ${potentialLocalLayerObject.Ref}.`);
}
return;
}

layerConfigurations.push({
Expand Down
40 changes: 28 additions & 12 deletions test/unit/lib/plugins/aws/package/compile/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2134,21 +2134,37 @@ describe('lib/plugins/aws/package/compile/functions/index.test.js', () => {
// https://github.com/serverless/serverless/blob/d8527d8b57e7e5f0b94ba704d9f53adb34298d99/lib/plugins/aws/package/compile/functions/index.test.js#L2325-L2362
});

it('should throw an error when nonexistent layer is referenced', async () => {
await expect(
runServerless({
fixture: 'functionDestinations',
command: 'package',
configExt: {
functions: {
fnInvalidLayer: {
handler: 'target.handler',
layers: [{ Ref: 'NonexistentLambdaLayer' }],
it('should support `Ref` references to external layers (not defined as a part of `layers` top-level property in configuration)', async () => {
const { cfTemplate, awsNaming } = await runServerless({
fixture: 'functionDestinations',
command: 'package',
configExt: {
functions: {
fnExternalLayer: {
handler: 'target.handler',
layers: [{ Ref: 'ExternalLambdaLayer' }],
},
},
resources: {
Resources: {
ExternalLambdaLayer: {
Type: 'AWS::Lambda::LayerVersion',
Properties: {
CompatibleRuntimes: ['nodejs12.x'],
Content: {
S3Bucket: 'bucket',
S3Key: 'key',
},
LayerName: 'externalLayer',
},
},
},
},
})
).to.be.eventually.rejected.and.have.property('code', 'LAMBDA_LAYER_REFERENCE_NOT_FOUND');
},
});
expect(
cfTemplate.Resources[awsNaming.getLambdaLogicalId('fnExternalLayer')].Properties.Layers
).to.deep.equal([{ Ref: 'ExternalLambdaLayer' }]);
});

it.skip('TODO: should support `functions[].conditions`', () => {
Expand Down

0 comments on commit ca0656f

Please sign in to comment.