Skip to content

Data did not match any variant of untagged enum LambdaRequest with SAM local #365

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
nmoutschen opened this issue Nov 16, 2021 · 7 comments

Comments

@nmoutschen
Copy link
Contributor

nmoutschen commented Nov 16, 2021

When running a Lambda function in Rust using the lambda-http crate with sam local start-api and an HttpApi event, the Lambda runtime is not able to deserialize the payload sent by SAM into a LambdaRequest.

I've created a minimum reproducible sample that just takes an event received by a Lambda function running locally in another language, and try to deserialize it into a lambda_http::request::LambdaRequest:

const PAYLOAD: &str = r#"{
    "version": "2.0",
    "routeKey": "GET /hello",
    "rawPath": "/hello",
    "rawQueryString": "",
    "cookies": [],
    "headers": {
      "Host": "127.0.0.1:3000",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0",
      "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
      "Accept-Language": "en-US,en;q=0.5",
      "Accept-Encoding": "gzip, deflate",
      "Connection": "keep-alive",
      "Upgrade-Insecure-Requests": "1",
      "Sec-Fetch-Dest": "document",
      "Sec-Fetch-Mode": "navigate",
      "Sec-Fetch-Site": "none",
      "Sec-Fetch-User": "?1",
      "Cache-Control": "max-age=0",
      "X-Forwarded-Proto": "http",
      "X-Forwarded-Port": "3000"
    },
    "queryStringParameters": {},
    "requestContext": {
      "accountId": "123456789012",
      "apiId": "1234567890",
      "http": {
        "method": "GET",
        "path": "/hello",
        "protocol": "HTTP/1.1",
        "sourceIp": "127.0.0.1",
        "userAgent": "Custom User Agent String"
      },
      "requestId": "1ac06eee-f687-44ec-9036-dfd49d0be0a3",
      "routeKey": "GET /hello",
      "stage": "$default",
      "time": "16/Nov/2021:11:54:33 +0000",
      "timeEpoch": 1637063673,
      "domainName": "localhost",
      "domainPrefix": "localhost"
    },
    "body": "",
    "pathParameters": {},
    "stageVariables": null,
    "isBase64Encoded": false
  }"#;

fn main() {
    let val: lambda_http::request::LambdaRequest = serde_json::from_str(PAYLOAD).unwrap();
    println!("{:?}", val);
}
@nmoutschen
Copy link
Contributor Author

If I change stageVariables from null into {}, the example above works.

@bnusunny
Copy link
Contributor

This might be an issue with SAM local. API Gateway omits those null valued properties.

@nmoutschen
Copy link
Contributor Author

nmoutschen commented Nov 16, 2021

Agree that this is probably an issue on SAM local, but that might be a possibility we need to deal with from a robustness principle perspective. lambda_http::request::LambdaRequest already supports fields with null values for Alb and ApiGateway variants. I've made a PR that would fix it for sam local in the meantime, and will create an issue for aws-sam-cli too.

@ionionascu
Copy link

I got a similar error when using the Test Method functionality of the API Gateway in the AWS Console, not related to SAM. I can see that the request sent to the lambda has:

    "headers": null,
    "multiValueHeaders": null,
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,

This happens for a deployment that used to work just fine until an unrelated lambda code change got deployed for testing.

@nmoutschen
Copy link
Contributor Author

@ionionascu Could you retry by using the git version of the lambda_http crate to know if the latest changes fix the issue in that situation?

You can do that by replacing the dependency like this in your Cargo.toml:

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

@nmoutschen
Copy link
Contributor Author

I'll close this issue since the PR is merged, but let me know if it doesn't work for you @ionionascu

@ionionascu
Copy link

I'll close this issue since the PR is merged, but let me know if it doesn't work for you @ionionascu

@nmoutschen , I have raised another issue, #367, as I think my problem has a different cause. I am testing with lambda_http = { git = "https://github.com/awslabs/aws-lambda-rust-runtime" } in Cargo.toml.

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

3 participants