简体   繁体   中英

How to know that in lambda,when to use event and event.body

I am new on writing lambda using nodejs. I need to write an API to let user login and send back an access token.

So I study others example and I found that sometimes when I write a post method the body (eg {'username':'***', 'password': '***'} ) can be accessed let say

exports.handler = async event => { console.log(event.body) }

and sometimes can only be called by

exports.handler = async event => { console.log(event) }

I have tried to use postman to check the difference between their codes and my code. Even if I copy the example I can only access the body in the event but not event.body . Can anyone explain me why there is this difference?

This is one of the differences between Lambda Proxy Integration and the Lambda Non-Proxy Integration .

With the first one you always get body as a string in event body.event .

Whereas with the Non-Proxy integration you can specify how does the request from client map to event using Mapping Tempaltes .

Lambda itself has no awareness of or constraints¹ on what event will contain -- that is entirely at the discretion of the caller that invoked the Lambda function.

When AWS services invoke Lambda functions they often use a common standard format that includes everything in the array event.Records (which, in some cases, like S3 event notifications and Lambda@Edge triggers, will always contain exactly one member). Other use cases include an entirely different structure.

What to expect is determined by the service, so you'll want to review the documentation for the service that is invoking the lambda function. The service should be consistent, overall, though there may be attributes that appear or disappear depending in the specifics of the trigger.

Using AWS Lambda with Other Services in the AWS Lambda Developer Guide discusses this in more detail, and provides links to some service-specific documentation for some of the AWS services that interoperate with Lambda.

And, of course, a Lambda function doesn't necessarily need to be invoked by another AWS service at all -- you can invoke a Lambda function from your own code, or even from inside another Lambda function (even recursively), or from the Lambda console, and in these cases the payload would be whatever you send.


¹ no constraints is not strictly true, since the Lambda API expects the input payload to be JSON, but this isn't relevant in the sense being discussed, here. The Node.js runtime environment deserializes the JSON into event transparently before calling your handler. The " event object" (as it is commonly called) is indeed typically an object. Exactly where/how/whether this is constrained (as opposed to, say, event being an array or scalar -- which I have never encountered but seems possible) is also beyond the intended scope of my choice of the phrase "no constraints."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM