简体   繁体   English

如何使用API​​网关将参数传递给Lambda函数以从DynamoDB查询项目?

[英]How to pass parameters to a Lambda function using API gateway for querying items from DynamoDB?

I have a Lambda function to query data from DynamoDB table. 我有一个Lambda函数,用于从DynamoDB表查询数据。 The Lambda function is as follows: Lambda函数如下:

'use strict';

var AWS = require('aws-sdk'),
documentClient = new AWS.DynamoDB.DocumentClient(); 

exports.listItems = function(event, context, callback){
var params = {
TableName : event.tablename,
IndexName : "active_flag-index",
KeyConditionExpression: "#active = :active",
FilterExpression: "#deliverable = :deliverable and #type = :type",
ProjectionExpression: "#name, price, item_description, item_type",
ExpressionAttributeNames:{
    "#active": "active_flag",
    "#deliverable": "deliverable_flag",
    "#name": "name",
    "#type": "item_type"
},
ExpressionAttributeValues: {
    ":active": "active",
    ":deliverable": "deliverable",
    ":type": event.type
}
};
documentClient.query(params, function(err, data) {
if (err) {
    console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
    console.log("Query succeeded.");
    data.Items.forEach(function(item) {
        console.log(" -", item.name + ": " + item.price);
    });
}
});
}

The test parameters are { "tablename": "vijayarams_items", "type": "Main Dish" } Using this test parameters, the items corresponding to Main Dish are retrieved successfully. 测试参数为{“ tablename”:“ vijayarams_items”,“ type”:“ Main Dish”}使用此测试参数,可以成功检索与Main Dish相对应的项目。 Now, I'm unsure how to pass these parameters using API to invoke this Lambda function. 现在,我不确定如何使用API​​传递这些参数来调用此Lambda函数。 I've created an API with GET method but the GET method doesn't use request Body to send parameters. 我已经使用GET方法创建了API,但是GET方法未使用请求正文发送参数。 Please enlighten me on how to proceed further. 请启发我进一步进行操作。 I'm able to create table, update items using POST method using AJAX and passing parameters to body. 我能够创建表,使用AJAX的POST方法更新项目并将参数传递给正文。 I'm just unable to query items from the table using the set parameters. 我只是无法使用设置的参数从表中查询项目。

Typically, a REST API will pass parameters through the Query string, such as : 通常,REST API将通过查询字符串传递参数,例如:

GET /resources?param1=value1&param2=value2 . GET /resources?param1=value1&param2=value2

You can define your parameters at API Gateway Level as described here : https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#setup-method-request-parameters 您可以按照以下说明在API网关级别定义参数: https : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#setup-method-request -参数

Then in your Lambda code, you need to read the values passed by the API Gateway in the incoming request and use them to build your DynamoDB params object. 然后,在Lambda代码中,您需要读取传入请求中API网关传递的值,并使用它们来构建DynamoDB params对象。 The exact format of the incoming request is here : https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format 传入请求的确切格式在这里: https : //docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for- lambda输入格式

I would suggest you to read this tutorial, it explains all the details, steps by steps. 我建议您阅读本教程,它逐步解释了所有详细信息。 https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 API 网关将事件参数传递给 AWS Lambda function? - How to pass event parameters to AWS Lambda function using API Gateway? 如何使用aws中的cloudformation在api网关中请求参数并将其传递给lambda函数? - How can I request parameters in api gateway using cloudformation in aws and pass it down to lambda function? 如何使用AWS API Gateway将URL参数传递给Lambda函数? - How do you pass URL Parameters to a Lambda function using AWS API Gateway? 从网页使用API​​ Gateway和Lambda写入DynamoDB表 - Writing to DynamoDB table using API Gateway and Lambda from a Web page How do I link a DynamoDB, Lambda Function, or Gateway API to an external API with a POST function? - How do I link a DynamoDB, Lambda Function, or Gateway API to an external API with a POST function? 如何在 API 网关中传递路径参数来调用 lambda 函数? - How to pass a path parameter in API gateway to invoke lambda function? 如何将POST变量传递给运行Lambda函数的AWS API Gateway? - How to pass POST variables to an AWS API Gateway running a lambda function? 如何使用 AWS CDK 将阶段变量传递给 api 网关中的 lambda 函数? - How to pass a stage variable to lambda function in api gateway using AWS CDK? 如何使用 python lambda function 通过 Z8A5DA52ED1206747D8AAZGatewayA70 传递和读取授权不记名令牌? - How to pass and read authorization bearer-token using python lambda function through api gateway? 如何将数据从AWS API Gateway自定义授权者传递到AWS Lambda函数? - How to pass data from AWS API Gateway Custom Authorizer to an AWS Lambda function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM