Skip to content

Commit

Permalink
fix(lambda-python): handler path is incorrectly generated when using …
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
sarisia authored Apr 26, 2022
1 parent e9670c8 commit 6787376
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-python/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from http import HTTPStatus

def handler(event, context):
print('No dependencies')
return HTTPStatus.OK.value

0 comments on commit 6787376

Please sign in to comment.