From de027e28ce5c95e70fed8874e6531eabba24521c Mon Sep 17 00:00:00 2001 From: Gtofig Date: Tue, 24 May 2022 15:56:04 +0100 Subject: [PATCH] feat(lambda): validate function description length (#20476) closes #20475. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-lambda/lib/function.ts | 6 +++++ .../@aws-cdk/aws-lambda/test/function.test.ts | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) 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