Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

amazon web services - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...