Skip to content
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.
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 @@ -79,7 +79,7 @@ export interface ILayerVersion extends IResource, ILayerVersionRef {
/**
* 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 @@ -219,7 +219,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
30 changes: 30 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,34 @@ describe('layers', () => {
CompatibleArchitectures: ['arm64'],
});
});

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,
});

// 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