简体   繁体   中英

AWS Lambda Java + ALB missing HTTP request data (ie path, etc)

I've create two lambda functions (one java, one python) and added both of them to an ALB.

The code of the functions just converts the event to JSON and returns it:

public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context context) {
  logger.info("path="+event.getRawPath());
  APIGatewayV2HTTPResponse response = new APIGatewayV2HTTPResponse();
  response.setStatusCode(200);
  response.setBody(new ObjectMapper().writeValueAsString(event));

When i call the ALB for the python lambda and I get

{
  "requestContext": {
    "elb": {
      "targetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:821844782278:targetgroup/lambda-fu7bq1bfvaetnsi28l5g/3440f9a769be4e61"
    }
  },
  "httpMethod": "GET",
  "path": "/test",
  "queryStringParameters": {},
  "headers": {
    "accept": "*/*",
    "host": "core-external-dev-135473791.us-west-2.elb.amazonaws.com",
    "user-agent": "curl/7.85.0",
    "x-amzn-trace-id": "Root=1-63a5d7ad-1aadf14e6ecfb0e733ba868d",
    "x-forwarded-for": "98.167.119.9",
    "x-forwarded-port": "443",
    "x-forwarded-proto": "https"
  },
  "body": "",
  "isBase64Encoded": false
}

However for the java lambda I get

{
  "version": null,
  "routeKey": null,
  "rawPath": null,
  "rawQueryString": null,
  "cookies": null,
  "headers": {
    "accept": "*/*",
    "host": "core-external-dev-135473791.us-west-2.elb.amazonaws.com",
    "user-agent": "curl/7.85.0",
    "x-amzn-trace-id": "Root=1-63a5d77d-457cc4773e94430d0d64d668",
    "x-forwarded-for": "98.167.119.9",
    "x-forwarded-port": "443",
    "x-forwarded-proto": "https"
  },
  "queryStringParameters": {},
  "pathParameters": null,
  "stageVariables": null,
  "body": "",
  "isBase64Encoded": false,
  "requestContext": {
    "routeKey": null,
    "accountId": null,
    "stage": null,
    "apiId": null,
    "domainName": null,
    "domainPrefix": null,
    "time": null,
    "timeEpoch": 0,
    "http": null,
    "authorizer": null,
    "requestId": null
  }
}

So while the headers are there, there's nothing else related to the request (!)

Ah found the problem, i need to change the class of the event when i switched the code from function URL to ALB ( to ApplicationLoadBalancerRequestEvent )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM