localstack / serverless-localstack

⚡ Serverless plugin for running against LocalStack
511 stars 82 forks source link

Not working with serverless-webpack #171

Closed ivanmartos closed 8 months ago

ivanmartos commented 2 years ago

I tried integrating this plugin with localstack and also with serverless-webpack, but it is not working

Versions I used

Sample repository with steps on how to simulate the issues is

https://github.com/ivanmartos/serverless-localstack-example

When using deploying to localstack the deployment succeeds but when invoking the endpoint I get an error Runtime.ImportModuleError

When running locally via serverless-offline plugin everything works correctly. Webpack is used for both approaches (localstack and also offline)

Error returned from invocation of lambda via browser (when deployed in localstack)

{"errorType": "InvocationException", "errorMessage": "Lambda process returned with error. Result: {\"errorType\":\"Runtime.ImportModuleError\",\"errorMessage\":\"Error: Cannot find module 'helloWorld'\\nRequire stack:\\n- /var/runtime/UserFunction.js\\n- /var/runtime/index.js\"}. Output:\n2021-11-16T08:38:53.880Z\tundefined\tERROR\tUncaught Exception \t{\"errorType\":\"Runtime.ImportModuleError\",\"errorMessage\":\"Error: Cannot find module 'helloWorld'\\nRequire stack:\\n- /var/runtime/UserFunction.js\\n- /var/runtime/index.js\",\"stack\":[\"Runtime.ImportModuleError: Error: Cannot find module 'helloWorld'\",\"Require stack:\",\"- /var/runtime/UserFunction.js\",\"- /var/runtime/index.js\",\" at _loadUserApp (/var/runtime/UserFunction.js:100:13)\",\" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)\",\" at Object. (/var/runtime/index.js:43:30)\",\" at Module._compile (internal/modules/cjs/loader.js:1063:30)\",\" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)\",\" at Module.load (internal/modules/cjs/loader.js:928:32)\",\" at Function.Module._load (internal/modules/cjs/loader.js:769:14)\",\" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)\",\" at internal/main/run_main_module.js:17:47\"]}\n\u001b[32mSTART RequestId: 0e12935a-c026-18ca-28a5-a32396537021 Version: $LATEST\u001b[0m\n\u001b[32mEND RequestId: 0e12935a-c026-18ca-28a5-a32396537021\u001b[0m\n\u001b[32mREPORT RequestId: 0e12935a-c026-18ca-28a5-a32396537021\tInit Duration: 224.00 ms\tDuration: 1.66 ms\tBilled Duration: 100 ms\tMemory Size: 1536 MB\tMax Memory Used: 41 MB\t\u001b[0m", "stackTrace": [" File \"/opt/code/localstack/localstack/services/awslambda/lambda_api.py\", line 813, in run_lambda\n lock_discriminator=lock_discriminator,\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 429, in execute\n return do_execute()\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 419, in do_execute\n return _run(func_arn=func_arn)\n", " File \"/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py\", line 158, in wrapped\n raise e\n", " File \"/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py\", line 154, in wrapped\n result = func(*args, **kwargs)\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 406, in _run\n raise e\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 402, in _run\n result = self._execute(lambda_function, inv_context)\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 710, in _execute\n result = self.run_lambda_executor(lambda_function=lambda_function, inv_context=inv_context)\n", " File \"/opt/code/localstack/localstack/services/awslambda/lambda_executors.py\", line 638, in run_lambda_executor\n ) from error\n"]}

Error log in localstack docker container

