Home > database >  API Gateway with Lambda non-proxy integration
API Gateway with Lambda non-proxy integration

Time:07-26

I am new to AWS API Gateway, hence I am struggling even with a simple app as the one below. I have a DynamoDB table as below:

enter image description here

I have created a REST API with a Lambda/proxy integration. I have defined a resource named "/query " and a GET method. The Lambda function Python code is shown below.

import json
import boto3
from boto3.dynamodb.conditions import Key

mydb=boto3.resource('dynamodb',region_name='us-east-1')
table=mydb.Table('catula')

def lambda_handler(event, context):
    #query_name=event['catName']
    query_name=event['queryStringParameters']['catName']
    response=table.query(KeyConditionExpression=Key('catName').eq(query_name))['Items']
    
    #print(response)
    
    return {
        "statusCode": 200,
        "body": json.dumps(response)
            }

I deploy the API as dev and when I invoke the dev stage with the proper query string, either on a new browser window or with Postman,

https://km7nniniyc.execute-api.us-east-1.amazonaws.com/dev/query?catName=bella

I get the desired response,

[
    {
        "isFeral": "true",
        "color": "calico",
        "age": "2",
        "catName": "bella"
    }
]

Now, I am trying to replicating with Lambda non-proxy.

On Method Request / URL Query String Parameters I define

enter image description here

and on Integration Request / Mapping Templates I define

enter image description here

Before I even deploy to a stage, I test the GET Method Execution with {query} catName=bella and I get the following Response Body (I intentionally do not include the Response Headers and Logs so I do not cram this question - I hope you get the idea):

enter image description here

I know the error or omission I am doing is something rather basic and simple. Yet, after 72 hrs I regret to admit that I am at a loss. Any help would be most appreciated.

Thanks in advance from Theodoros.

CodePudding user response:

This line of your code

query_name=event['queryStringParameters']['catName']

is looking for queryStringParameters in event. However, your custom request mapping template has already read the query parameters and created a key catName with a value. So you are getting a key error because there is no key queryStringParameters. This line of your code, which is commented out, looks more correct.

#query_name=event['catName']

Mark is correct, add a print(event) at the start of your lamda and turn on logging for your lamda. You can also run a lambda or an API test in the console. In the logs you will see what is being passed in.

  • Related