Skip to content

Error("data did not match any variant of untagged enum LambdaRequest") #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johhnsmmith198 opened this issue Nov 16, 2021 · 6 comments

Comments

@johhnsmmith198
Copy link

Hi Nicolas,
Excellent work thank you so much!

I was trying to run it locally, everything seemed fine but I cannot interact with local sam.

here are the steps that I ran:

  • updated some dependencies from 0.0.24-alpha to 0.0.25-alpha because it seems 0.0.24-alpha are not available anymore
  • build and deploy the first time to create was resources
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

make build 
sam deploy --stack-name serverless-rust-demo --resolve-s3 --capabilities CAPABILITY_IAM
  • run the local API
sam local start-api --debug --profile default

-try to insert an item

curl -X PUT -H "Content-Type: application/json" -d '{"id"": "i1", "name": "name1", "price": "1"}' http://localhost:3000/i1

some resulting logs

2021-11-17 06:59:10,537 | Mounting PutProductFunction at http://127.0.0.1:3000/{id} [PUT]
2021-11-17 06:59:10,537 | Mounting DeleteProductFunction at http://127.0.0.1:3000/{id} [DELETE]
2021-11-17 06:59:10,537 | Mounting GetProductFunction at http://127.0.0.1:3000/{id} [GET]
2021-11-17 06:59:10,537 | Mounting GetProductsFunction at http://127.0.0.1:3000/ [GET]
2021-11-17 06:59:10,537 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2021-11-17 06:59:10,537 | Localhost server is starting up. Multi-threading = True
2021-11-17 06:59:10  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2021-11-17 06:59:19,628 | Constructed String representation of Event Version 2.0 to invoke Lambda. Event: {"version": "2.0", "routeKey": "PUT /<id>", "rawPath": "/i1", "rawQueryString": "", "cookies": [], "headers": {"Host": "127.0.0.1:3000", "User-Agent": "curl/7.68.0", "Accept": "*/*", "Content-Type": "application/json", "Content-Length": "44", "X-Forwarded-Proto": "http", "X-Forwarded-Port": "3000"}, "queryStringParameters": {}, "requestContext": {"accountId": "123456789012", "apiId": "1234567890", "http": {"method": "PUT", "path": "/i1", "protocol": "HTTP/1.1", "sourceIp": "127.0.0.1", "userAgent": "Custom User Agent String"}, "requestId": "d448b162-7c1c-4bad-bba2-0f10bf9257f4", "routeKey": "PUT /<id>", "stage": "$default", "time": "16/Nov/2021:19:59:10 +0000", "timeEpoch": 1637092750, "domainName": "localhost", "domainPrefix": "localhost"}, "body": "{\"id\"\": \"i1\", \"name\": \"name1\", \"price\": \"1\"}", "pathParameters": {"id": "i1"}, "stageVariables": null, "isBase64Encoded": false}
2021-11-17 06:59:19,629 | Found one Lambda function with name 'PutProductFunction'
2021-11-17 06:59:19,629 | Invoking bootstrap (provided.al2)
2021-11-17 06:59:19,629 | Environment variables overrides data is standard format
2021-11-17 06:59:19,629 | Loading AWS credentials from session with profile 'default'
2021-11-17 06:59:19,639 | Resolving code path. Cwd=/home/pc/Documents/funspace/serverless-rust-demo-main, CodeUri=/home/pc/Documents/funspace/serverless-rust-demo-main/build/put-product
2021-11-17 06:59:19,639 | Resolved absolute path to code is /home/pc/Documents/funspace/serverless-rust-demo-main/build/put-product
2021-11-17 06:59:19,639 | Code /home/pc/Documents/funspace/serverless-rust-demo-main/build/put-product is not a zip/jar file
2021-11-17 06:59:19,651 | Skip pulling image and use local one: public.ecr.aws/sam/emulation-provided.al2:rapid-1.35.0-arm64.

