AWS Lambda Hands-On| Part-3

  • 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”.
  • 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.
  • 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.
  • 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.
(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:/
sam build
sam deploy
  • 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.
  • 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.
  • Fixed same value for static variable.
  • Fixed same value for instance variables.
  • 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.
  • 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.
aws lambda invoke --invocation-type Event --function-name adityaLambdaFirstStack-HelloWorldFunction-fH9DLwMkeCcQ outputfile.txt
  • 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.
  • 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.

--

--

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
aditya goel

aditya goel

Software Engineer for Big Data distributed systems