Function URL returns null but Lambda function is working

0

i have a function URL linked to a lambda function that is returning 'null' when referenced. The function calls AWS rekognition and the json response is formatted like so:

Test Event Name test1

Response null

Function Logs START RequestId: b62907a6-2d92-46f2-a1a8-815e4016e2f6 Version: $LATEST { "BoundingBox": { "Width": 0.2756405174732208, "Height": 0.21851715445518494, "Left": 0.3744633197784424, "Top": 0.5871027112007141 }, "AgeRange": { "Low": 38, "High": 46 }, "Smile": { "Value": false, "Confidence": 94.42446899414062 }, "Eyeglasses": { "Value": false, "Confidence": 91.79085540771484 }, "Sunglasses": { "Value": false, "Confidence": 99.99473571777344

So when the function URL is invoked from the browser it is referencing the JSON response (which is NULL). The relevant informaton is contained under "Functional Logs" How can this be rectified? I am using the DetectFaces operation (https://docs.aws.amazon.com/rekognition/latest/dg/faces-detect-images.html)

질문됨 9달 전892회 조회
1개 답변
0
수락된 답변

As you can see here, the response format should be like this:

{
   "statusCode": 201,
    "headers": {
        "Content-Type": "application/json",
        "My-Custom-Header": "Custom Value"
    },
    "body": "{ \"message\": \"Hello, world!\" }",
    "cookies": [
        "Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
        "Cookie_2=Value2; Max-Age=78000"
    ],
    "isBase64Encoded": false
}

So make sure to construct your response correctly, embedding the Rekognition response in the body attribute and setting the statusCode.

profile pictureAWS
전문가
Uri
답변함 9달 전
profile picture
전문가
검토됨 한 달 전
  • Hi Uri, thanks for the reply. Do you mean something like this?

    import json import boto3 import requests

    client = boto3.client('rekognition', region_name='ap-southeast-2')

    def lambda_handler(event, context): # TODO implement bucket_name = "intelli1a" file_name = "msic.jpg"

    response = client.detect_faces(Image={'S3Object':{'Bucket':bucket_name,'Name':file_name}},
                                   Attributes=['ALL'])
    
    #Process response
    for faceDetail in response['FaceDetails']:
        print(json.dumps(faceDetail, indent=2))
    
    #Insert JSON response format
    expected_response = {
        "statusCode": 201,
        "headers": {
            "Content-Type": "application/json",
            "My-Custom-Header": "Custom Value"
        },
        "body": "{ \"message\": \"Hello, world!\" }",
        "cookies": [
            "Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
            "Cookie_2=Value2; Max-Age=78000"
        ],
        "isBase64Encoded": False
    }
    
  • got it Uri, just a small change and it worls. Thanks so much for your input, really appreciate it

    #Process response
    for faceDetail in response['FaceDetails']:
        return {  'statusCode' : 200,
        'body': json.dumps(faceDetail, indent=2)
    
  • Happy to hear that it works, however, looking at your code, I see that you return inside a loop. This means that your function will operate correctly only if there is a single face in the image.

    You should construct an array of all faces in the loop and then, after the loop, return the array as part of the body.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