7

I'm following a tutorial on setting up AWS API Gateway with a Lambda Function to create a restful API. I have the following code:

import json

def lambda_handler(event, context):
    # 1. Parse query string parameters
    transactionId = event['queryStringParameters']['transactionid']
    transactionType = event['queryStringParameters']['type']
    transactionAmounts = event['queryStringParameters']['amount']

    # 2. Construct the body of the response object
    transactionResponse = {}
    # returning values originally passed in then add separate field at the bottom
    transactionResponse['transactionid'] = transactionId
    transactionResponse['type'] = transactionType
    transactionResponse['amount'] = transactionAmounts
    transactionResponse['message'] = 'hello from lambda land'

    # 3. Construct http response object
    responseObject = {}
    responseObject['StatusCode'] = 200
    responseObject['headers'] = {}
    responseObject['headers']['Content-Type'] = 'application/json'
    responseObject['body'] = json.dumps(transactionResponse)

    # 4. Return the response object
    return responseObject

When I link the API Gateway to this function and try to call it using query parameters I get the error:

{
"message":"Internal server error"
}

When I test the lambda function it returns the error:

{
  "errorMessage": "'transactionid'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 5, in lambda_handler\n    transactionId = event['queryStringParameters']['transactionid']\n"
  ]

Does anybody have any idea of what's going on here/how to get it to work?

16
  • What URL path/query did you submit and what does event['queryStringParameters'] contain? Commented Sep 23, 2020 at 19:47
  • I submitted my-api-gateway-url.com/test?transactionid=5&type=purchase&amount=50 and from my understanding event['queryStringParameters'] is a dictionary if the API Gateway passes a query string parameter Commented Sep 23, 2020 at 19:58
  • Are you using Lambda proxy integration? What does event['queryStringParameters'] actually contain? Commented Sep 23, 2020 at 20:06
  • yes, and how do I check that? It should contain the query string parameters, no? Commented Sep 23, 2020 at 20:31
  • The docs explain how to configure Lambda proxy integration. Check that, and then actually print out the values of event and event['queryStringParameters'] (preferably as prettified JSON) so you can see what's actually being presented to your Lambda function. Commented Sep 23, 2020 at 20:49

5 Answers 5

11

I recommend adding a couple of diagnostics, as follows:

import json

def lambda_handler(event, context):
    print('event:', json.dumps(event))
    print('queryStringParameters:', json.dumps(event['queryStringParameters']))

    transactionId = event['queryStringParameters']['transactionid']
    transactionType = event['queryStringParameters']['type']
    transactionAmounts = event['queryStringParameters']['amount']
    // remainder of code ...

That way you can see what is in event and event['queryStringParameters'] to be sure that it matches what you expected to see. These will be logged in CloudWatch Logs (and you can see them in the AWS Lambda console if you are testing events using the console).

In your case, it turns out that your test event included transactionId when your code expected to see transactionid (different spelling). Hence the KeyError exception.

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

Comments

3

just remove ['queryStringParameters']. the print event line shows the event i only a array not a key value pair. I happen to be following the same tutorial. I'm still on the api gateway part so i'll update once mine is completed.

Whan you test from the lambda function there is no queryStringParameters in the event but it is there when called from the api gateway, you can also test from the api gateway where queryStringParameters is required to get the values passed.

2 Comments

so you're saying event['transactionid'] should work?
or add queryStringParameters into your test json on the lambdafunction { "queryStringParameters": { "transactionid": 123, "type": "type", "amount": 123 } }
1

Make sure you select the checkbox 'Use Lambda Proxy integration' at the point of choosing your Lambda function integration.

Comments

0

The problem is not your code. It is the Lambda function intergration setting. Please do not enable Lambda function intergration setting . You can still attach the Lambda function without it. Leave this unchecked.

Comments

-1

It's because of the typo in responseObject['StatusCode'] = 200.

'StatusCode' should be 'statusCode'.

I got the same issue, and it was that.

Comments

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.