AWS Lambda Hands-On| Part-3
In case you are landing here directly, it’s recommended to read through this documentation first.
In this particular blog, we would demonstrate following aspects :-
- Demonstrate the fundamentals of Cold -Start with AWS Lambdas.
- Invoking/Triggering Lambda function remotely.
- Demonstrate the Lambdas with dynamic data handling using InputStream & OutputStream.
- Demonstrate the Lambdas with Context Object.
- Introduction to API-Gateway.
- Introduction to DynamoDB.
- Creation of DynamoDB table using CloudFormation “template.yaml”.
Question:- What’s the meaning of ColdStart ?
Answer:- Here are the steps involved whenever Lambda is started :-
- When a lambda is triggered for the very first time, the lambda service will set up the entire environment that is required for that lambda to work.
- It will load the lambda class into memory, create an instance of that class and invoke the lambda function we have written.
This entire process is called cold start.
Question:- Does ColdStart process happens at deployment time ?
Answer:- This process of ColdStart happens when the lambda is triggered and NOT when it is deployed.
Question:- Does ColdStart process happens at every invocation of Lambda ?
Answer:- NO, the ColdStart doesn’t happens at every invocation of the Lambda because Lambda service can freeze the lambda instance so that it can be reused for a few other events that might occur immediately. So, when a lambda function is triggered for the very first time, the lambda service does the cold start and it will freeze this entire environment i.e. freeze this instance to serve more requests that might come in immediately or more events that might trigger the same lambda immediately.
Question:- When does ColdStart process can happen ?
Answer:- The cold start happens during following situations :-
- When the lambda is triggered for the very first time i.e. the first request comes in.
- When we have redeployed those changes and then a new request comes in, the LAMBDA service will not use the old instance, even if they are up, it will take them down and shall reload the changes. It will create a new environment.
- Whenever Lambda instance has been expired → Usually Lambda Service has an instance to which, it freezes. That instance is usually used across multiple events, but when there are no events coming in after some time., the LAMBDA service will take that instance down. Now, please note specially here that, We don’t know what the time is. It is not documented anywhere. Typically, I see that that time is within four to six hours.
- Cold Start process might also happen due to age. Although there are events coming in on a continuous basis, after some time, the LAMBDA service will take the LAMBDA instance down and start fresh one. Now, please note specially here that, we don’t know what that age is. It is not documented anywhere as well. It is totally up to the AWS.
- ColdStart process also happens during Scaling. If there are too many requests i.e. too many events happening and too many lambdas are being triggered. The same lambda is being triggered multiple times on a continuous basis. Then, if the same instance cannot handle the incoming requests, the LAMBDA service will create more instances.
Question:- How do we know from Cloudwatch Logs timings, whether ColdStart process has happened ?
Answer:- To know whether ColdStart process has indeed happened → We can look at the time that Lambda has taken. Clearly the cloud watch logs show us how much time our function has taken to execute and how much time the environment.
Question:- Can we know from Cloudwatch Logs Streams, whether ColdStart process has happened ?
Second way → There is a log group, cloud watch log group, but for each Lambda Instance there is a log stream within this log group.
- So, If the same instance is reused, then those logs will end up in the same log stream.
- If two different events were served by two different lambda instances that are created by the lambda service, then those logs will have their own log streams.
Question:- What are the advantages of ColdStart process ?
Answer:- The advantage of Coldstart is that : The same Lambda Instance can be reused across lambda invocations and it will save a lot of time for the application, although, we are not charged for the time it takes to bring the environment up.
Question:- Can you demonstrate the cold start fundamentals with AWS Lambda function ?
Step #1.) Let’s now first write some code :-
Step #2.) Let’s next write our template.yaml file :-
Step #3.) This code we developed in our local host. Let’s now take our codebase to the docker, which contains all necessary aws toolkit :- (Note here, that, please refer to previous blogs, if you want to setup the docker container fresh).
(base) B0218162@APB-LTB0218162-MAC PROJECTS % pwd/Users/B0218162/Documents/LEARNINGS/MEDIUM-BLOG/AWS/AWS-Lambda/PROJECTS(base) B0218162@APB-LTB0218162-MAC PROJECTS % lsadityaLearningLambda(base) B0218162@APB-LTB0218162-MAC PROJECTS % docker cp adityaLearningLambda 028cce033ce7:/
Step #4.) Inside the docker container, let’s now build our project :-
Let’s now observe whether all of the compiled classes & dependencies got created now :-
Step #5.) Let’s now go ahead and deploy the project to AWS :-
Step #6.) Let’s now go ahead and create a sample test event at AWS Lambda console :-
Step #7.) Let’s now go test our Lambda function, multiple times :-
Step #8.) Let’s observe how many log-streams do we have in Cloud-Watch ?
Observe that, there is only ONE log-stream and this stream have the logs from the four Lambda invocations(that we just did) within the same stream because the same instance of the lambda was used.
Step #9.) Let’s observe the actual logs in this cloudwatch stream :-
- The start and the end mark a particular lambda invocation start and end.
- The request id as you can see, is unique for each lambda invocation.
Question :- Does the above logs indicate that, only ONE Lambda instance is being created ?
Answer:- Key here is that the same lambda is being used : The static block executed is only ONCE.
- So, for the very first time, the lambda is loaded and the host environment is created.
- Then we have the JVM being created and then the Lambda Java runtime.
- Finally the lambda itself is instantiated, the lambda class is instantiated only once and the class is loaded into memory. That is when, the static block is executed, then it will use the constructor to instantiate the lambda class.
Question :- Can you explain the fixed static value printed in the logs ?
Answer:- Next, now observe the function invocation itself. This function here is invoked and the output can be seen to have :-
- Fixed same value for static variable.
- Fixed same value for instance variables.
So, it is using the same lambda class instance. It will not create a new instance unless required. It will try to use the same instance and the same environment when available.
Question :- For how much time does, this environment remains Up ?
Answer:- There is no guarantee how long this environment will be up. That time keeps changing. It is not documented anywhere. At least I couldn’t find it.
- So, that process is called Cold start for the very first time. It will take some time for this entire environment to be set up.
- But if we are sure that within our application we are going to hit that lambda several times at a given point in time, we can have that lambda remain up. And for the consecutive calls, it shall re-use existing Lambda function.
Question :- Can you conclude on : what we have learnt so far ?
- Whenever a lambda is triggered for the very first time, the entire context is created.
- That is when the lambda function class is loaded into memory. Then, an instance is created.
- The static blocks, if they exist, which is very rare, get executed.
- Construct used to create the instance at which point these fields here are initialized.
- The lambda function itself will be invoked. It will have its own stack for every time the lambda is triggered.
Question :- Can you demonstrate : how can we invoke the Lambda function remotely
aws lambda invoke --invocation-type Event --function-name adityaLambdaFirstStack-HelloWorldFunction-fH9DLwMkeCcQ outputfile.txt
Question :- Can you demonstrate the Lambdas with dynamic data handling using InputStream & OutputStream ?
Step #1.) Here is the code :-
Step #2.) Here is the template.yaml file :-
Step #3.) Copy our codebase to the Docker container :-
Step #4.) Build our codebase :-
Step #5.) Deploy our Lambda to AWS :-
Step #6.) Test our Lambda to AWS with below sample event :-
Step #7.) Here is how the output/logs of our Lambda function looks like :-
Question :- Can you demonstrate the Lambdas with Context Object ?
Step #1.) First, have the code where AWS Context object shall be used :-
Step #2.) Next, let’s have our template.yaml file prepared:-
Step #3.) Now, we would build & deploy this function to AWS and test it out :-
Question :- What’s the concept of API Gateway ?
Answer:- API-Gateway is a serverless component that can be used to create APIs to our back end functionality. It will act as a layer of abstraction between the client applications and our back end .
Question :- What’s the advantage of API Gateway ?
- We can implement various non-functional requirements like security at this API-Gateway Layer and our applications and code need not worry about it. For example, API Gateway can be easily used with AWS Cognito, which is a security component wherein we will get the authentication, authorization and certificates right out of the box.
- Similarly, we can use the AWS Cloud-Front in front of the API-Gateway to distribute the traffic to various edge locations within the AWS cloud and also add additional security to prevent denial of attacks.
- It can scale automatically. It can handle from one to ten thousand requests per second and we don’t have to worry about it. AWS will take care of it. And if we want more requests to be handled, we can create a support ticket and AWS will make that possible.
Question :- What’s the concept of DynamoDB ?
- It is a serverless, unstructured database component using which we can create tables, indexes and store data for our otherwise stateless LAMBDA functions.
- It has scaling and replication across availability zones built in like most of the AWS serverless components.
- DynamoDB will have tables, within which we shall have items.
- Items are like rows in typical RDBMS databases and the columns are referred to as attributes.
- In addition to that, you should also know that every table has a mandatory partition key. This is like the primary key for our database table.
- Optionally, it can also have a soft key which will help us, when we query the data.
Question :- Can DynamoDB trigger to Lambda Function as well ?
Answer:- DynamoDB can also act as event source that triggers the lambda function. Basically, whenever something happens on a DynamoDB table, that can trigger a lambda function and the data from that table can be used by the lambda function to process and do whatever business logic it had to do.
Question:- What’s the importance of Template.yaml file ?
Answer:- Whenever we do “sam deploy”, all the infrastructure that is required will be created by referring to template.yaml file.
Question :- Let’s now go ahead and create a DynamoDB Table using CFN template ?
We define a DynamoDB table resource. Under properties section, we have also defined the Primarykey of the database table “id” of type number. Cloud formation will create a database for us. It will create a table inside that database called OrdersTable.
That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.