Skip to content

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

Closed
@nmoutschen

Description

@nmoutschen

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions