diff --git a/packages/aws-cdk-lib/aws-lambda/lib/layers.ts b/packages/aws-cdk-lib/aws-lambda/lib/layers.ts index eb3b5d040e862..585628ac66f82 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/layers.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/layers.ts @@ -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[]; @@ -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, diff --git a/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts b/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts index 47386596cebe2..35946e669566c 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/layers.test.ts @@ -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'); + }); });