Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/aws-cdk-lib/aws-lambda/lib/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface ILayerVersion extends IResource {
/**
* The runtimes compatible with this Layer.
*
* @default Runtime.All
* @default - All supported runtimes. Setting this to Runtime.ALL is equivalent to leaving it undefined.
*/
readonly compatibleRuntimes?: Runtime[];

Expand Down Expand Up @@ -208,7 +208,9 @@ export class LayerVersion extends LayerVersionBase {
}

const resource: CfnLayerVersion = new CfnLayerVersion(this, 'Resource', {
compatibleRuntimes: props.compatibleRuntimes && props.compatibleRuntimes.map(r => r.name),
compatibleRuntimes: (props.compatibleRuntimes === Runtime.ALL)
? undefined
: props.compatibleRuntimes?.map(r => r.name),
compatibleArchitectures: props.compatibleArchitectures?.map(a => a.name),
content: {
s3Bucket: code.s3Location.bucketName,
Expand Down
54 changes: 54 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/layers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,58 @@ describe('layers', () => {
CompatibleArchitectures: ['arm64'],
});
});

test('Runtime.ALL is handled correctly by omitting compatibleRuntimes', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 tests that we are adding are a duplication of code, since they are identical but with just changing the compatibleRuntimes value. I'd suggest we better do something like this to avoid the duplication of code:

test.each([
  ['Runtime.ALL is handled correctly', { compatibleRuntimes: lambda.Runtime.ALL }],
  ['undefined compatibleRuntimes is handled correctly', {}]
])('%s by omitting compatibleRuntimes', (_, props) => {
  // GIVEN
  const stack = new cdk.Stack();
  const bucket = new s3.Bucket(stack, 'Bucket');
  const code = new lambda.S3Code(bucket, 'ObjectKey');

  // WHEN
  new lambda.LayerVersion(stack, 'LayerVersion', {
    code,
    ...props
  });

// the remaining stays the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All addressed. Thank you

// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
const code = new lambda.S3Code(bucket, 'ObjectKey');

// WHEN
new lambda.LayerVersion(stack, 'LayerVersion', {
code,
compatibleRuntimes: lambda.Runtime.ALL,
});

// THEN - CompatibleRuntimes should be omitted from CloudFormation template
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::Lambda::LayerVersion', {
Content: {
S3Bucket: stack.resolve(bucket.bucketName),
S3Key: 'ObjectKey',
},
});

// Verify that CompatibleRuntimes is NOT present in the template
const resources = template.findResources('AWS::Lambda::LayerVersion');
const layerResource = Object.values(resources)[0];
expect(layerResource.Properties).not.toHaveProperty('CompatibleRuntimes');
});

test('undefined compatibleRuntimes is handled correctly by omitting compatibleRuntimes', () => {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
const code = new lambda.S3Code(bucket, 'ObjectKey');

// WHEN
new lambda.LayerVersion(stack, 'LayerVersion', {
code,
// compatibleRuntimes is undefined (default behavior)
});

// THEN - CompatibleRuntimes should be omitted from CloudFormation template
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::Lambda::LayerVersion', {
Content: {
S3Bucket: stack.resolve(bucket.bucketName),
S3Key: 'ObjectKey',
},
});

// Verify that CompatibleRuntimes is NOT present in the template
const resources = template.findResources('AWS::Lambda::LayerVersion');
const layerResource = Object.values(resources)[0];
expect(layerResource.Properties).not.toHaveProperty('CompatibleRuntimes');
});
});
Loading