Serverless Architecture and the Reasons AWS Lambda is Considered Serverless.
Serverless architecture refers to a cloud computing model where the cloud provider manages the infrastructure and automatically handles scaling, patching, and administration of the server. This allows developers to focus solely on writing and deploying code without worrying about server management.
Serverless architecture is an approach to software design that allows developers to build and run services without having to manage the underlying infrastructure. Developers can write and deploy code, while a cloud provider provisions servers to run their applications, databases, and storage systems at any scale.
While serverless architecture has been around for more than a decade, Amazon introduced the first mainstream FaaS platform, AWS Lambda, in 2014. Currently, a majority of developers still use AWS Lambda to build serverless applications, but Google and Microsoft have their own FaaS offerings as well, called Google Cloud Functions (GCF) and Azure Functions respectively.
Fundamental Concepts in Serverless Architecture
Although serverless architecture eliminates the need for server management, there’s still a steep learning curve, especially if you’re chaining multiple functions together to create complex workflows in an application. It can therefore be helpful to familiarize yourself with these fundamental serverless terms:
Invocation
A single function execution.
Duration
The time it takes for a serverless function to execute.
Cold Start
The latency that occurs when a function is triggered for the first time or after a period of inactivity.
Warm Start:
If the function has been recently invoked, cloud provider may reuse the existing execution environment, reducing the initialization time and resulting in faster execution.
Concurrency Limit
The number of function instances that can run simultaneously in one region, as determined by the cloud provider. A function will be throttled if it exceeds this limit.
Timeout
The amount of time that a cloud provider allows a function to run before terminating it. Most providers set a default timeout and a maximum timeout.
Keep in mind that each cloud provider may use different terminology and set unique limits on serverless functions, but the list above defines the basic concepts.
Serverless Architecture vs. Container Architecture
Both serverless and container architectures allow developers to deploy application code by abstracting away the host environment, but there are key differences between them. For example, developers who are using container architecture have to update and maintain each container they deploy, as well as its system settings and dependencies. In contrast, server maintenance in serverless architectures is handled entirely by the cloud provider. Additionally, serverless apps scale automatically, while scaling container architectures requires the use of an orchestration platform like Kubernetes.
Containers give developers control over the underlying operating system and runtime environment, making them suitable for applications that consistently get high traffic or as a first step in a cloud migration. Serverless functions, on the other hand, are better suited for trigger-based events such as payment processing.
AWS Lambda is often called "serverless" because it abstracts away the server infrastructure from the developer. Here's why:
While the term "serverless" might be misleading, as servers are indeed used to run the code, the "serverless" model focuses on removing the need for developers to manage and worry about the underlying server infrastructure.
Example: Hello World AWS Lambda Function in Node.js
Create a new Lambda function: In the AWS Management Console, navigate to Lambda and create a new function. Choose "Author from scratch" and give your function a name, such as HelloWorldFunction. For the runtime, select Node.js.
Write the Lambda function code:
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello, World!'),
};
return response;
};
Deploy and Test the Function:
Deploy the function and create a test event. When you run the test, the function should return a 200 status code with the body Hello, World!.
How It Is Serverless:
Example with Event Trigger
To illustrate how AWS Lambda fits into the serverless model, let's add an API Gateway trigger to our Lambda function.
Create an API Gateway:
In the AWS Management Console, navigate to API Gateway and create a new REST API. Create a new resource and a GET method.
Integrate with Lambda:
In the GET method settings, set the Integration type to "Lambda Function" and select your HelloWorldFunction.
Deploy the API:
Deploy the API to a stage, such as "prod". You will get an endpoint URL.
Test the API:
Make a GET request to the endpoint URL. This request will trigger your Lambda function, which will return Hello, World!.
AWS Lambda functions are triggered by events from various sources such as HTTP requests via Amazon API Gateway, file uploads to Amazon S3, changes in a DynamoDB table, SNS notifications, CloudWatch events, and many other AWS services.
CloudWatch Logs: Lambda automatically logs all function invocations to Amazon CloudWatch Logs. You can view, search, and monitor these logs to troubleshoot issues.
CloudWatch Metrics: Lambda provides various metrics such as the number of invocations, execution duration, and errors, which you can monitor in CloudWatch.
Recommended by LinkedIn
The function code is stored and managed within the Lambda service. When you create or update a function, Lambda stores the deployment package in a highly available and durable storage system.
How does AWS lambda work behind the scenes?
AWS Lambda works behind the scenes to provide a seamless serverless computing experience.
1. Event Trigger:
- When an event occurs, such as an HTTP request to API Gateway, a new file in S3, or a scheduled CloudWatch event, the event source sends the event data to AWS Lambda.
2. API Gateway:
- For HTTP requests, API Gateway receives the request and forwards it to the appropriate Lambda function.
3. Event Source Mapping:
- Event source mapping is the process of associating events from event sources to the Lambda function. This mapping ensures that the correct function is invoked with the appropriate event data.
4. Function Invocation:
- Cold Start:
- When a function is invoked for the first time or after a period of inactivity, AWS Lambda initializes a new execution environment. This process involves provisioning resources, downloading the function code from storage, setting up the runtime, and initializing any dependencies.
- This initialization process can introduce latency, known as a cold start.
- Warm Start:
- If the function has been recently invoked, AWS Lambda may reuse the existing execution environment, reducing the initialization time and resulting in faster execution.
5. Execution Environment:
- The execution environment is an isolated environment where the function code runs. It includes the following components:
- Runtime: The runtime provides the language-specific execution environment (e.g., Node.js, Python, Java).
- Handler: The handler is the entry point to the function code. It processes the incoming event and returns a response.
- Temporary Storage: The execution environment provides a small amount of temporary storage (up to 512 MB) that can be used for the duration of the function execution. - - - Environment Variables: These can be used to pass configuration settings and secrets to the function code.
6. Resource Management:
- AWS Lambda automatically manages the underlying infrastructure, including provisioning servers, scaling resources, patching, and monitoring. The developer does not need to manage or provision any infrastructure.
7. Scaling:
- AWS Lambda automatically scales horizontally by creating new instances of the execution environment to handle multiple simultaneous requests. Each instance runs a copy of the function code.
- Concurrency limits can be configured to control the number of concurrent instances. 8. Execution and Response:
- The Lambda function executes the handler code, processes the event, and returns a response.
- For synchronous invocations (e.g., API Gateway), the response is returned to the client. - For asynchronous invocations (e.g., S3, SNS), the response is typically logged, and any follow-up actions are handled based on the function logic.
9. Logging and Monitoring:
- AWS Lambda automatically captures logs generated by the function and sends them to Amazon CloudWatch Logs.
- Metrics such as invocation count, duration, and error rates are available in Amazon CloudWatch Metrics.
10. Security:
- Each Lambda function runs with an AWS Identity and Access Management (IAM) role that grants it the necessary permissions to interact with other AWS services and resources.
- Resource-based policies can be used to control access to the Lambda function.
Example Flow of an API Gateway to Lambda Invocation:
1. API Request:
- A client sends an HTTP request to an API Gateway endpoint.
2. API Gateway:
- API Gateway receives the request and forwards it to the associated Lambda function. 3. Lambda Service:
- AWS Lambda retrieves the function code from its internal storage.
- If necessary, it initializes a new execution environment (cold start) or reuses an existing one (warm start).
4. Execution:
- The Lambda function's handler method processes the event, performs the required actions (e.g., querying a database, processing data), and returns a response.
5. Response:
- The response is sent back to API Gateway.
- API Gateway formats the response and sends it back to the client.
6. Logging:
- Logs generated during the function execution are sent to CloudWatch Logs for monitoring and troubleshooting.
AWS Lambda abstracts away the complexities of server management, allowing developers to focus on writing and deploying code. By handling provisioning, scaling, and managing the execution environment, AWS Lambda enables a highly scalable and cost-effective serverless architecture.
AWS Lambda provides a mix of ephemeral storage (Temporary Storage) (/tmp) for temporary data during function execution and allows integration with various AWS storage services like Amazon S3 (Persistent Storage), Amazon EFS (Persistent Storage), and databases for persistent data storage, making it highly flexible for different storage needs.