简体   繁体   中英

Getting message: “Internal server error” while invoking AWS Lambda via API Gateway. The function is working but the response is this error

I am accessing the following function created in lambda:

import json
import boto3
from get_json_s3 import get_json

def lambda_handler(event, context):
    
    print(event)
    jobId = event['queryStringParameters']['jobID']
    
    try:
        print(jobId)
        response = get_json(None,f'{jobId}.json')
        print(response)
        
    except:
        return {
          'statusCode': 200,
          'body': json.dumps('jobID not found')  
        }
    print("success")
    
    return {
          'statusCode': 200,
          'body': json.dumps(response)  
        }

get_json is defined as follows:

import json
import boto3

s3 = boto3.client('s3')

def get_json(filegroup, filename):
    bucket = 'bucket name'
    if filegroup!=None:
        key = f'{filegroup}/{filename}'
    else:
        key = f'{filename}'
    
    response = s3.get_object(Bucket = bucket, Key = key)
    content = response['Body']
    jsonObject = json.loads(content.read())
    return jsonObject

I have created a API gateway with lambda as a proxy. I have added the invoke access and apigateway access to lambda function but I keep getting 502: Internal Server Error.

The lambda is doing the function it is supposed to do correctly as I can see from Cloud watch logs. It is being triggered correctly via APIgateway too. Only the response part is not working

Here is the common issues which might be able to help you diagnose the issue.

  1. It doesn't have a right permission to allow API Gateway invoke your Lambda function.
  2. It is set as AWS Service Proxy to your Lambda function, the response from your Lambda function doesn't return the response in the proper format.

I recommend you to enable logging feature on API Gateway side or you can use the test invoke feature on API Gateway console using this doc .

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