Working with AWS Lambda | Part-1

  • 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.
  • 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
  • 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.
  • 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.
  • S3
  • SNS
  • DynamoDB, etc.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • Synchronous.
  • Asynchronous.
  • Polling.
  • 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.
  • 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.
  • 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.
  • 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.



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