aws-lambdaspring-cloud-function

How to obtain path of request in spring cloud function on AWS Lambda


I can obtain all I need from the incoming request except for the path component of the request.

e.g. for a postman request for xxx.lambda-url.us-west-2.on.aws/example?param=true

I can see host, query param and headers. but the /example is missing.

My function currently looks like this, using the standard handler in the AWS configuration. The host name being used is the Function URL in the AWS Lambda function configuration.

org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest

    public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> handleRequest() {
        return value -> {
            APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
            responseEvent.setStatusCode(200);
            responseEvent.setHeaders(Collections.singletonMap("Content-Type", "text/plain"));
            responseEvent.setBody(value.toString());
            return responseEvent;
        };
    }

I have also tried using Message<String> but the message similarly has no evidence of the requested path.

How do I obtain the full details about the incoming request to the lambda?

Example response from the above code:

{version: 2.0,headers: {content-length=5, x-amzn-tls-version=TLSv1.2, x-forwarded-proto=https, postman-token=63fb055b-b7a9-4118-bd65-b3871178feba, x-forwarded-port=443, x-api-token=123456, accept=* /*, x-amzn-tls-cipher-suite=ECDHE-RSA-AES128-GCM-SHA256, x-amzn-trace-id=Root=1-62e0b85d-37132a0f191aa55604dc8ed3, host=xxx.lambda-url.us-west-2.on.aws, content-type=text/plain, cache-control=no-cache, accept-encoding=gzip, deflate, br, user-agent=PostmanRuntime/7.29.2},queryStringParameters: {param1=true},requestContext: {accountId: anonymous,stage: $default,requestId: 82277194-0d8c-4edc-b5b8-7ad35ca62e76,apiId: xxx,},isBase64Encoded: false}

The raw event incoming from Lambda logged by o.s.c.f.adapter.aws.FunctionInvoker looks like this below. I think the problem is that the incoming event format does not match the definition of the APIGatewayProxyRequestEvent. There is no "rawPath" property on the base input, and no "http" property on the requestContext.

{
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/testing",
    "rawQueryString": "hello=true",
    "headers": { removed for brevity }
    "queryStringParameters": {
        "hello": "true"
    },
    "requestContext": {
        "accountId": "anonymous",
        "apiId": "xxx",
        "domainName": "xxx.lambda-url.us-west-2.on.aws",
        "domainPrefix": "xxx",
        "http": {
            "method": "GET",
            "path": "/testing",
            "protocol": "HTTP/1.1",
            "sourceIp": "151.210.174.210",
            "userAgent": "PostmanRuntime/7.29.2"
        },
        "requestId": "bc27912e-672f-48e3-bf6d-84b28da74431",
        "routeKey": "$default",
        "stage": "$default",
        "time": "27/Jul/2022:20:46:42 +0000",
        "timeEpoch": 1658954802798
    },
    "isBase64Encoded": false
}

I am using latest release libs from aws and cloud function


Solution

  • Using APIGatewayV2HTTPEvent instead of APIGatewayProxyRequestEvent gives me what I need.

    @Bean
    public Function<APIGatewayV2HTTPEvent, String> handleRequest() {
        return requestEvent -> {
            return requestEvent.toString();
        };
    }