1
00:00:00,690 --> 00:00:06,900
In this video, we are going to see lambda function code and the best practices related to the Lambda

2
00:00:06,900 --> 00:00:07,740
Lifecycle.

3
00:00:08,580 --> 00:00:12,810
Lambda runs instance of your function to process events.

4
00:00:13,080 --> 00:00:20,640
You can invoke your function directly using the Lambda API or you can configure as an A service or resources

5
00:00:20,640 --> 00:00:22,440
in order to invoke your function.

6
00:00:23,100 --> 00:00:30,000
You can find in the image on the slide, which is the lambda function with Node.js version is you can

7
00:00:30,000 --> 00:00:37,680
see that we have a lambda function name that we export in the handle method and we have defined function

8
00:00:37,680 --> 00:00:40,710
which has event, context and callback parameters.

9
00:00:42,030 --> 00:00:48,510
With these parameters, we develop actual business function and after that perform some callback operations

10
00:00:48,510 --> 00:00:52,680
that can be returned in response or not response for asynchronous invocations.

11
00:00:53,740 --> 00:00:59,800
Full Lambda function has called to process the events that you pass into the function or that other

12
00:01:00,250 --> 00:01:03,520
services sent to the function with an object.

13
00:01:04,600 --> 00:01:10,060
The event object contains all the information about the event that triggered this lambda function.

14
00:01:10,210 --> 00:01:16,780
For example, if lambda invokes from the API gateway, then an HTTP request will be the information

15
00:01:16,780 --> 00:01:17,560
of event.

16
00:01:18,730 --> 00:01:27,190
The context object contains info about the runtime or lambda functions is executing in and after that

17
00:01:27,190 --> 00:01:30,070
we do all work inside our lambda function.

18
00:01:30,070 --> 00:01:37,390
We simply call the callback function with the results or the error and will respond to the HTTP request

19
00:01:37,420 --> 00:01:41,800
with it if we are invoking synchronous lambda function.

20
00:01:42,450 --> 00:01:48,420
So as you can see that there are several key concepts when writing lambda function code like event,

21
00:01:48,690 --> 00:01:54,930
you can see on the slide we have an event trigger execution, environment layer and so on.

22
00:01:54,930 --> 00:01:58,440
There is lots of key concepts when writing the lambda function.

23
00:01:58,620 --> 00:02:02,370
So let's see these key concepts for our lambda function.

24
00:02:02,580 --> 00:02:10,110
So the first, first of all, we should define the runtime so we select the runtime as part of configuring

25
00:02:10,110 --> 00:02:14,130
the function and the lambda loads and the runtime one.

26
00:02:14,130 --> 00:02:15,660
Initializing the environment.

27
00:02:15,660 --> 00:02:22,140
You can think lambda is the code container and when the lambda invocation happens, lambda sitting up

28
00:02:22,590 --> 00:02:24,150
is a container with that runtime.

29
00:02:24,150 --> 00:02:30,720
So runtime is very important and several runtimes that can be configured during the lambda.

30
00:02:31,890 --> 00:02:39,150
So after we write the function, when we are writing the function, the another key concept is handler

31
00:02:39,150 --> 00:02:39,750
method.

32
00:02:40,080 --> 00:02:43,470
Our function runs starting at the handle method.

33
00:02:43,530 --> 00:02:48,000
It is a best practice to separate the lambda handle from your business logic.

34
00:02:48,000 --> 00:02:56,220
So you should be separate your, for example, database connections and other pair loading operations

35
00:02:56,340 --> 00:02:57,780
from the handle method.

36
00:02:57,810 --> 00:03:00,960
Handle method should be only include your business logic.

37
00:03:00,960 --> 00:03:06,360
This is the best practice when writing lambda functions and another key concept is function.

38
00:03:06,690 --> 00:03:09,630
A function is a resource that you can invoke to run your code.

39
00:03:09,630 --> 00:03:15,990
In Lambda, a function has to code process the events that you pass into the function or that other

40
00:03:16,410 --> 00:03:18,210
services sent to the function.

41
00:03:20,190 --> 00:03:23,100
We can say that another key concept is triggered.

42
00:03:23,190 --> 00:03:27,120
A trigger is the resource or configuration that invokes a lambda function.

43
00:03:27,240 --> 00:03:33,210
Triggers includes a services that you can configure to invoke a function or event source mapping.

44
00:03:33,690 --> 00:03:39,450
An event source mapping is a resource in lambda that reads items from a stream or queue and invokes

45
00:03:39,450 --> 00:03:40,290
a function.

46
00:03:40,380 --> 00:03:45,540
This is the invoking lambda functions and using lambda with other services.

47
00:03:46,110 --> 00:03:49,530
And of course we have an event as a key concept.

48
00:03:49,650 --> 00:03:55,800
An event is a JSON formatted document that contains data for a lambda function in order to process.

49
00:03:56,100 --> 00:04:02,310
So the runtime converts the events to an object and passes into the your function code.

50
00:04:02,820 --> 00:04:08,280
When you invoke a function, you determine the structure and contents of the event.

51
00:04:08,400 --> 00:04:17,010
So if we call back to the previous slide, when a function is triggering from somewhere in a list,

52
00:04:17,040 --> 00:04:20,130
this event object we can see in the function.

53
00:04:20,130 --> 00:04:24,540
This event object will be including all the information as a JSON format.

54
00:04:24,540 --> 00:04:30,180
It is very important and we will see in the upcoming section how the event will be load and how we can

55
00:04:30,180 --> 00:04:34,290
get the parameters values from the event object into our lambda function.

56
00:04:34,440 --> 00:04:40,940
But basically this event is a JSON format document and contains all of the data, a lambda, which is

57
00:04:41,040 --> 00:04:42,990
services from the process.

58
00:04:43,650 --> 00:04:48,570
So now the another key concept is execution environment.

59
00:04:48,720 --> 00:04:54,570
An execution environment provides a secure and isolated runtime environment for our lambda function,

60
00:04:54,900 --> 00:05:00,630
an execution environment, manage the processes and resources that are required to run the function.

61
00:05:00,720 --> 00:05:05,970
So when Lambda is invoked, invoke invoking from the other services.

62
00:05:05,970 --> 00:05:11,970
The concurrent lambda invocations can be happened and these are all execution environments isolated

63
00:05:11,970 --> 00:05:13,500
between each other.

64
00:05:14,900 --> 00:05:19,370
And now we can continue with the key concept of the Lambda function code.

65
00:05:19,550 --> 00:05:22,310
Another key key concept is layer.

66
00:05:22,580 --> 00:05:28,760
A lambda layer is a zip file archive that contains additional code or other content.

67
00:05:29,000 --> 00:05:33,890
A layer can contain libraries, a custom runtime data or configuration files.

68
00:05:34,310 --> 00:05:39,950
Lambda layers provide a convenient way to package libraries and other dependencies that you can use

69
00:05:39,950 --> 00:05:41,270
with your lambda function.

70
00:05:41,480 --> 00:05:47,000
Using layers reduce the size of the uploaded deployment archives and makes it faster to deploy your

71
00:05:47,000 --> 00:05:47,540
code.

72
00:05:47,540 --> 00:05:50,600
So this is the best practice to using the layers.

73
00:05:50,600 --> 00:05:56,870
When you have additional packages and dependencies, you can combine into the layer and you can directly

74
00:05:56,870 --> 00:05:59,030
use the layer into lambda functions.

75
00:05:59,030 --> 00:06:00,470
When you are writing the code.

76
00:06:00,470 --> 00:06:06,200
By this way, you can reduce the size of upload deployment and also fast to deploy your lambda functions.

77
00:06:06,230 --> 00:06:12,620
There is also promote code sharing and separation of responsibilities so that you can iterate faster

78
00:06:12,620 --> 00:06:14,130
on writing business logics.

79
00:06:14,720 --> 00:06:17,360
And another key concept is concurrency.

80
00:06:17,450 --> 00:06:23,750
Concurrency is the number of requests that your function is solving at any given time when your function

81
00:06:23,750 --> 00:06:24,680
is invoked.

82
00:06:24,680 --> 00:06:28,220
Lambda provisions an instance of it is processed to event.

83
00:06:28,310 --> 00:06:31,940
When the function code finished running, it can handle another request.

84
00:06:32,030 --> 00:06:38,300
So if the function is invoked again, while a request is still being processed, another instance is

85
00:06:38,300 --> 00:06:41,240
provisioned and increasing the functions concurrency.

86
00:06:41,480 --> 00:06:44,300
So another key concept is the destination.

87
00:06:44,450 --> 00:06:49,970
A destination is an added resource where lambda can send events from asynchronous invocation.

88
00:06:50,150 --> 00:06:56,540
So you can configure a destination for events that fail processing like setting to the letter queue.

89
00:06:56,570 --> 00:06:59,510
The network is very important for lambda functions.

90
00:06:59,510 --> 00:07:06,740
If you your function execution phase, you can set into the data queue for the destination and understand

91
00:07:06,740 --> 00:07:08,570
and don't lose any invocation.

92
00:07:08,570 --> 00:07:13,730
Some services also support a destination for events that are successfully processed.

93
00:07:15,190 --> 00:07:19,840
So now I would like to talk about best practices related to the lifecycle.

94
00:07:20,870 --> 00:07:27,740
So we can say that best practices do take advantage of the environment, reduce and check the background

95
00:07:27,740 --> 00:07:35,180
processes have completed manage database connections pooling with a database proxy persist states data

96
00:07:35,180 --> 00:07:40,040
externally minimize deployment, package size and complexity of dependencies.

97
00:07:40,040 --> 00:07:46,910
So these are the main best practices when writing the lambda and Lambda Lifecycles.

98
00:07:47,120 --> 00:07:53,990
And we can say that configure functions with resource based policies because resource page policy grants

99
00:07:53,990 --> 00:07:56,360
permission to invoke your lambda functions.

100
00:07:56,360 --> 00:08:03,050
It is very important to policy when you are giving the grant permissions to invoking your lambda functions.

101
00:08:03,290 --> 00:08:06,830
It is important to configure function with the resource based policy.

102
00:08:07,070 --> 00:08:13,370
An execution role defines a functions permission to interact with resources, and another best practice

103
00:08:13,370 --> 00:08:15,650
is using environment variables.

104
00:08:15,650 --> 00:08:22,790
We can use environment variable in order to store secrets securely and adjust your functions behavior

105
00:08:22,790 --> 00:08:24,290
without updating your code.

106
00:08:24,380 --> 00:08:30,410
So an environment variable is a pair of string that are stored in a functions virgin specific configuration.

107
00:08:30,650 --> 00:08:36,380
We can configure the environment value on the Islam, the configuration section, the underlying invocation

108
00:08:36,380 --> 00:08:41,990
environment for the runtime provides additional libraries as well as the environment variables that

109
00:08:41,990 --> 00:08:44,130
you can access from your function code.

110
00:08:44,690 --> 00:08:49,520
So we can also create your own environment variables in order to use in your code.

111
00:08:49,520 --> 00:08:55,190
When you publish a version, the environment variables are locked for that version along with the other

112
00:08:55,190 --> 00:08:56,660
virgin specific configuration.

113
00:08:56,660 --> 00:09:02,050
So this is a very powerful feature, as you can see that we have also seen best practices of Islam,

114
00:09:02,120 --> 00:09:03,080
the function code.

115
00:09:03,320 --> 00:09:08,090
In the next video we are going to work through a lambda with a management console.
