aws-samples / aws-cdk-examples

Example projects using the AWS CDK
Apache License 2.0
5.09k stars 2.14k forks source link

dynamodb-lambda: local start-lambda fails to mount a lambda function sub-directory #587

Open njlynch opened 2 years ago

njlynch commented 2 years ago

Created on behalf of @dmitriierkineroad from https://github.com/aws/aws-cdk/issues/18088

What is the problem?

For this example https://github.com/aws-samples/aws-cdk-examples/tree/master/python/dynamodb-lambda sam fails to run the consumer lambda function:

...

Mounting /home/dmitrii/sandbox/aws-cdk-examples/python/dynamodb-lambda/.aws-sam/build/asset.de3b9cf558a0778ce41c0deffcc7f5ae1209ae47d9b4a99b9668294ae095d8dc as /var/task:ro,delegated inside runtime container
START RequestId: c1614ee9-c36c-483b-942d-2b7de0dbb10d Version: $LATEST
START RequestId: 5a743bf1-d735-4cef-a6c2-6990f7e417cb Version: $LATEST
Unable to import module 'lambda_function': No module named 'lambda_function'

[ERROR] [1639909113400] LAMBDA_RUNTIME Failed to get next invocation. Http Response code: 403
Traceback (most recent call last):
  File "/var/runtime/awslambda/bootstrap.py", line 620, in <module>
    main()
  File "/var/runtime/awslambda/bootstrap.py", line 599, in main
    (invokeid, x_amzn_trace_id, sockfd, credentials, event_body, context_objs, invoked_function_arn) = wait_for_invoke()
  File "/var/runtime/awslambda/bootstrap.py", line 213, in wait_for_invoke
    ) = lambda_runtime.receive_invoke()
RuntimeError: Runtime internal error
Unable to import module 'lambda_function': No module named 'lambda_function'

Reproduction Steps

  1. download https://github.com/aws-samples/aws-cdk-examples/tree/master/python/dynamodb-lambda
  2. cd dynamodb-lambda
  3. python -m virtualenv -p /usr/bin/python3.6 .venv
  4. source .venv/bin/activate
  5. python -m pip install -r requirements.txt
  6. cdk synth
  7. sam build --template-file ./cdk.out/dynamodb-lambda.template.json
  8. sam local start-lambda and then in a separate shell: 9, aws lambda invoke --profile sandbox --function-name "consumerlambdafunction40710347" --endpoint-url "http://127.0.0.1:3001" --no-verify-ssl out.txt

What did you expect to happen?

I expected step 7. sam build --template-file ./cdk.out/dynamodb-lambda.template.json to copy ./cdk.out/asset.de3b9cf558a0778ce41c0deffcc7f5ae1209ae47d9b4a99b9668294ae095d8dc to ./.aws-sam/build/ because step 8. sam local start-lambda tries to mount this sub-folder:

Mounting ./.aws-sam/build/asset.de3b9cf558a0778ce41c0deffcc7f5ae1209ae47d9b4a99b9668294ae095d8dc as /var/task:ro,delegated inside runtime container
START RequestId: ec141273-dc5a-4efb-b389-5cdf52e96331 Version: $LATEST
START RequestId: 249ae7e1-a1b3-429d-90ac-396bce204b12 Version: $LATEST
Unable to import module 'lambda_function': No module named 'lambda_function'

What actually happened?

step 7. sam build --template-file ./cdk.out/dynamodb-lambda.template.json created sub-folder /.aws-sam/build/consumerlambdafunction40710347 for the consumer lambda function, which step 8. sam local start-lambda doesn't try to mount.

CDK CLI Version

2.2.0 (build 4f5c27c)

Framework Version

No response

Node.js Version

v14.18.2

OS

Ubuntu 20.04

Language

Python

Language Version

No response

Other information

Created from https://github.com/aws/aws-cdk/issues/18088

siddjoshi commented 1 year ago

Not able to reproduce the issue either with python3.6 or 3.7. In both cases sample is working fine for both consumer and producer lambda functions. I tried with CDK version - 2.60.0 (build 2d40d77), did not check with the older version of CDK.