Lambda Function URL vs API Gateway

0

I've done this several time with API Gateway. I'm now trying to do it with a function URL. When I try to add a GET style parameter onto the end of the function URL I get error: KeyName - In other words, the parameter is not being passed to the function. If I dump the entire event to the console, I see that the request has not been mapped into an event Json. This is how the API Gateway does it. The online directions say that the function URL also maps to an event (just like the api gateway) but that isn't what I am seeing.

This is the event that is being passed when using Function URL:

{'version': '2.0', 'routeKey': '$default', 'rawPath': '/', 'rawQueryString': 'epoch=1724773643', 'headers': {'sec-fetch-mode': 'navigate', 'x-amzn-tls-version': 'TLSv1.3', 'sec-fetch-site': 'none', 'x-forwarded-proto': 'https', 'accept-language': 'en-US,en;q=0.9', 'x-forwarded-port': '443', 'dnt': '1', 'x-forwarded-for': '73.40.230.54', 'sec-fetch-user': '?1', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7', 'x-amzn-tls-cipher-suite': 'TLS_AES_128_GCM_SHA256', 'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"', 'x-amzn-trace-id': 'Root=TTTTTT', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'host': 'TTTTT.lambda-url.us-east-1.on.aws', 'upgrade-insecure-requests': '1', 'cache-control': 'max-age=0', 'accept-encoding': 'gzip, deflate, br, zstd', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0', 'sec-fetch-dest': 'document'}, 'queryStringParameters': {'epoch': '1724773643'}, 'requestContext': {'accountId': 'anonymous', 'apiId': 'kXXXe', 'domainName': 'XXXXmbda-url.us-east-1.on.aws', 'domainPrefix': 'TTTTTTTe', 'http': {'method': 'GET', 'path': '/', 'protocol': 'HTTP/1.1', 'sourceIp': 'TTTTT', 'userAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'}, 'requestId': 'eXXXXXfc019', 'routeKey': '$default', 'stage': '$default', 'time': '27/Aug/2024:16:25:14 +0000', 'timeEpoch': 1724775914041}, 'isBase64Encoded': False}

And here is what I get when I execute it from the "test" button in lambda or via API Gateway: {'epoch': 1724773643}

Any idea why the event is mapped to the correct JSON when using test button, but when accessing from Function URL it is completely different?

  • C
1 Answer
0

Hello! Based on how you've described your problem, I think there could be a discrepancy between the format of your test event and the format of a request via Function URL. If that's the case, then that would explain why your function is able to access the parameter with a test event but not when calling the Function URL.

To elaborate: The format of the request you shared for the Function URL is the default format for requests made with a Function URL. The payload you include in the test events that you execute from the Lambda console should mirror that format as that's how the requests will be structured when calling the Function URL. If your test event payload JSON is simply {'epoch': 1724773643} then that means your Lambda function is probably accessing 'epoch' based on that format (i.e. myPayload['epoch']) instead of the format of a Function URL request (i.e. myPayload['queryStringParameters']['epoch']). I would try creating a new test event, double-checking that the JSON you provide conforms to the request format linked above, and then testing your function based on that event.

Hope this helps out!

AWS
segrace
answered 2 months ago
  • It is the same code. The only difference is if I click the "test" button in the lambda editor screen, or if the code runs via someone connecting to the Function URL. I placed the lambda function behind API Gateway and it now works as expected.

    I'm just not sure why it doesn't work the same via 'test' or via connecting to the URL.

    I'm attempting to access the parameters via the EVENT variable:

    def lambda_handler(event, context):

    If I then perform:

    print(event)

    I see the differences described above for how it looks from API Gateway (or test button) and how it looks from someone hitting the Function URL.

    • C

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions