1
00:00:02,120 --> 00:00:08,590
In the last lecture, we learned how to look into the lambda function and we saw the object being passed

2
00:00:08,590 --> 00:00:13,120
to that function, that request object with all the request metadata.

3
00:00:13,120 --> 00:00:16,400
Now as I said at the end of the last lecture, I don't like that,

4
00:00:16,430 --> 00:00:21,400
I like my lambda functions to be clean and to only work with the data I need.

5
00:00:21,400 --> 00:00:28,300
I think the lambda functions shouldn't need to parse any incoming event and extract exactly what it

6
00:00:28,300 --> 00:00:28,800
needs if

7
00:00:28,840 --> 00:00:33,110
it doesn't need 90% of that data, instead

8
00:00:33,160 --> 00:00:35,540
that should be the job of the API Gateway.

9
00:00:35,740 --> 00:00:36,240
There,

10
00:00:36,250 --> 00:00:41,560
you should ensure that you only pass to your action, lambda in this case,

11
00:00:41,560 --> 00:00:43,660
what this action needs.

12
00:00:44,140 --> 00:00:50,110
So I will go back to integration request and uncheck this lambda proxy integration, confirm and confirm

13
00:00:50,110 --> 00:00:56,730
again and we're back to the old world thereafter with the body mapping template.

14
00:00:56,740 --> 00:01:02,930
Now again, since no template is selected, the whole request body is forwarded to lambda.

15
00:01:02,950 --> 00:01:16,920
So now if we reword this to simply login the event and we test this again by passing an object where

16
00:01:16,920 --> 00:01:25,020
we have name, Max, I'll omit the age here, we see that we get this back and this is back to the state we were at.

17
00:01:25,470 --> 00:01:25,800
Now

18
00:01:25,830 --> 00:01:33,510
I was mentioning a more elegant way of extracting data and only passing data to lambda it needs,

19
00:01:33,510 --> 00:01:37,770
this way uses body mapping templates,

20
00:01:37,770 --> 00:01:41,130
let me change the structure of the request body.

21
00:01:41,400 --> 00:01:51,990
Let's say we actually have a person data property here which is another object which then in turn holds

22
00:01:51,990 --> 00:01:53,450
that name.

23
00:01:53,460 --> 00:02:01,590
So this is the data we send and we maybe also have the age here, 28. This is the data we send to lambda, if we

24
00:02:01,590 --> 00:02:04,220
do send it, we get this returned.

25
00:02:04,710 --> 00:02:09,980
Now in the lambda function, let's say I don't just want to return this event data,

26
00:02:10,290 --> 00:02:14,960
instead what I want to do here is I log it,

27
00:02:15,210 --> 00:02:20,850
then I want to retrieve the age and I want to retrieve age on the event.

28
00:02:20,850 --> 00:02:29,680
However right now, I would have to use person data age, right because age is a property

29
00:02:29,730 --> 00:02:35,730
on this person data object here. I also get the name even though the lambda function is not interested

30
00:02:35,730 --> 00:02:36,740
in that

31
00:02:36,810 --> 00:02:45,510
and here, I could return age times two. Now if I save this and I click test again, we get back 56 which is twice

32
00:02:46,650 --> 00:02:47,350
28.

33
00:02:47,430 --> 00:02:48,890
So that makes sense

34
00:02:49,050 --> 00:02:54,100
but again, the lambda function has to care about the structure of the data it receives.

35
00:02:54,240 --> 00:02:59,280
It would be much better if the lambda function could simply access age on the event because it knows that

36
00:02:59,280 --> 00:03:05,720
there will be an age property and only age property because that is all this lambda function needs

37
00:03:06,060 --> 00:03:12,180
and this is where body mapping templates enter the stage. In the integration request and response,

38
00:03:12,240 --> 00:03:18,780
we can use them to map in the case of request, the data we pass to the action and in the case of response,

39
00:03:19,020 --> 00:03:21,410
data we get out of that action.

40
00:03:21,420 --> 00:03:28,170
So here integration request, we can scroll down and first of all switch this to when there are no templates

41
00:03:28,170 --> 00:03:29,390
defined.

42
00:03:29,400 --> 00:03:34,550
Now let's add a template, I'll name it application/json

43
00:03:34,710 --> 00:03:40,410
and with that, the request body will not be forwarded by default anymore but this template will be used,

44
00:03:40,860 --> 00:03:46,200
though just as for the response if this template is empty, it will still forward the request

45
00:03:46,500 --> 00:03:55,650
but if I simply set this to an empty object here and hit save and we now go back to testing this, let

46
00:03:55,650 --> 00:03:56,230
me hit

47
00:03:56,280 --> 00:04:08,120
test here, you see we get back null and we can check our cloud watch logs here, the latest stream to see

48
00:04:08,690 --> 00:04:12,070
that indeed, we get an empty object here,

49
00:04:12,230 --> 00:04:15,260
this is what we'll log the console here.

50
00:04:15,260 --> 00:04:21,440
The reason for this is that in our template, we transform the incoming data and simply pass on an

51
00:04:21,470 --> 00:04:24,360
empty object to our action, to lambda.

52
00:04:24,430 --> 00:04:29,050
Not super useful obviously but just showing what we can do with that.

53
00:04:29,100 --> 00:04:32,390
Now typically of course, we don't overwrite the data with an empty object,

54
00:04:32,390 --> 00:04:38,510
instead what we want to do here is do something more useful and use the powerful body mapping template

55
00:04:38,570 --> 00:04:40,150
language

56
00:04:40,430 --> 00:04:42,560
AWS API Gateway provides us with.
