Skip to content

Commit

Permalink
pr feedback and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc committed Feb 17, 2022
1 parent 7a1cab6 commit f9d48eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
26 changes: 9 additions & 17 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ export class Function extends FunctionBase {
physicalName: props.functionName,
});

if (props.functionName && !Token.isUnresolved(props.functionName)) {
if (props.functionName.length > 64) {
throw new Error(`Function name can not be longer than 64 characters but has ${props.functionName.length} characters.`);
}
if (!/^[a-zA-Z0-9-_]+$/.test(props.functionName)) {
throw new Error(`Function name ${props.functionName} can contain only letters, numbers, hyphens, or underscores with no spaces.`);
}
}

const managedPolicies = new Array<iam.IManagedPolicy>();

// the arn is in the form of - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Expand Down Expand Up @@ -690,15 +699,6 @@ export class Function extends FunctionBase {
}
this._architecture = props.architecture ?? (props.architectures && props.architectures[0]);

if (props.functionName && !Token.isUnresolved(props.functionName)) {
if (props.functionName.length > 140) {
throw new Error('Function name can not be longer than 140 characters.');
}
if (!this.validateFunctionName(props.functionName)) {
throw new Error('Function name can contain only letters, numbers, hyphens, or underscores with no spaces.');
}
}

const resource: CfnFunction = new CfnFunction(this, 'Resource', {
functionName: this.physicalName,
description: props.description,
Expand Down Expand Up @@ -1093,14 +1093,6 @@ Environment variables can be marked for removal when used in Lambda@Edge by sett
throw new Error('AWS_CODEGURU_PROFILER_GROUP_ARN and AWS_CODEGURU_PROFILER_ENABLED must not be set when profiling options enabled');
}
}

/**
* Validate if the string contains only letters, numbers, hyphens, underscore but no spaces
*/
private validateFunctionName(functionName: string) {
const regexp = /^[a-zA-Z0-9-_]+$/;
return functionName.search(regexp) !== -1;
}
}

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2280,14 +2280,14 @@ describe('function', () => {
expect(fn.architecture?.name).toEqual('arm64');
});

test('Error when function name is longer than 140 chars', () => {
test('Error when function name is longer than 64 chars', () => {
const stack = new cdk.Stack();
expect(() => new lambda.Function(stack, 'MyFunction', {
code: lambda.Code.fromInline('foo'),
runtime: lambda.Runtime.NODEJS_12_X,
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
functionName: 'a'.repeat(141),
})).toThrow(/Function name can not be longer than 140 characters./);
functionName: 'a'.repeat(65),
})).toThrow(/Function name can not be longer than 64 characters/);
});

test('Error when function name contains invalid characters', () => {
Expand All @@ -2297,10 +2297,10 @@ describe('function', () => {
new lambda.Function(stack, `foo${invalidChar}`, {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
runtime: lambda.Runtime.NODEJS_14_X,
functionName: `foo${invalidChar}`,
});
}).toThrow(/Function name can contain only letters, numbers, hyphens, or underscores with no spaces./);
}).toThrow(/can contain only letters, numbers, hyphens, or underscores with no spaces./);
});
});

Expand All @@ -2313,7 +2313,7 @@ describe('function', () => {
new lambda.Function(stack, 'foo', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
runtime: lambda.Runtime.NODEJS_14_X,
functionName: tokenizedFunctionName,
});
}).not.toThrow();
Expand Down

0 comments on commit f9d48eb

Please sign in to comment.