2021-11-17 06:59:19,651 | Mounting /home/pc/Documents/funspace/serverless-rust-demo-main/build/put-product as /var/task:ro,delegated inside runtime container
2021-11-17 06:59:20,136 | Starting a timer for 5 seconds for function 'PutProductFunction'
START RequestId: ba4e9de5-a60f-499a-9912-74010803ddee Version: $LATEST
{"timestamp":"Nov 16 19:59:20.255","level":"INFO","fields":{"message":"config file not found","path":"~/.aws/config"},"target":"aws_config::profile::parser::source","span":{"name":"load_config_file"},"spans":[{"name":"get_store"},{"name":"load_config_file"}]}
{"timestamp":"Nov 16 19:59:20.270","level":"INFO","fields":{"message":"config file loaded","path":"~/.aws/config","size":"0"},"target":"aws_config::profile::parser::source","span":{"name":"load_config_file"},"spans":[{"name":"get_store"},{"name":"load_config_file"}]}
{"timestamp":"Nov 16 19:59:20.271","level":"INFO","fields":{"message":"config file not found","path":"~/.aws/credentials"},"target":"aws_config::profile::parser::source","span":{"name":"load_credentials_file"},"spans":[{"name":"get_store"},{"name":"load_credentials_file"}]}
{"timestamp":"Nov 16 19:59:20.271","level":"INFO","fields":{"message":"config file loaded","path":"~/.aws/credentials","size":"0"},"target":"aws_config::profile::parser::source","span":{"name":"load_credentials_file"},"spans":[{"name":"get_store"},{"name":"load_credentials_file"}]}
{"timestamp":"Nov 16 19:59:20.273","level":"WARN","fields":{"message":"failed to get selected 'default' profile"},"target":"aws_config::profile::retry_config","span":{"name":"get_store"},"spans":[{"name":"get_store"}]}
{"timestamp":"Nov 16 19:59:20.301","level":"INFO","fields":{"message":"Initializing DynamoDB store with table name: Table"},"target":"products::utils","span":{"name":"get_store"},"spans":[{"name":"get_store"}]}
Error: Error("data did not match any variant of untagged enum LambdaRequest", line: 0, column: 0)
END RequestId: ba4e9de5-a60f-499a-9912-74010803ddee
REPORT RequestId: ba4e9de5-a60f-499a-9912-74010803ddee  Init Duration: 0.16 ms  Duration: 310.09 msBilled Duration: 400 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
2021-11-17 06:59:20,734 | Cleaning all decompressed code dirs
2021-11-17 06:59:20,735 | Unable to find Click Context for getting session_id.
2021-11-17 06:59:20,735 | Invalid lambda response received: Lambda response must be valid json
2021-11-17 06:59:20 127.0.0.1 - - [17/Nov/2021 06:59:20] "PUT /i1 HTTP/1.1" 502 -

What am I doing wrong, please?
Thanks

@nmoutschen
Copy link
Contributor

Hey @johhnsmmith198 !

Thanks a lot for creating this issue. You're not doing anything wrong. This is an issue between the event that SAM local sends, and the LambdaRequest representation.

You can find the related issues here:

And here's the PR to make the Rust runtime more permissive: awslabs/aws-lambda-rust-runtime#366

@johhnsmmith198
Copy link
Author

Hey @nmoutschen thanks for the explanation.
If not with SAM, Can I ask how have you been running your code locally, please?

@nmoutschen
Copy link
Contributor

nmoutschen commented Nov 18, 2021

The PR with the fix for this is merged into the main branch for the lambda_http crate, but not yet deployed. You can use the version from git by changing the dependency in your Cargo.toml file like this:

lambda_http = { git = "https://github.com/awslabs/aws-lambda-rust-runtime/lambda-http" }

@johhnsmmith198
Copy link
Author

Hey @nmoutschen
I had to make it looks like this in Cargo.toml

lambda_http = { git = "https://github.com/awslabs/aws-lambda-rust-runtime", package="lambda_http", optional = true }

I see the log durring build

...
 Compiling lambda_runtime v0.4.1 (https://github.com/awslabs/aws-lambda-rust-runtime#576f6013)
   Compiling lambda_runtime v0.4.1
   Compiling aws-smithy-http-tower v0.28.0-alpha
   Compiling aws-endpoint v0.0.25-alpha
   Compiling aws-http v0.0.25-alpha
   Compiling aws-sig-auth v0.0.25-alpha
   Compiling lambda_http v0.4.1 (https://github.com/awslabs/aws-lambda-rust-runtime#576f6013)
...

Still I run sam

 sam local start-api --debug --profile default

and query it simply with

curl -X PUT -H "Content-Type: application/json" -d '{"id"": "i1", "name": "name1", "price": "1"}' http://127.0.0.1:3000/i1

I get the exact same error:

{"timestamp":"Nov 19 11:17:05.261","level":"INFO","fields":{"message":"Initializing DynamoDB store with table name: Table"},"target":"products::utils","span":{"name":"get_store"},"spans":[{"name":"get_store"}]}
Error: Error("data did not match any variant of untagged enum LambdaRequest", line: 0, column: 0)
END RequestId: b2b67611-774c-4483-85b5-f563bce0a4ac
REPORT RequestId: b2b67611-774c-4483-85b5-f563bce0a4ac  Init Duration: 0.07 ms  Duration: 309.97 msBilled Duration: 400 ms Memory Size: 128 MB     Max Memory Used: 128 MB
2021-11-19 22:17:05,724 | Cleaning all decompressed code dirs
2021-11-19 22:17:05,724 | Unable to find Click Context for getting session_id.
2021-11-19 22:17:05,725 | Invalid lambda response received: Lambda response must be valid json
2021-11-19 22:17:05 127.0.0.1 - - [19/Nov/2021 22:17:05] "PUT /i1 HTTP/1.1" 502 -

@nmoutschen
Copy link
Contributor

Hey @johhnsmmith198 !

I just noticed one issue with your JSON payload: you have two quotes after the id tag:

{"id"": "i1", "name": "name1", "price": "1"}

It should be:

{"id": "i1", "name": "name1", "price": "1"}

I just tried on my end and it works for me with the Cargo.toml change.

@johhnsmmith198
Copy link
Author

Thank you very much. What a silly mistake this one.
Thank you for putting this demo on and improving important library like lambda_http in the process.
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants