Working with AWS Lambda | Part-1

In case you are landing here directly, it’s recommended to read through this documentation first.

Question:- What are AWS Lambdas and where they fit into A.W.S serverless platform ?

Answer:-

  • The Serverless platform is comprised of a number of services, from compute to messaging to S3 lambdas and more.

Question:- What are AWS Lambdas Handler Functions ?

Answer:-

  • Just like how we have a main method in standalone Java components, the lambda functions we create will have a handler function.

Question:- Does AWS Lambdas follow Nano-Services pattern ?

Answer:- These lambdas follows the guidelines of nano services. Every LAMBDA function, we create will be a nano service.

Question:- What’s the traditional way of powering our applications i.e. BAAS (Backend As A Service) ?

Answer:-

  • Basically, we have our EC2 instances, in the cloud.

Question:- What’s the disadvantage of the traditional flow, BAAS ?

Answer:- The server process and the application process will wait for the client request once they are up and running, even when there are no requests, they are up and running.

Question:- What’s the alternative to avoid the scenario, where application process are up & running, even when there is no need ? What is the meaning of FAAS ?

Answer:-

  • That is where the function as a service comes in.

Question:- Which all other services can trigger the AWS Lambda Functions ?

Answer:- Below services might trigger or fire an event. The LAMBDA service will receive that event, invoke the lambda function as they are event based

  • S3

Question:- What happens to Lambda functions, once it successfully serves the request ?

Answer:-

  • Once the function finishes its work, the function can live for a little bit more time and serve other immediately available events.

That is the main difference here between FAAS and the earlier traditional model BAAS.

Question:- Whats the benefit of going with FAAS over BAAS ?

Answer:-

  • Through FAAS, we derive the benefit of usage based cost. We will be charged for how much our time our function took to execute.

Question:- When does our environment comes up ?

Answer:-

  • Our environment (i.e. the function) will be up only when there is event happening, when the function is really required and that environment will be shut down completely by the LAMBDA service when we don’t need it.

Question:- Can you demonstrate an example of Java based environment ?

Answer:-

  • The LAMBDA service will create the host environment, the Java Virtual Machine, the Lambda Java runtime.

Question:- Who performs the Infrastructure provisioning & security ?

Answer:- We don’t have to worry about the infrastructure that is required. We don’t have to worry about non-functional requirements like scaling, security, reliability, etc.The LAMBDA service will take care of all that for us. Those are huge advantages of function as service.

Question:- What is Serverless Computing ?

Answer:- Serverless component is any component that we don’t have to worry about managing including the reliability, scalability, etc. since a lambda function can be triggered by events happening in these.

Question:- What are the various use-cases, where Lambdas can be used ?

Use-case #1.) Lambda to create Restful Backends :- We can deploy our applications either on EC2 instances, Elastic Bean Stalk or a static application on S3. And these applications can make calls to the API gateway component in AWS, which will trigger your lambda, which is responsible for communicating with the database like Dynamo DB and fetch the data or save the data to the Dynamo DB. So using lambdas you can create a restful backend for your web applications.

Use-case #2.) Complete web application using lambdas :- That is not a typical use case for functional programming and nano services. We still host our web applications itself separately and then lambdas can take care of restful APIs and act as a restful backend.

Use-case #3.) Data processing through Streams:- The event or the data itself can come from a kinesis stream or Dynamo DB stream or any other stream. Lambdas are perfect to convert your batch jobs if you have them in your projects to lambda functions. Thanks to LAMBDA service, it can scale and create as many in a number of lambda instances as required on the fly, depending on the data and the load.

Use-case #4.) Hosting logic for chat-bots or Alexa skills :- If you have ever use Alexa Speaker using voice commands, it is making backend calls to lambda functions that are hosted in the cloud by AWS and those LAMBDA functions are hosting the logic, which is sending the response back. They are the brain behind Alexa and any chatbot applications.

Use-case #5.) Managing infrastructure :- When you are using CFN from AWS to create an entire infrastructure, you can configure lambda functions in such a way that they will be triggered by CloudWatch logs. When something happens to your infrastructure, it can be known through the CloudWatch logs, alarms, etc. and your lambda function can react to that and make sure your infrastructure is intact.

  • If your server goes down, for example, a lambda function can bring another server up so that your clients can remain happy.

Question:- Let’s demonstrate AWS Lambda in action ?

Answer:- Lambda functions needs some trigger to work upon. We would demonstrate the Lambdas with simple print instructions :-

Step #1.) Specify the Lambda’s name along with runtime as Node JS :-

Step #2.) We would next go with auto-role thhat lambdas would create for us :-

Step #3.) That’s all, our lambda is all set and has been created now :-

Step #4.) Let’s go ahead and see code of our Lambda function :-

Step #5.) Let’s go ahead and create a new test event to verify our Lambda function :-

Step #6.) Here is the output / logs, auto generated by the Lambdas :-

Question:- What happens under the hoods, when Lambdas are executed ?

Answer:- Lambda service is responsible for creating the entire environment that is required for a lambda to execute. Depending on the language that is being used, it will create the appropriate language runtime, on top of which our program will be executed. Here is the workflow of Lambdas :-

  • Usually, Lambda code is stored into the AWS Lambda Repository in the AWS cloud.

So all this is magically happening behind the scenes.

Question:- Can we invoke Lambdas through Command Line as well ?

Answer:-

  • The lambdas can be invoked not only from the console. Most of the times the lambda function can be invoked from the console, which is just for testing.

Question:- From which other AWS services, can Lambda functions be triggered ?

Answer:-The lambda function can be triggered by other services within AWS like S3, Dynamo-DB, API Gateway, SNS, etc.

  • API Gateway can trigger a lambda function when a user hits a URL which will hit the API gateway. This API gateway can trigger a lambda function.

Question:- What are various modes, to invoke Lambdas ?

Answer:-There are different modes in which we can invoke a lambda function :-

  • Synchronous.

Question:- Pl explain the Synchronous way of invoking Lambdas ?

Answer:- In this SYNC mode, Lambda function is expected to return a response right away. Examples of synchronous event sources are API Gateway, Alexa Cloud front and many more. We usually create API using API Gateway, where the end users will be hitting the URLs of the API gateway. The API gateway will trigger the lambda function. It gets a response right away and it sends it back. As of writing this series, max limit of event size is 64 MB. Example :-

  • If you are using the Amazon’s Alexa speaker, then when you use the voice commands against the Alexa speaker in the back end it is making calls to lambda functions that are hosted, it gets the response from the lambda functions and that that response is what you hear back. So, if you are asking for whether or asking for searching something, Alexa Speaker is going against the lambda functions that are already there developed by AWS so that communication happens synchronously.

Question:- Pl explain the Asynchronous way of invoking Lambdas ?

Answer:- In this ASYNC mode, Lambda function won’t return back any response. The return type of a lambda function will be void in case of Java and event source will trigger the lambda function. It will forget about it. The lambda function will do its work and it might write to a database or do something else, which is totally up to the application and use case example, event sources for asynchronous communication or asynchronous triggering or invocation. As of writing this series, max limit of event size is 256 KB.

  • When data is uploaded to S3 bucket or deleted from S3 bucket, any event can trigger a lambda function asynchronously. And it is up to the lambda function, What to do if it wants to read data from the S3 bucket, it can do it or it can do something totally different totally upto the use case and business requirements.

Question:- Pl explain the Polling way of invoking Lambdas ?

Answer:- In this POLLING mode, actually, lambda functions are not triggered. Instead, the lambda service will do polling. It will poll other sources like Kinesis data streams. Dynamo DB streams or the SQS. We can configure the lambda service to keep polling these services, the kinesis data streams. For example → If there is a message on the SQS , then that Lambda service will trigger a particular lambda. This process is called polling.

Question:- What are the steps involved in deployment of Serverless Application ?

Answer:- Following are the steps involved in deployment process of serverless applications :-

  • Building the application i.e. Coding part.

Question:- Is there some way to automate the deployment of Lambda Service ?

Answer:- To automate this entire process of infrastructure creation, the security setup, etc, we can use the cloud formation service, which is infrastructure as a service. But we will have to write a lot of lines of code or configuration to accomplish it when we use cloud formation.

Question:- Is there some alternative to using Cloud-Formation (CFN) ?

Answer:- Yes, we can leverage SAM i.e. Serverless Application Model. AWS SAM is a Serverless Application Framework which takes care of all above steps. And the configuration file for AWS SAM is very simple when compared to the cloud formation template.

Question:- Are there other choices available with us for Serverless Application Framework?

Answer:-

  • Yes, there are several other frameworks that are available in the market, but SAM is from AWS and very easy to use.

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