AWS Lambda:澄清从事件对象检索数据 [英] AWS Lambda: Clarification on retrieving data from event object

查看:126
本文介绍了AWS Lambda:澄清从事件对象检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我认为我对如何从lambda函数&中提取/传递数据有基本的误解.我正在寻求澄清

At the moment I think I have a foundational misunderstanding of how to extract/pass data from lambda functions & I'm seeking clarification

示例:假设我想将一些数据传递给lambda函数(即{"hello":"world"}),并通过在

Example: Let's say I want to pass some data to a lambda function ie {"hello":"world"} and do so by passing data to that Lambda function by creating a REST endpoint in AWS API Gateway

目前,我了解有三种提取数据的方法:

At the moment I understand there are three ways to extract data:

1)event.queryStringParameters(有意义)

1) event.queryStringParameters (makes sense)

例如我们可以将查询参数附加到请求URL: https://fakefakefake.execute-api.us-west-2.amazonaws.com/test/myapi?hello=world 并在lambda函数中:

ex. We can attach query parameters to the request URL: https://fakefakefake.execute-api.us-west-2.amazonaws.com/test/myapi?hello=world and in the lambda function:

const data = event.queryStringParameters.hello; // 'world'

2)event.body(由于"Lambda代理集成",这是有道理的)

2) event.body (makes sense & this is possible because of "Lambda Proxy Integration")

例如如果我们使用 Lambda代理集成(即转发所有数据),我们可以通过event.body&在lambda函数中(但请确保JSON.parse event.body,因为Lambda代理集成将通过字符串化JSON和无效/真实" JSON传递):

ex. If we attach the data in the body of the POST/PUT/etc request using Lambda Proxy Integration (ie forward all the data), we can access it via event.body & within the lambda function (but making sure to JSON.parse the event.body since Lambda Proxy Integration will pass through stringified JSON & not valid/"real" JSON):

const parsedBody = JSON.parse(event.body); // should wrap in try/catch
const data = parsedBody.hello; // 'world'

3)直接在事件对象上(不清楚)

3) Directly on the event object (Unclear)

例如目前尚不清楚这种情况-通过API网关中的REST端点设置将数据传递给lambda函数,然后可以从事件对象直接访问它?

ex. This case is unclear at the moment-- pass data to the lambda function from a REST endpoint setup in API Gateway where it is then accessible directly from the event object?

const data = event.hello; // 'world'

在情况3中,如何在Lambda函数中的事件对象上直接"传递数据的示例是什么?我认为这种情况要求我在设置API/Lambda时创建一个映射模板",但是我仍然不清楚.

What is an example of how to pass data "directly" on the event object in a Lambda function like in case #3? I THINK this case requires that I create a "mapping template" when setting up the API/Lambda but I'm still unclear.

对于一个简单的Node脚本,情况2似乎具有从字符串化的JSON解析事件主体的开销",因此这是可以理解的缺点,但是除了如何做到这一点之外,为什么情况3还是何时情况3是更理想的方法?

For a simple Node script, case 2 appears to have the "overhead" of parsing event body from stringified JSON so that's an understandable downside, but in addition to how to do it why or when would Case 3 be a more desirable approach?

Lambda代理集成:通过API网关在AWS Lambda中获取json正文

Lambda proxy integration: Getting json body in aws Lambda via API gateway

Lambda代理集成(AWS示例):

Lambda proxy integration (AWS example): https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

Lambda代理集成与Lambda代理(上述选项#2与选项#3):

Lambda Proxy Integration vs Lambda Proxy (option #2 vs option #3 above): Lambda Integration vs. Lambda Proxy: Pros and Cons

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

推荐答案

Lambda是独立服务,不需要与API Gateway集成. queryStringParametersbodybody mapping templates,所有这些都不特定于Lambda,而是特定于Lambda-API网关集成.

Lambda is standalone service that doesn't need to be integrated with API Gateway. queryStringParameters, body, body mapping templates, all of this is specific not to Lambda, but to Lambda - API Gateway integration.

如果您将Lambda与其他服务一起使用,则数据通常直接通过event对象传递,因此没有太多理由以其他方式传递数据.

If you are using Lambda with other services then the data is usually passed directly via event object and there is not much of a reason to pass it in some other way.

例如,您可以将Lambda函数订阅到S3存储桶,并使用该功能以编程方式处理事件,例如将文件上传到存储桶.在这种情况下,存储桶名称,对象密钥,对象数据,元数据等信息将直接通过event对象传递.

For example, you can subscribe Lambda function to S3 bucket and use it to programatically process events such as file being uploaded to your bucket. In this case, information such as bucket name, object key, object data, metadata, ... will be passed directly via event object.

而且,在将Lambda与API Gateway一起使用时,为什么要使用body mapping templates直接通过event对象将数据传递到Lambda函数?因为您可以更轻松地重用该功能用于其他目的(如果在您的方案中可行),因为您的Lambda函数将具有更简单的接口,而不是与API Gateway集成紧密相关的接口.

And, when using Lambda with API Gateway, why would you want to use body mapping templates to pass data to your Lambda function directly via event object? Because you can reuse that function much easier for other purposes (if viable in your scenario), because your Lambda function will have much simpler interface, instead of one that is tight to API Gateway integration.

例如,您可以具有一个函数,该函数对传入的数字执行一些计算,可以通过API网关调用该数字,也可以直接从您的应用程序调用它.如果期望使用event.xevent.y而不是某些event.queryStringParameter.x(可能在API网关之外可能具有零意义),则使用该功能将更加容易.

For example, you can have a function that performs some calculations on passed in numbers which you can call via API Gateway as well as to call it directly from your application. It will be much easier to work with such function if it expects event.x and event.y instead of some event.queryStringParameter.x which might make zero sense outside of API Gateway.

这篇关于AWS Lambda:澄清从事件对象检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