aws-samples / lambda-refarch-webapp

The Web Application reference architecture is a general-purpose, event-driven, web application back-end that uses AWS Lambda, Amazon API Gateway for its business logic. It also uses Amazon DynamoDB as its database and Amazon Cognito for user management. All static content is hosted using AWS Amplify Console.
https://aws.amazon.com/lambda/web-apps/
Apache License 2.0
1.59k stars 907 forks source link

CORS policy error after login #50

Open MASalem opened 3 years ago

MASalem commented 3 years ago

Hello,

After deploying the webapp, when I sign up and then login, the page that is in the screenshot (attached) appears briefly then I get redirected to the login page again. image

This seem to be an issue with the CORS policy, do you know how I can solve this?

Thank you!

Reproduction Steps

Error Log

Environment

Other


This is :bug: Bug Report

Iviglious commented 3 years ago

Hi,

In summary

The CORS error is a secondary error caused by a previous error of a missing package (aws-xray-sdk-core) in AWS Lambda.

Workaround fix

Generate local installation of all packages (libraries) and then deploy them together in AWS Lambda.

In details

When checking the CloudWatch logs of the AWS Lambda functions It can be seen the error of a missing package/library aws-xray-sdk-core:

2021-03-23T00:17:07.587Z    undefined   ERROR   Uncaught Exception  {
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'aws-xray-sdk-core'\nRequire stack:\n- /var/task/app.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'aws-xray-sdk-core'",
        "Require stack:",
        "- /var/task/app.js",
        "- /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:999:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
        "    at Module.load (internal/modules/cjs/loader.js:863:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

Currently the AWS Lambda functions only require/depend on these 3 packages:

    "aws-sdk": "^2.823.0",
    "aws-embedded-metrics": "^2.0.2",
    "aws-xray-sdk-core": "^3.2.0"

The aws-sdk seems to be already included in AWS Lambda (no error about it). However, the xray library seems to be missing (or there is some internal bug).
I tested with NodeJS 14 version of AWS Lambda and the xray library was not available there either.

Workaround fix

I did npm install inside the todo-src\getAllTodo folder then did another sam deploy and it was fine.
The CORS error disappeared.
I created a bash script to do the install of the libraries in all function folders:

cd todo-src/

cd addTodo; npm install; cd ..
cd completeTodo; npm install; cd ..
cd deleteTodo; npm install; cd ..
cd getAllTodo; npm install; cd ..
cd getTodo; npm install; cd ..
cd updateTodo; npm install; cd ..

cd ..

Feedback

1) Check with the AWS Lambda team on why the aws-xray-sdk-core library is not available.
2) Furthermore, it will be useful to expand/update this page: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html and give detailed list of all libraries/packages and their version supported by AWS Lambda.
3) Deploying the whole installation of packages is not user friendly as the code gets zipped and is not visible in AWS portal.

Regards,
Iviglious