November 21, 2022

How AWS Lambda Works – Functions and Architecture Explained

Table of Contents

If you are a returning reader here at the Moralis blog, you might have read one of our previous articles exploring AWS Lambda and what it is used for. That guide dives into the intricacies of AWS Lambda to give you a comprehensive overview of the service. However, it does not cover the very specifics of how AWS Lambda works. Thus, we decided to publish this article, which explains more in-depth how AWS Lambda works, including its functions and architecture. Sounds interesting? Follow along as we break it all down for you! 

This article begins by explaining the ins and outs of AWS Lambda and provides an overview of how the service works. Following that, we will dive a bit deeper into the details by explaining the role of AWS Lambda functions and how they work. Once you have familiarized yourself with this component, the article will cover the AWS Lambda architecture. Lastly, to top things off, we will briefly outline an AWS Lambda NodeJS tutorial where you can learn how to run JavaScript functions via Lambda using Moralis. 

Once you become aware of how AWS Lambda works, you will be able to combine this service with other handy development tools, such as Moralis’ Web3 APIs. If this sounds interesting, make sure to check out the Moralis Streams API. With this tool, you can seamlessly stream on-chain data into the backend of any application through webhooks. If you want to know more about creating streams, you will find our articles on Polygon webhooks and Ethereum webhooks helpful! 

Nevertheless, if you want access to the tools mentioned above, sign up with Moralis right now. In doing so, you can fully leverage the power of blockchain technology and start building industry-leading dapps! 

AWS Lambda – What is it and How Does it Work?

Amazon Web Services (AWS) is one of the most well-established cloud platforms, featuring over 200 web-based services. Within this comprehensive AWS ecosystem, you can find AWS Lambda. But what exactly is AWS Lambda, and how does it work? If you are looking for the answer to this question, read on as we explore the intricacies of this prominent AWS service! 

Editor code and Lambda logo.

Lambda is an event-driven, serverless computing service part of the extensive AWS ecosystem. With Lambda, you can run code seamlessly without needing to provision or manage servers. Instead, you rely on the service’s computing infrastructure to administer all your computing resources. This includes security and code patch development, code monitoring and logging, automatic scaling and capacity provisioning, server and operating system maintenance, etc. Consequently, your sole responsibility is to supply the code. 

AWS Lambda runs the supplied code in response to multiple events. This can, for example, be HTTP requests through Amazon API Gateway, table updates in DynamoDB, object modifications in Amazon S3, or other third-party events. As such, this highlights one of Lambda’s main features: easy integration with other tools and services for monitoring, security, governance, etc. 

Working with AWS Lambda provides several significant benefits, such as cost reductions, automatic scaling, tight AWS integrations, access to a fully managed infrastructure, etc. Accordingly, you can avoid redundant computing-related tasks through Lambda to focus more on writing excellent code for your applications and websites! 

Moreover, the code you supply to the service is organized into Lambda functions. However, what are AWS Lambda functions, and how do they work? We set out to answer this question in the following section. As such, if this sounds exciting and you want to explore the intricacies of Lambda functions, read on! 

How do AWS Lambda Functions Work?

Lambda functions are the backbone of the service’s functionality and a vital part of its architecture. As such, this initial section answers the following question, ”how do AWS Lambda functions work?”.

The code you upload to AWS Lambda is consistently formatted as ”Lambda functions”. These functions have configuration information attached, including a name, resource requirements, an entry point, and a description. Moreover, all Lambda functions are ”stateless”. Consequently, the functions make no underlying compute infrastructure assumptions.

Lambda functions are not continuously running, only executing when required, and can scale accordingly. As such, Lambda functions can handle everything from a few requests per day to thousands each second. Furthermore, you only pay for the computing resources the functions consume. As a result, there are no costs when you do not run the code.

Lambda functions can be invoked in two ways: either through APIs or in response to events from additional AWS services. So, how exactly does this happen? To give you a better understanding of how Lambda functions work, let’s briefly look at three scenarios when they are requested: 

  • File Processing – You can use Amazon S3, a storage service, to trigger Lambda functions for data processing in real-time after a new upload. In this scenario, the process can look something like this:
Graph explaining how AWS Lambda functions work.
  • Web Applications – It is further possible to utilize other AWS services with Lambda to build web applications that can scale up and down: 
Graph showing how web applications work with AWS Lambda.
  • Mobile Backends – Use AWS Lambda to build backends by combining this service with Amazon API Gateway to process and authenticate API requests: 
Use Lambda to build mobile backends.

Nevertheless, that covers some prominent examples of how the functions and AWS Lambda work! 

What is AWS Lambda’s Architecture

AWS Lambda features a serverless, event-driven architecture where events are used to trigger and communicate between decoupled systems. Events can take many forms, and a few examples can be changes in state, updates, or items placed in an e-commerce website’s shopping cart. What’s more, event-driven architectures have grown common in modern apps developed with microservices.

AWS Lambda architecture graph.

Event-driven architectures have three central components closely intertwined with one another: event producers, event routers, and event consumers. Producers generate events and publish them to routers. Furthermore, routers filter, process, and send the events to the consumer. From there, consumers receive events for further processing. 

The event producers and consumers are separate systems, and the event-driven architecture facilitates the communication between services. Moreover, since both systems are decoupled, they can scale, update, and deploy interdependently.

In the AWS Lambda architecture, the starting point, which is the event producer, is also generally referred to as an event source. The source is responsible for publishing events to Lambda functions, which you learned in the previous section can occur via APIs or other AWS services. 

Lambda functions accept the events passed by the event source and then process them according to the functions’ logic. From there, the functions eventually publish events to other systems or services. As such, within the AWS Lambda architecture, functions act as event routers. 

