Skip to content

Commit de027e2

Browse files
authored
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*
1 parent 85f4e29 commit de027e2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/@aws-cdk/aws-lambda/lib/function.ts

+6
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,12 @@ export class Function extends FunctionBase {
668668
}
669669
}
670670

671+
if (props.description && !Token.isUnresolved(props.description)) {
672+
if (props.description.length > 256) {
673+
throw new Error(`Function description can not be longer than 256 characters but has ${props.description.length} characters.`);
674+
}
675+
}
676+
671677
const managedPolicies = new Array<iam.IManagedPolicy>();
672678

673679
// the arn is in the form of - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

packages/@aws-cdk/aws-lambda/test/function.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,31 @@ describe('function', () => {
26482648
}).not.toThrow();
26492649
});
26502650

2651+
test('Error when function description is longer than 256 chars', () => {
2652+
const stack = new cdk.Stack();
2653+
expect(() => new lambda.Function(stack, 'MyFunction', {
2654+
code: lambda.Code.fromInline('foo'),
2655+
runtime: lambda.Runtime.NODEJS_14_X,
2656+
handler: 'index.handler',
2657+
description: 'a'.repeat(257),
2658+
})).toThrow(/Function description can not be longer than 256 characters/);
2659+
});
2660+
2661+
test('No error when function name is Tokenized and Unresolved', () => {
2662+
const stack = new cdk.Stack();
2663+
expect(() => {
2664+
const realFunctionDescription = 'a'.repeat(257);
2665+
const tokenizedFunctionDescription = cdk.Token.asString(new cdk.Intrinsic(realFunctionDescription));
2666+
2667+
new lambda.Function(stack, 'foo', {
2668+
code: new lambda.InlineCode('foo'),
2669+
handler: 'index.handler',
2670+
runtime: lambda.Runtime.NODEJS_14_X,
2671+
description: tokenizedFunctionDescription,
2672+
});
2673+
}).not.toThrow();
2674+
});
2675+
26512676
describe('FunctionUrl', () => {
26522677
test('addFunctionUrl creates a function url with default options', () => {
26532678
// GIVEN

0 commit comments

Comments
 (0)