Ajay
Ajay

Reputation: 197

AWS API Gateway Input/Output Mapping

I am sending a response from lambda to API gateway but api gateway always show status as 200 irrespective of error status received in response body from lambda function

Upvotes: 0

Views: 491

Answers (1)

Ajay
Ajay

Reputation: 197

I have below piece of code written in python:

import psycopg2
import json

db_endpoint = 'XYZ'
db_port = 5432
db_username = 'XYZ'
db_password = 'XYZ'
db_name = 'XYZ'

def lambda_handler(event, context):
    print("event is ----", event)
    project_id = event['project_id']
    identifier = 'IAM_USER'
    array_of_commands = None
    
    try:
        # Establish a connection to the database
        connection = psycopg2.connect(user=db_username, password=db_password, host=db_endpoint, database=db_name)
        
        try:
            # Create a cursor object to execute SQL queries
            with connection.cursor() as cursor:
                # Execute the SQL query
                query = f"SELECT value from project_datas where project_id = '{project_id}' and identifier= '{identifier}' "
                cursor.execute(query)
                
                # Fetch all the results
                results = cursor.fetchall()
                
                if results:
                    array_of_commands = results[0][0]['cred_value']
                    print("results-----", results)
                    print("array_of_commands-----", array_of_commands)
                else:
                    
                    # Return an error status code indicating no results found
                    response = {

                        'status': 404,
                        'headers': {
                            'Content-Type': 'application/json'
                        },
                        'body': 'No results found'
                    }
                    raise Exception(json.dumps(response))
        
        except psycopg2.Error as e:
            # Return an error status code and error message
            response = {
                'status': 500,
                'headers': {
                    'Content-Type': 'application/json'
                },
                'body': f"Error executing SQL query: {e}"
            }
        
        finally:
            # Close the cursor
            cursor.close()
    
    except psycopg2.Error as e:
        # Return an error status code and error message
        response = {
            'status': 500,
            'headers': {
                'Content-Type': 'application/json'
            },
            'body': f"Error connecting to the database: {e}"
        }
    
    finally:
        # Close the database connection
        if connection:
            connection.close()
    
    if array_of_commands is not None:
        # Return a success status code with the result
        response=array_of_commands
    
    return response

At API gateway, I have to perform 2 actions:-

  1. Add status in Method Response
  2. Integration Response

enter image description here

Click on Method Response

enter image description here

Click on Integration Response enter image description here

Field Value Lambda Error Regex ."status": 404. Mapping Template $input.path('$.errorMessage')

enter image description here

Now at API gateway test your method

Upvotes: 0

Related Questions