question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-lambda-python] Unify `entry` field betwen aws-lambda-nodejs and aws-lambda-python

See original GitHub issue

I love the new PythonFunction support in aws-cdk! I did, however, notice a difference between how the entry property works between aws-lambda-nodejs and aws-lambda-python.

In aws-lambda-nodejs, the entry field is a fully qualified path to the index file. From the docs:

https://github.com/aws/aws-cdk/blob/9fbeec3d7fe73ec870fe2de0e122b7714165f70e/packages/%40aws-cdk/aws-lambda-nodejs/README.md#L34-L40

However, the entry and index are broken into two distinct properties in aws-lambda-python. From the docs:

https://github.com/aws/aws-cdk/blob/9fbeec3d7fe73ec870fe2de0e122b7714165f70e/packages/%40aws-cdk/aws-lambda-python/README.md#L19-L22

So, when I was converting a function over to PythonFunction, I fully specified the entry, like I would with NodejsFunction:

new PythonFunction(this, 'MyFunc', {
  entry: path.join(__dirname, 'lambda', 'my_lambda.py')
}

But I received this error, which surprised me.

Cannot find index file at /Users/blimmer/my-project/foo/src/my-func/lambda/my_lambda.py/index.py

I fixed the problem by using entry and index like this:

new PythonFunction(this, 'MyFunc', {
  entry: path.join(__dirname, 'lambda'),
  index: 'my_lambda.py',
}

However, I believe that entry should work the same between python and nodejs for consistency.

Use Case

Unifying the entry property between PythonFunction and NodejsFunction will make it easier for developers to define both types of functions.

Proposed Solution

I propose that index be removed from PythonFunction and for entry to be the fully qualified path to the index file, like it is in NodejsFunction.

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
ztanecommented, Mar 10, 2021

Please do not unify anything. In fact the current implementation does not work with all Python lambda code where it would make sense. In Python lambda it would make sense to have only the current meaning of entry and the handler parameter with index gone altogether.

How the AWS Lambda Runtime for Python is realized is that it will unpack the source files into a directory that is in Python module search path. One specifies the handler in format foo.bar.baz. The Lambda runtime will decode this as from foo.bar import baz and thereinafter use baz as the handler function. Now the problem is that we need to rewrite all of our lambda code that was inside modules just that we can have one .py file in a suitable place so that it works.

There are no “index files” in Python, so looking for one is misguided. In fact I’ve been writing a library that would have the handler inside it - a small framework setting up logging and such, and then calling the the actual handler function that resides in the installed source code. The handler need not even be in a file, it is just sufficient that it is something that Python can import.

The fix could be as simple as removing the index parameter and specifying that the entry must be a directory, and then just:

    const handler = props.handler ?? 'index.handler';
    const runtime = props.runtime ?? lambda.Runtime.PYTHON_3_7;

    super(scope, id, {
      ...props,
      runtime,
      code: bundle({
        ...props,
        entry,
        runtime,
      }),
      handler: handler,
   }

It is of course possible to make it work with the index parameter as well, if specified, but the default value is tricky.

2reactions
ztanecommented, Apr 14, 2021

@michaelbrewer yes, and the unification they’re after in this bug would make it even more difficult to get anything like that working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda function handler in Python - AWS Documentation
The Lambda function handler is the method in your Python code that processes events. When your function is invoked, Lambda runs the handler...
Read more >
@aws-cdk/aws-lambda-nodejs | Yarn - Package Manager
The NodejsFunction construct creates a Lambda function with automatic transpiling and bundling of TypeScript or Javascript code. This results in smaller Lambda ...
Read more >
AWS Lambda support for Node.js 10: Should you switch to v14?
AWS Lambda support for Node.js 10 is due to end in August 2021. In this article, we compare Node.js 10 and Node.js 14....
Read more >
NodeJS Lambda | The Complete Guide to Get Started 101
In this post, you'll learn about AWS Lambda functions and how to use ... either the drag-and-drop interface or our nifty python interface....
Read more >
Intro to AWS Lambda with Python - YouTube
Join us in this video tutorial we walk through step-by-step how to write AWS Lambda functions in Python to interact with S3 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found