example-localstack | 2021-11-16T08:40:36:DEBUG:localstack.services.awslambda.lambda_api: Running Lambda function arn:aws:lambda:eu-west-1:000000000000:function:example-service-offline-hello from API Gateway invocation: GET /hello
example-localstack | 2021-11-16T08:40:36:INFO:localstack.services.awslambda.lambda_executors: Running lambda: arn:aws:lambda:eu-west-1:000000000000:function:example-service-offline-hello
example-localstack | 2021-11-16T08:40:36:DEBUG:localstack.utils.docker_utils: Running container with image: localstack/lambda-js:nodejs14.x
example-localstack | 2021-11-16T08:40:36:DEBUG:localstack.utils.docker_utils: Creating container with image localstack/lambda-js:nodejs14.x, command '.webpack/service/src/helloWorld.handler', volumes [('/Users/ivan.martos/WorkspacePriv/serverless-localstack-example', '/var/task')], env vars {'AWS_NODEJS_CONNECTION_REUSE_ENABLED': '1', 'AWS_ACCESS_KEY_ID': 'test', 'AWS_SECRET_ACCESS_KEY': 'test', 'AWS_REGION': 'eu-west-1', 'DOCKER_LAMBDA_USE_STDIN': '1', 'LOCALSTACK_HOSTNAME': '172.25.0.2', 'EDGE_PORT': '4566', '_HANDLER': '.webpack/service/src/helloWorld.handler', 'AWS_LAMBDA_FUNCTION_TIMEOUT': '6', 'AWS_LAMBDA_FUNCTION_NAME': 'example-service-offline-hello', 'AWS_LAMBDA_FUNCTION_VERSION': '$LATEST', 'AWS_LAMBDA_FUNCTION_INVOKED_ARN': 'arn:aws:lambda:eu-west-1:000000000000:function:example-service-offline-hello', 'AWS_LAMBDA_COGNITO_IDENTITY': '{}', 'NODE_TLS_REJECT_UNAUTHORIZED': '0'}
example-localstack | 2021-11-16T08:40:36:DEBUG:localstack.utils.docker_utils: Starting container f4798a28f8dcdc8f860886325669d269a559a76f8566cfe96e42807bbd2956e7
example-localstack | 2021-11-16T08:40:37:DEBUG:localstack.utils.docker_utils: Removing container: f4798a28f8dcdc8f860886325669d269a559a76f8566cfe96e42807bbd2956e7
example-localstack | 2021-11-16T08:40:37:DEBUG:localstack.services.awslambda.lambda_executors: Lambda arn:aws:lambda:eu-west-1:000000000000:function:example-service-offline-hello result / log output:
example-localstack | {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}
example-localstack | > 2021-11-16T08:40:37.113Z undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'helloWorld'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1063:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)","    at internal/main/run_main_module.js:17:47"]}
example-localstack | > START RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866 Version: $LATEST
example-localstack | > END RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866
example-localstack | > REPORT RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866   Init Duration: 207.48 ms    Duration: 1.76 ms   Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 41 MB
example-localstack | 2021-11-16T08:40:37:INFO:localstack.services.awslambda.lambda_api: Error executing Lambda function arn:aws:lambda:eu-west-1:000000000000:function:example-service-offline-hello: Lambda process returned with error. Result: {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}. Output:
example-localstack | 2021-11-16T08:40:37.113Z   undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'helloWorld'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1063:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)","    at internal/main/run_main_module.js:17:47"]}
example-localstack | START RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866 Version: $LATEST
example-localstack | END RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866
example-localstack | REPORT RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866 Init Duration: 207.48 ms    Duration: 1.76 ms   Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 41 MB   Traceback (most recent call last):
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 599, in run_lambda_executor
example-localstack |     stdin=event_stdin_bytes,
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 1119, in execute_in_container
example-localstack |     stdin=stdin,
example-localstack |   File "/opt/code/localstack/localstack/utils/docker_utils.py", line 1418, in run_container
example-localstack |     attach=not detach,
example-localstack |   File "/opt/code/localstack/localstack/utils/docker_utils.py", line 1284, in start_container
example-localstack |     stderr=stderr,
example-localstack | localstack.utils.docker_utils.ContainerException: Docker container returned with exit code 1
example-localstack |
example-localstack | The above exception was the direct cause of the following exception:
example-localstack |
example-localstack | Traceback (most recent call last):
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_api.py", line 813, in run_lambda
example-localstack |     lock_discriminator=lock_discriminator,
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 429, in execute
example-localstack |     return do_execute()
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 419, in do_execute
example-localstack |     return _run(func_arn=func_arn)
example-localstack |   File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 158, in wrapped
example-localstack |     raise e
example-localstack |   File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 154, in wrapped
example-localstack |     result = func(*args, **kwargs)
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 406, in _run
example-localstack |     raise e
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 402, in _run
example-localstack |     result = self._execute(lambda_function, inv_context)
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 710, in _execute
example-localstack |     result = self.run_lambda_executor(lambda_function=lambda_function, inv_context=inv_context)
example-localstack |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 638, in run_lambda_executor
example-localstack |     ) from error
example-localstack | localstack.services.awslambda.lambda_executors.InvocationException: Lambda process returned with error. Result: {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"}. Output:
example-localstack | 2021-11-16T08:40:37.113Z   undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'helloWorld'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'helloWorld'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1063:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)","    at internal/main/run_main_module.js:17:47"]}
example-localstack | START RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866 Version: $LATEST
example-localstack | END RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866
example-localstack | REPORT RequestId: 5e88635d-e2f8-163d-ca54-5d2b1cfbe866 Init Duration: 207.48 ms    Duration: 1.76 ms   Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 41 MB
example-localstack |

