In this video, we are going to see lambda function code and the best practices related to the Lambda
Lifecycle.
Lambda runs instance of your function to process events.
You can invoke your function directly using the Lambda API or you can configure as an A service or resources
in order to invoke your function.
You can find in the image on the slide, which is the lambda function with Node.js version is you can
see that we have a lambda function name that we export in the handle method and we have defined function
which has event, context and callback parameters.
With these parameters, we develop actual business function and after that perform some callback operations
that can be returned in response or not response for asynchronous invocations.
Full Lambda function has called to process the events that you pass into the function or that other
services sent to the function with an object.
The event object contains all the information about the event that triggered this lambda function.
For example, if lambda invokes from the API gateway, then an HTTP request will be the information
of event.
The context object contains info about the runtime or lambda functions is executing in and after that
we do all work inside our lambda function.
We simply call the callback function with the results or the error and will respond to the HTTP request
with it if we are invoking synchronous lambda function.
So as you can see that there are several key concepts when writing lambda function code like event,
you can see on the slide we have an event trigger execution, environment layer and so on.
There is lots of key concepts when writing the lambda function.
So let's see these key concepts for our lambda function.
So the first, first of all, we should define the runtime so we select the runtime as part of configuring
the function and the lambda loads and the runtime one.
Initializing the environment.
You can think lambda is the code container and when the lambda invocation happens, lambda sitting up
is a container with that runtime.
So runtime is very important and several runtimes that can be configured during the lambda.
So after we write the function, when we are writing the function, the another key concept is handler
method.
Our function runs starting at the handle method.
It is a best practice to separate the lambda handle from your business logic.
So you should be separate your, for example, database connections and other pair loading operations
from the handle method.
Handle method should be only include your business logic.
This is the best practice when writing lambda functions and another key concept is function.
A function is a resource that you can invoke to run your code.
In Lambda, a function has to code process the events that you pass into the function or that other
services sent to the function.
We can say that another key concept is triggered.
A trigger is the resource or configuration that invokes a lambda function.
Triggers includes a services that you can configure to invoke a function or event source mapping.
An event source mapping is a resource in lambda that reads items from a stream or queue and invokes
a function.
This is the invoking lambda functions and using lambda with other services.
And of course we have an event as a key concept.
An event is a JSON formatted document that contains data for a lambda function in order to process.
So the runtime converts the events to an object and passes into the your function code.
When you invoke a function, you determine the structure and contents of the event.
So if we call back to the previous slide, when a function is triggering from somewhere in a list,
this event object we can see in the function.
This event object will be including all the information as a JSON format.
It is very important and we will see in the upcoming section how the event will be load and how we can
get the parameters values from the event object into our lambda function.
But basically this event is a JSON format document and contains all of the data, a lambda, which is
services from the process.
So now the another key concept is execution environment.
An execution environment provides a secure and isolated runtime environment for our lambda function,
an execution environment, manage the processes and resources that are required to run the function.
So when Lambda is invoked, invoke invoking from the other services.
The concurrent lambda invocations can be happened and these are all execution environments isolated
between each other.
And now we can continue with the key concept of the Lambda function code.
Another key key concept is layer.
A lambda layer is a zip file archive that contains additional code or other content.
A layer can contain libraries, a custom runtime data or configuration files.
Lambda layers provide a convenient way to package libraries and other dependencies that you can use
with your lambda function.
Using layers reduce the size of the uploaded deployment archives and makes it faster to deploy your
code.
So this is the best practice to using the layers.
When you have additional packages and dependencies, you can combine into the layer and you can directly
use the layer into lambda functions.
When you are writing the code.
By this way, you can reduce the size of upload deployment and also fast to deploy your lambda functions.
There is also promote code sharing and separation of responsibilities so that you can iterate faster
on writing business logics.
And another key concept is concurrency.
Concurrency is the number of requests that your function is solving at any given time when your function
is invoked.
Lambda provisions an instance of it is processed to event.
When the function code finished running, it can handle another request.
So if the function is invoked again, while a request is still being processed, another instance is
provisioned and increasing the functions concurrency.
So another key concept is the destination.
A destination is an added resource where lambda can send events from asynchronous invocation.
So you can configure a destination for events that fail processing like setting to the letter queue.
The network is very important for lambda functions.
If you your function execution phase, you can set into the data queue for the destination and understand
and don't lose any invocation.
Some services also support a destination for events that are successfully processed.
So now I would like to talk about best practices related to the lifecycle.
So we can say that best practices do take advantage of the environment, reduce and check the background
processes have completed manage database connections pooling with a database proxy persist states data
externally minimize deployment, package size and complexity of dependencies.
So these are the main best practices when writing the lambda and Lambda Lifecycles.
And we can say that configure functions with resource based policies because resource page policy grants
permission to invoke your lambda functions.
It is very important to policy when you are giving the grant permissions to invoking your lambda functions.
It is important to configure function with the resource based policy.
An execution role defines a functions permission to interact with resources, and another best practice
is using environment variables.
We can use environment variable in order to store secrets securely and adjust your functions behavior
without updating your code.
So an environment variable is a pair of string that are stored in a functions virgin specific configuration.
We can configure the environment value on the Islam, the configuration section, the underlying invocation
environment for the runtime provides additional libraries as well as the environment variables that
you can access from your function code.
So we can also create your own environment variables in order to use in your code.
When you publish a version, the environment variables are locked for that version along with the other
virgin specific configuration.
So this is a very powerful feature, as you can see that we have also seen best practices of Islam,
the function code.
In the next video we are going to work through a lambda with a management console.