1

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

2
  • 1
    The way to debug this is turn on API Gateway logging to see what the actual error is. docs.aws.amazon.com/apigateway/latest/developerguide/… Commented Jan 6, 2021 at 21:01
  • Thanks @MarkB , it helped. Our response was not in a proper format. Commented Jan 10, 2021 at 13:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The latter was the case. The return from lambda was not a proper JSON. rectifying it made it work. We were able to confirm this from the logging you suggested. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.