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 ?
- The Serverless platform is comprised of a number of services, from compute to messaging to S3 lambdas and more.
- AWS LAMBDA is a compute Service that allows us to write our own code, which will be run in response to the events that happen in other resources or other components. These events triggers our lambda functions, the code that we write, will be run.
- Our lambda function can interact with other components in the ecosystem if required.
Question:- What are AWS Lambdas Handler Functions ?
- Just like how we have a main method in standalone Java components, the lambda functions we create will have a handler function.
- This handler function can take different inputs and written back different types of responses
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) ?
- Basically, we have our EC2 instances, in the cloud.
- On EC2 instances, we have a server process up and running. We then deploy, applications through either Docker-containers or direct-applications.
- We have classes and functions depending on the language runtime. When a request comes in from the End-user or from another application, the application process will trigger or invoke these functions.
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 ?
- That is where the function as a service comes in.
- Function as services (FAAS) will have a platform that will host various functions or operations in case of AWS. This is the LAMBDA service.
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
- DynamoDB, etc.
Question:- What happens to Lambda functions, once it successfully serves the request ?
- Once the function finishes its work, the function can live for a little bit more time and serve other immediately available events.
- If there are NO events, then the platform will shut down the function.
That is the main difference here between FAAS and the earlier traditional model BAAS.
Question:- Whats the benefit of going with FAAS over BAAS ?
- Through FAAS, we derive the benefit of usage based cost. We will be charged for how much our time our function took to execute.
- We will not be charged for how much time the environment took to come up or how much time the environment was up and all that.
Question:- When does our environment comes up ?
- 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.
- The platform will create the required environment for our function and it will invoke the function.
Question:- Can you demonstrate an example of Java based environment ?
- The LAMBDA service will create the host environment, the Java Virtual Machine, the Lambda Java runtime.
- Finally, it will run the lambda function which you deploy onto it.
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.
- If a particular resource is deleted through cloud formation, a lambda can react to that.
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.
- When the lambda function gets triggered, the LAMBDA service will first create a virtual-environment or a virtual machine on the cloud, which involves a Linux operating system on top of this virtual machine.
- It will add the language runtime. For e.g., If it is a Java based lambda, it will add a Java virtual machine, which can be used to create a LAMBDA Java runtime. So it will launch a virtual machine instantly using the JVM and then it will pull the Java class file from the repository, load it into memory, create an instance of that class file, and within that instance it will invoke the lambda function code that we create and ask it to invoke.
So all this is magically happening behind the scenes.
Question:- Can we invoke Lambdas through Command Line as well ?
- 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.
- It can be invoked from the AWS CLI from the command line or you can also invoke a lambda function from within your code.
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.
- Similarly, if you upload some file to a S3 bucket that will create an event or raise an event that will trigger a lambda function.
- If you put a message into a SNS topic, that SNS topic can trigger a lambda function as well.
Question:- What are various modes, to invoke Lambdas ?
Answer:-There are different modes in which we can invoke a lambda function :-
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.
- Cloud front can be used as a front end, where our FE code (Angular, React JS) can trigger lambda functions in the back end to get the work done against a database or execute some logic and return some data back.
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.
- Similarly, a message to SNS topic can trigger a lambda function asynchronously.
- And also Cloud Formation, when an event happens in cloud formation, like resource creation in the AWS cloud or the resource depletion, it can also can trigger a lambda function.
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.
- Packaging it into a zip file.
- Uploading the zip file into an S3 bucket or onto a S3 bucket.
- Create the IAM roles that are required to trigger our lambda function.
- Create all the other resources or components that our lambda function interacts with or the resources that trigger the lambda function itself.
- Creation of lambda function itself using the Lambda service.
- Configuration of the permissions that are required for our lambda function to interact with other components in the serverless application itself.
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?
- Yes, there are several other frameworks that are available in the market, but SAM is from AWS and very easy to use.
- SAM, is an extension of cloud formation or it is a superset of cloud formation. You can directly use anything from cloud formation in your SAM template as well.
- With SAM, you are eventually going to create the CI/CD, the continuous integration and continuous deployment for your serverless project.
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.