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.

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.

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.

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.

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 :-

sam build

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 :-

sam deploy

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.

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.

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.

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.

Question :- Can you conclude on : what we have learnt so far ?

Answer:-

  • Whenever a lambda is triggered for the very first time, the entire context is created.

Question :- Can you demonstrate : how can we invoke the Lambda function remotely

Answer:-

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 ?

Answer:-

  • 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.

Question :- What’s the concept of DynamoDB ?

Answer:-

  • It is a serverless, unstructured database component using which we can create tables, indexes and store data for our otherwise stateless LAMBDA functions.

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.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store