From 678737607cea769109aa8315520a71bc47eb50ef Mon Sep 17 00:00:00 2001 From: Sarisia Date: Tue, 26 Apr 2022 13:22:35 +0900 Subject: [PATCH] fix(lambda-python): handler path is incorrectly generated when using PythonFunction (#20083) When using PythonFunction, current implementation generates `handler` value incorrectly. ## Code ```typescript new PythonFunction(stack, 'handler', { entry: 'test/lambda-handler-sub-nested', index: 'inner/inner2/custom_index.py', handler: 'custom_handler', runtime: Runtime.PYTHON_3_8, }); ``` ## Expected ```yaml Handler: inner.inner2.custom_index.custom_handler ``` ## Current Behaviour ```yaml Handler: inner.inner2/custom_index.custom_handler ``` This PR fix the issue by modifying string replace pattern. ---- ### 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* --- .../@aws-cdk/aws-lambda-python/lib/function.ts | 2 +- .../aws-lambda-python/test/function.test.ts | 17 +++++++++++++++++ .../inner/inner2/custom_index.py | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk/aws-lambda-python/test/lambda-handler-sub-nested/inner/inner2/custom_index.py diff --git a/packages/@aws-cdk/aws-lambda-python/lib/function.ts b/packages/@aws-cdk/aws-lambda-python/lib/function.ts index 15013622d2cbc..d82c2e210ecb6 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/function.ts @@ -68,7 +68,7 @@ export class PythonFunction extends Function { throw new Error(`Cannot find index file at ${resolvedIndex}`); } - const resolvedHandler =`${index.slice(0, -3)}.${handler}`.replace('/', '.'); + const resolvedHandler =`${index.slice(0, -3)}.${handler}`.replace(/\//g, '.'); if (props.runtime && props.runtime.family !== RuntimeFamily.PYTHON) { throw new Error('Only `PYTHON` runtimes are supported.'); diff --git a/packages/@aws-cdk/aws-lambda-python/test/function.test.ts b/packages/@aws-cdk/aws-lambda-python/test/function.test.ts index 6fd12540b78c3..41c4e4ad3d2d1 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/function.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/function.test.ts @@ -77,6 +77,23 @@ test('PythonFunction with index in a subdirectory', () => { }); }); +test('PythonFunction with index in a nested subdirectory', () => { + new PythonFunction(stack, 'handler', { + entry: 'test/lambda-handler-sub-nested', + index: 'inner/inner2/custom_index.py', + handler: 'custom_handler', + runtime: Runtime.PYTHON_3_8, + }); + + expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({ + entry: expect.stringMatching(/aws-lambda-python\/test\/lambda-handler-sub-nested$/), + })); + + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { + Handler: 'inner.inner2.custom_index.custom_handler', + }); +}); + test('throws when index is not py', () => { expect(() => new PythonFunction(stack, 'Fn', { entry: 'test/lambda-handler', diff --git a/packages/@aws-cdk/aws-lambda-python/test/lambda-handler-sub-nested/inner/inner2/custom_index.py b/packages/@aws-cdk/aws-lambda-python/test/lambda-handler-sub-nested/inner/inner2/custom_index.py new file mode 100644 index 0000000000000..d7b2ce8db00e9 --- /dev/null +++ b/packages/@aws-cdk/aws-lambda-python/test/lambda-handler-sub-nested/inner/inner2/custom_index.py @@ -0,0 +1,5 @@ +from http import HTTPStatus + +def handler(event, context): + print('No dependencies') + return HTTPStatus.OK.value