[英]API Gateway with Lambda non-proxy integration
I am new to AWS API Gateway, hence I am struggling even with a simple app as the one below.我是 AWS API 网关的新手,因此即使使用以下简单的应用程序,我也很挣扎。 I have a DynamoDB table as below:
我有一个 DynamoDB 表,如下所示:
I have created a REST API with a Lambda/proxy integration.我创建了一个带有 Lambda/代理集成的 REST API。 I have defined a resource named "/query " and a GET method.
我已经定义了一个名为“/query”的资源和一个 GET 方法。 The Lambda function Python code is shown below.
Lambda function Python 代码如下所示。
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,我将 API 部署为开发人员,当我在新浏览器 window 或 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.现在,我正在尝试使用 Lambda 非代理进行复制。
On Method Request / URL Query String Parameters I define在方法请求/URL 查询字符串参数我定义
and on Integration Request / Mapping Templates I define和我定义的集成请求/映射模板
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):在我部署到一个阶段之前,我使用 {query} catName=bella 测试了 GET 方法执行,我得到了以下响应正文(我故意不包括响应标头和日志,所以我不会填塞这个问题 - 我希望你明白了):
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.
然而,在 72 小时后,我遗憾地承认我不知所措。 Any help would be most appreciated.
非常感激任何的帮助。
Thanks in advance from Theodoros.在此先感谢西奥多罗斯。
This line of your code这行代码
query_name=event['queryStringParameters']['catName']
is looking for queryStringParameters in event.正在寻找事件中的 queryStringParameters。 However, your custom request mapping template has already read the query parameters and created a key catName with a value.
但是,您的自定义请求映射模板已经读取了查询参数并创建了一个带有值的键 catName。 So you are getting a key error because there is no key queryStringParameters.
所以你得到一个关键错误,因为没有关键的 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.标记是正确的,在您的 lamda 开头添加一个 print(event) 并为您的 lamda 打开日志记录。 You can also run a lambda or an API test in the console.
您还可以在控制台中运行 lambda 或 API 测试。 In the logs you will see what is being passed in.
在日志中,您将看到正在传递的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.