0

I am using python 3.8 as the lambda runtime for my lambda function which I am trying to deploy using cdk v2. This is a lambda as a custom resource that runs every time the stack deploys.

This is the folder structure

 /Dynamics/cust_resource/__init__.py
 /Dynamics/cust_resource/data_objects.py
 /Dynamics/cust_resource/lambda.py
 /Dynamics/cust_resource/requirements.txt
 /Dynamics/__init__.py
 /Dynamics/dyna_stack.py
 app.py

This is the lambda code

import os
import boto3
import sys
 
print('before lambda import') 

from data_objects import poco_data

This is how I am creating the function in the stack

cust_res_lambda = _lambda.Function(
    self, 'crLambda',
    runtime=_lambda.Runtime.PYTHON_3_8,
    code=_lambda.Code.from_asset('Dynamics/cust_resource',
                                 bundling=BundlingOptions(
                                        image=_lambda.Runtime.PYTHON_3_8.bundling_image,
                                        command=["bash", "-c", "pip install -r requirements.txt -t /asset-output && cp -au . /asset-output"]
                                     )),
    handler='lambda.lambda_handler',
    function_name='crlambdaFn',
    
)

The lambda executes and I see the line before lambda import in cloudwatch logs followed by.

[ERROR] RuntimeError: generator didn't stop after throw()
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/lambda.py", line 12, in <module>
    from data_objects import poco_data
  File "/var/task/data_objects.py", line 1, in <module>
    from aws_cdk import (
  File "/var/task/aws_cdk/__init__.py", line 1257, in <module>
    from ._jsii import *
  File "/var/task/aws_cdk/_jsii/__init__.py", line 13, in <module>
    import constructs._jsii
  File "/var/task/constructs/__init__.py", line 43, in <module>
    from ._jsii import *
  File "/var/task/constructs/_jsii/__init__.py", line 13, in <module>
    __jsii_assembly__ = jsii.JSIIAssembly.load(
  File "/var/task/jsii/_runtime.py", line 54, in load
    _kernel.load(assembly.name, assembly.version, os.fspath(assembly_path))
  File "/var/lang/lib/python3.8/contextlib.py", line 162, in __exit__
    raise RuntimeError("generator didn't stop after throw()")
[ERROR] RuntimeError: generator didn't stop after throw() Traceback (most recent call last):   File "/var/lang/lib/python3.8/imp.py", line 234, in load_module     return load_source(name, filename, file)   File "/var/lang/lib/python3.8/imp.py", line 171, in load_source     module = _load(spec)   File "<frozen importlib._bootstrap>", line 702, in _load   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked   File "<frozen importlib._bootstrap_external>", line 843, in exec_module   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "/var/task/lambda.py", line 12, in <module>     from data_objects import poco_data   File "/var/task/data_objects.py", line 1, in <module>     from aws_cdk import (   File "/var/task/aws_cdk/__init__.py", line 1257, in <module>     from ._jsii import *   File "/var/task/aws_cdk/_jsii/__init__.py", line 13, in <module>     import constructs._jsii   File "/var/task/constructs/__init__.py", line 43, in <module>     from ._jsii import *   File "/var/task/constructs/_jsii/__init__.py", line 13, in <module>     __jsii_assembly__ = jsii.JSIIAssembly.load(   File "/var/task/jsii/_runtime.py", line 54, in load     _kernel.load(assembly.name, assembly.version, os.fspath(assembly_path))   File "/var/lang/lib/python3.8/contextlib.py", line 162, in __exit__     raise RuntimeError("generator didn't stop after throw()")

What does this error mean and how to fix it?

user20358
  • 14,182
  • 36
  • 114
  • 186
  • Why are you importing CDK itself in your Lambda code? Can you try without it? CDK uses JSII which tries to load something and fails (probably because Node and CDK CLI are not isntalled). – kichik Dec 09 '22 at 06:38
  • I don't believe the CustomResource context matters here. If you are having bundling issues, consider the [PythonFunction](https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_lambda_python_alpha/README.html) construct, a Python-specific version of `Function` which helps with [packaging dependencies](https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_lambda_python_alpha/README.html#packaging). – fedonev Dec 09 '22 at 08:00
  • @kichik: Agreed. It doesn't need to import cdk. I took it out later. However, earlier even though I was including cdk, the only time I saw that error is when I had this line `from data_objects import poco_data`. With `from data_objects import poco_data` commented out, the lambda deployment would work even though I was importing cdk. So I suspect that is not causing the error maybe? – user20358 Dec 10 '22 at 03:56
  • @fedonev: Should this file containing the data object class be packaged as a layer instead? The data class is shared by both the lambda and the dyna_stack. – user20358 Dec 10 '22 at 03:59
  • I don't know what the pieces of your setup are, but I think it's safe to say that Lambda Layers would be at best a premature optimization. My advice is to get things working with a simple .zip dependency bundle first. – fedonev Dec 10 '22 at 09:51

0 Answers0