Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AWS Layers): Support references to external layers #9826

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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