Deploying to localstack via serverless does not give any errors

ivan.martos@CHZHCLLTIMARTOS serverless-localstack-example % npm run deploy:localstack

> deploy:localstack
> TZ=UTC serverless deploy -s offline

Serverless: config.options_stage: offline
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: offline
Serverless: config.stage: offline
Serverless: Using serverless-localstack
Serverless: Reconfiguring service acm to use http://localhost:4566
Serverless: Reconfiguring service amplify to use http://localhost:4566
Serverless: Reconfiguring service apigateway to use http://localhost:4566
Serverless: Reconfiguring service apigatewayv2 to use http://localhost:4566
Serverless: Reconfiguring service application-autoscaling to use http://localhost:4566
Serverless: Reconfiguring service appsync to use http://localhost:4566
Serverless: Reconfiguring service athena to use http://localhost:4566
Serverless: Reconfiguring service autoscaling to use http://localhost:4566
Serverless: Reconfiguring service batch to use http://localhost:4566
Serverless: Reconfiguring service cloudformation to use http://localhost:4566
Serverless: Reconfiguring service cloudfront to use http://localhost:4566
Serverless: Reconfiguring service cloudsearch to use http://localhost:4566
Serverless: Reconfiguring service cloudtrail to use http://localhost:4566
Serverless: Reconfiguring service cloudwatch to use http://localhost:4566
Serverless: Reconfiguring service cloudwatchlogs to use http://localhost:4566
Serverless: Reconfiguring service codecommit to use http://localhost:4566
Serverless: Reconfiguring service cognito-idp to use http://localhost:4566
Serverless: Reconfiguring service cognito-identity to use http://localhost:4566
Serverless: Reconfiguring service docdb to use http://localhost:4566
Serverless: Reconfiguring service dynamodb to use http://localhost:4566
Serverless: Reconfiguring service dynamodbstreams to use http://localhost:4566
Serverless: Reconfiguring service ec2 to use http://localhost:4566
Serverless: Reconfiguring service ecr to use http://localhost:4566
Serverless: Reconfiguring service ecs to use http://localhost:4566
Serverless: Reconfiguring service eks to use http://localhost:4566
Serverless: Reconfiguring service elasticache to use http://localhost:4566
Serverless: Reconfiguring service elasticbeanstalk to use http://localhost:4566
Serverless: Reconfiguring service elb to use http://localhost:4566
Serverless: Reconfiguring service elbv2 to use http://localhost:4566
Serverless: Reconfiguring service emr to use http://localhost:4566
Serverless: Reconfiguring service es to use http://localhost:4566
Serverless: Reconfiguring service events to use http://localhost:4566
Serverless: Reconfiguring service firehose to use http://localhost:4566
Serverless: Reconfiguring service glacier to use http://localhost:4566
Serverless: Reconfiguring service glue to use http://localhost:4566
Serverless: Reconfiguring service iam to use http://localhost:4566
Serverless: Reconfiguring service iot to use http://localhost:4566
Serverless: Reconfiguring service iotanalytics to use http://localhost:4566
Serverless: Reconfiguring service iotevents to use http://localhost:4566
Serverless: Reconfiguring service iot-data to use http://localhost:4566
Serverless: Reconfiguring service iot-jobs-data to use http://localhost:4566
Serverless: Reconfiguring service kafka to use http://localhost:4566
Serverless: Reconfiguring service kinesis to use http://localhost:4566
Serverless: Reconfiguring service kinesisanalytics to use http://localhost:4566
Serverless: Reconfiguring service kms to use http://localhost:4566
Serverless: Reconfiguring service lambda to use http://localhost:4566
Serverless: Reconfiguring service logs to use http://localhost:4566
Serverless: Reconfiguring service mediastore to use http://localhost:4566
Serverless: Reconfiguring service neptune to use http://localhost:4566
Serverless: Reconfiguring service organizations to use http://localhost:4566
Serverless: Reconfiguring service qldb to use http://localhost:4566
Serverless: Reconfiguring service rds to use http://localhost:4566
Serverless: Reconfiguring service redshift to use http://localhost:4566
Serverless: Reconfiguring service route53 to use http://localhost:4566
Serverless: Reconfiguring service s3 to use http://localhost:4566
Serverless: Reconfiguring service s3control to use http://localhost:4566
Serverless: Reconfiguring service sagemaker to use http://localhost:4566
Serverless: Reconfiguring service sagemaker-runtime to use http://localhost:4566
Serverless: Reconfiguring service secretsmanager to use http://localhost:4566
Serverless: Reconfiguring service ses to use http://localhost:4566
Serverless: Reconfiguring service sns to use http://localhost:4566
Serverless: Reconfiguring service sqs to use http://localhost:4566
Serverless: Reconfiguring service ssm to use http://localhost:4566
Serverless: Reconfiguring service stepfunctions to use http://localhost:4566
Serverless: Reconfiguring service sts to use http://localhost:4566
Serverless: Reconfiguring service timestream to use http://localhost:4566
Serverless: Reconfiguring service transfer to use http://localhost:4566
Serverless: Reconfiguring service xray to use http://localhost:4566
Serverless: config.options_stage: offline
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: offline
Serverless: config.stage: offline
Serverless: config.options_stage: offline
Serverless: serverless.service.custom.stage: undefined
Serverless: serverless.service.provider.stage: offline
Serverless: config.stage: offline
Serverless: Using custom endpoint for STS: http://localhost:4566
Serverless: Bundling with Webpack...
1 asset
2 modules
webpack 5.64.1 compiled successfully in 3141 ms
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: nanoid@^3.1.28
Serverless: Copying existing artifacts...
Serverless: Skip plugin function Package.packageService (lambda.mountCode flag is enabled)
Serverless: Skip plugin function ServerlessWebpack.cleanup (lambda.mountCode flag is enabled)
Serverless: Skip plugin function AwsCompileFunctions.downloadPackageArtifacts (lambda.mountCode flag is enabled)
Serverless: Installing dependencies for custom CloudFormation resources...
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Skip plugin function AwsDeploy.extendedValidate (lambda.mountCode flag is enabled)
Serverless: Using custom endpoint for CloudFormation: http://localhost:4566
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Using custom endpoint for Lambda: http://localhost:4566
Serverless: Uploading CloudFormation file to S3...
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Skip plugin function AwsDeploy.uploadFunctionsAndLayers (lambda.mountCode flag is enabled)
Serverless: Uploading custom CloudFormation resources...
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Validating template...
Serverless: Skipping template validation: Unsupported in Localstack
Serverless: Updating Stack...
Serverless: Using custom endpoint for CloudFormation: http://localhost:4566
Serverless: Overriding S3 templateUrl to http://localhost:4566
Serverless: Checking Stack update progress...
Serverless: Using custom endpoint for CloudFormation: http://localhost:4566
..................
Serverless: Stack update finished...
Serverless: Using custom endpoint for CloudFormation: http://localhost:4566
Serverless: Using custom endpoint for CloudFormation: http://localhost:4566
Service Information
service: example-service
stage: offline
region: eu-west-1
stack: example-service-offline
resources: 15
api keys:
  None
