diff --git a/packages/@aws-cdk/aws-lambda/lib/function.ts b/packages/@aws-cdk/aws-lambda/lib/function.ts index 8588aff8d9963..ba6ddcf414f82 100644 --- a/packages/@aws-cdk/aws-lambda/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda/lib/function.ts @@ -668,6 +668,12 @@ export class Function extends FunctionBase { } } + if (props.description && !Token.isUnresolved(props.description)) { + if (props.description.length > 256) { + throw new Error(`Function description can not be longer than 256 characters but has ${props.description.length} characters.`); + } + } + const managedPolicies = new Array(); // the arn is in the form of - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole diff --git a/packages/@aws-cdk/aws-lambda/test/function.test.ts b/packages/@aws-cdk/aws-lambda/test/function.test.ts index 78f17b5f55394..187fa061b2e19 100644 --- a/packages/@aws-cdk/aws-lambda/test/function.test.ts +++ b/packages/@aws-cdk/aws-lambda/test/function.test.ts @@ -2648,6 +2648,31 @@ describe('function', () => { }).not.toThrow(); }); + test('Error when function description is longer than 256 chars', () => { + const stack = new cdk.Stack(); + expect(() => new lambda.Function(stack, 'MyFunction', { + code: lambda.Code.fromInline('foo'), + runtime: lambda.Runtime.NODEJS_14_X, + handler: 'index.handler', + description: 'a'.repeat(257), + })).toThrow(/Function description can not be longer than 256 characters/); + }); + + test('No error when function name is Tokenized and Unresolved', () => { + const stack = new cdk.Stack(); + expect(() => { + const realFunctionDescription = 'a'.repeat(257); + const tokenizedFunctionDescription = cdk.Token.asString(new cdk.Intrinsic(realFunctionDescription)); + + new lambda.Function(stack, 'foo', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_14_X, + description: tokenizedFunctionDescription, + }); + }).not.toThrow(); + }); + describe('FunctionUrl', () => { test('addFunctionUrl creates a function url with default options', () => { // GIVEN