Lastly, there is always an event consumer involved in the process, which is another significant component of the AWS Lambda architecture. Moreover, Lambda functions are insufficient in isolation, and other systems are required to implement business logic. This can, for example, be database object storage, other third-party services, etc., which are consumers of the events published by the functions. 

An Example of How AWS Lambda Works

Until now, you have familiarized yourself with the intricacies of AWS Lambda and how the service works in theory. You have also explored how AWS Lambda functions work and learned more about the service’s architecture. This section teaches you how to apply your newly acquired knowledge in practice. In doing so, we will show you how to easily integrate Moralis’ NodeJS SDK with AWS Lambda. If you follow along, you will learn everything about building, testing, deploying, and running Web3 JavaScript functions through Lambda! 

How AWS Lambda works with Moralis.

To illustrate the accessibility of Moralis, we will show you how to build and deploy a Lambda application featuring two functions: ”getNativeBalance()” and ”getWalletNfts()”. What’s more, to make this tutorial as straightforward as possible, it has been broken down into the following three steps: 

  1. Prerequisites
  2. Cloning the App
  3. Test and Deploy

Nevertheless, let us kick things off immediately by jumping straight into the first step and covering the necessary prerequisites! 

Step 1: Prerequisites

Before taking a closer look at the application, you must complete a few prerequisites. You will find all the requirements in the list below: 

When setting up your IAM user, make sure to select both AWS credential types: 

Creating an IAM user page.

Be sure to check the ”AdministratorAccess” box: 

Showing input fields and checkboxes when setting permissions.

Lastly, you need to download your credentials in a ”.csv” file: 

Downloading .csv file.

Nonetheless, that covers the prerequisites and the initial step! Let us progress and look closer at cloning the code for the application in the next part! 

Step 2: Cloning the App 

For this step, you need to clone the project to your local directory, which you can do by visiting the GitHub repository down below: 

Complete AWS Lambda Repository – https://github.com/MoralisWeb3/examples-aws-lambda-nodejs/tree/main/aws-node-project 

The repository contains the complete code for the Lambda application, including the ”getNativeBalance()” and ”getWalletNfts()” functions. As such, feel free to explore the repository on your own to get a better idea of how it works. 

Nevertheless, once you have cloned the project and have a copy in your directory, you must install the Moralis dependencies. To do so, place yourself in the project’s root folder and run the command below in a new terminal: 

​​npm install moralis

That covers the second step! All that remains from here is testing and deploying the functions! 

Step 3: Test and Deploy

To test the application, create a new ”event.json” file in the root folder of the project: 

Showing how AWS Lambda works in the even.json file.

From there, add the wallet address you want to receive information from and the chain ID as parameters. It will look something like this: 

{
    "address": "0x99EXAMPLEADDRESS",
    "chain": "chain_id"
}

You should now be able to test the functions of the Lambda app by running this command in the terminal: 

serverless invoke -f FUNCTION_NAME --path event.json

However, note that you need to replace ”FUNCTION_NAME” with the actual name of the function you want to test. If the tests are successful, you should receive a message similar to the one below: 

Message displayed inside your IDE.

If you are happy with the results, you can now deploy the functions to AWS. This is relatively straightforward, and you only need to run the following command in the terminal: 

serverless deploy

Once you run this command, your terminal should display the following message if it has successfully deployed: 

Success message after deploying the function.

That’s it for this tutorial! Congratulations, you now know how to build, test, deploy, and run JavaScript functions via AWS Lambda! 

If you experienced trouble at any point during the tutorial, please check out Moralis’ AWS Lambda documentation for a more detailed breakdown of the entire process. Additionally, you can join the Moralis Discord channel to receive expert assistance firsthand! 

Summary – How Does AWS Lambda Work? 

You now know that AWS Lambda is a serverless, event-driven computing service. With Lambda, you can run code without provisioning and managing servers. Consequently, all you need to do to run programs is supply the code itself. However, how exactly does AWS Lambda work? 

AWS Lambda logo.

AWS Lambda features an event-driven architecture with three core components: event providers, event routers, and event consumers. The providers are responsible for publishing events to the routers, which, in turn, process the events and send them on to the consumers. In the case of Lambda, functions play the role of the routers. Consequently, Lambda functions play an essential role in the service’s architecture. That said, what are AWS Lambda functions, and how do they work?  

The code developers upload to Lambda is organized into ”Lambda functions”. All Lambda functions have configuration information such as a name, an entry point, a description, and resource requirements. Also, these functions are written in a ”stateless” style. This means that they make no assumptions about underlying computing infrastructures. Furthermore, in combination with learning more about how AWS Lambda works, the article provided an AWS Lambda NodeJS tutorial. As such, if you read that part, you now know how to run Web3-compatible JavaScript functions through Lambda! 

Nevertheless, if you found this article informative, consider checking out additional Moralis content. Moralis’ Web3 blog features fresh and exciting Web3 development content for experienced and new developers. For instance, read more about Web3 for enterprise or how to get the metadata of an NFT

What’s more, unlock the power of blockchain technology by creating your Moralis account today! You can sign up with Moralis for free and receive immediate access to enterprise-grade development tools! 

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
December 5, 2022

Web3 Storage – How Web3 Data Storage Works

December 13, 2022

Ethereum Web3 API – Exploring How to Use a Web3 Ethereum JavaScript API

January 5, 2024

How to List All the Coins in An ETH Address – Full Guide 

January 2, 2023

Get Contract Logs – Exploring Web3 Get Event Logs for Ethereum

August 2, 2022

How to Create an NFT Candy Machine

February 13, 2023

How to Build a Decentralized Cryptocurrency Exchange

August 10, 2022

How to Pull Data from Polygon Blockchain

November 15, 2023

How to Build a Real-Time Wallet Tracker

March 22, 2023

How to Get a Wallet Balance on Aptos