endpoints:
  http://localhost:4566/restapis/tpj63l31zc/offline/_user_request_
functions:
  hello: example-service-offline-hello
layers:
  None
Serverless: Using custom endpoint for APIGateway: http://localhost:4566
Serverless: Using custom endpoint for APIGateway: http://localhost:4566
Serverless: Using custom endpoint for APIGateway: http://localhost:4566
Serverless: Using custom endpoint for APIGateway: http://localhost:4566
Serverless: Using custom endpoint for S3: http://localhost:4566
Serverless: Removing old service artifacts from S3...
Serverless: Using custom endpoint for S3: http://localhost:4566
sirimykland commented 2 years ago

I experienced the same issue, however it works as it should up to v0.4.30 for us. Thus, likely introduced in v0.4.31

the module could not be found because the handlers path was '.webpack/service/src/helloWorld.handler' instead of 'src/helloWorld.handler' as it should be

aahdahl commented 2 years ago

Not sure if it is 100% related but when we deploy our solution to localstack it works correctly, but when we deploy to aws, the handler paths are incorrect, just as @sirimykland describes. We've made a node patch with patch-package and it solves the issue for us. I've attached the patch here (rename it from serverless-localstack+0.4.35.txt to serverless-localstack+0.4.35.patch to apply it) serverless-localstack+0.4.35.txt

aahdahl commented 2 years ago

Added a PR for a fix that resolved our issue..

ackdav commented 1 year ago

Hi all - thank you for raising this issue and thank you @aahdahl for pushing a fix! @ivanmartos would you be able to pull the latest version and double check whether the issue persists? Thank you!

steffyP commented 8 months ago

Closing the issue due to inactivity. The issue should already be resolved in the latest release.