Serverless computing is a cloud computing paradigm that abstracts away server management, allowing developers to focus solely on writing and deploying code. Here’s a breakdown of the core concepts:
- No Server Management: In traditional deployments, developers manage servers, including provisioning, scaling, and patching. Serverless computing eliminates this burden. The cloud provider handles infrastructure management, freeing developers to concentrate on application logic.
- Pay-Per-Use Model: Serverless platforms charge based on resource consumption, typically execution time and memory used. This translates to cost-effectiveness, especially for applications with variable workloads. You only pay for the resources your code utilizes.
- Event-Driven Architecture: Serverless functions are often triggered by events. These events can originate from various sources like file uploads to cloud storage, changes in databases, or HTTP requests. Functions execute in response to these events, enabling a highly responsive and scalable architecture.
- Scheduled Execution: Functions can also be triggered based on schedules. This is useful for tasks that need to run periodically, such as sending automated reports or data backups.
Popular Serverless Platforms:
- AWS Lambda: A serverless compute service offered by Amazon Web Services (AWS). It allows developers to upload code and define triggers for execution.
- Google Cloud Functions: A serverless offering by Google Cloud Platform (GCP). Similar to AWS Lambda, it provides a platform for deploying and running code triggered by events or schedules.
Benefits of Serverless Computing:
- Faster Development and Deployment: Eliminating server management streamlines development and deployment cycles.
- Scalability: Serverless functions automatically scale based on demand, handling traffic spikes efficiently.
- Cost-Effectiveness: The pay-per-use model ensures you only pay for the resources your code consumes.
- Focus on Code: Developers can focus on writing code without being bogged down by infrastructure management.
Function Creation — Lambda
Creation of functions with Lambda can take two forms, either direct access of the Lambda Console or through the Serverless framework. I chose to use the Serverless framework, as it made deployment (later) much easier.
Serverless encourages centralizing all configuration of your functions into a single YML file. The YML file requires the function name as it will be displayed on the Lambda console, the name of the function in your code base, and some configuration on when to execute the function. In our case, we wanted to execute the function on an incoming HTTP request with a method of POST.
Implementation :
Go to AWS Lambda function
Create a Lambda function
To create a Lambda function, start by signing in to your AWS Management Console. Navigate to the “Services” tab and select Lambda under the “Compute” category. Once on the Lambda page, click on Create Function and choose the option Author from scratch. You will then need to provide essential details for your function, including a unique Function name (e.g., EventProcessor) and selecting your preferred Runtime, such as Python 3.x or Node.js 14.x. These initial steps set the foundation for building and deploying your serverless function effectively.
Lambda function created successfully
After completing the setup, your Lambda function is successfully created and ready for use. AWS provisions the necessary resources in the background, making your function available to handle triggers and execute code as configured. You can now test the function, integrate it with event sources like API Gateway or S3, and monitor its performance using the built-in tools provided by AWS Lambda.
Type the python code
A simple AWS Lambda function written in Python can be designed to process events and return a response. The lambda_handler
serves as the entry point, logging the received event, extracting relevant data and constructing a JSON response. For example, the function can greet a user by Hello from lambda
when provided in the event. Once deployed, this Lambda function can be tested with custom events in the AWS Lambda Console, where it processes the input and returns a dynamic response.
Add trigger via API Gateway
To trigger an AWS Lambda function using API Gateway in parallel (executing multiple Lambda functions simultaneously), follow these steps:
-
Set Up Your Lambda Functions
Ensure that each Lambda function you want to invoke is already created and deployed in AWS. These functions will be triggered by API Gateway. -
Create an API in API Gateway
Go to the API Gateway service in the AWS Management Console.
Create a new HTTP API or REST API.
Great! Now that the trigger is created, you can test and optimize it as needed.
Configure API Gateway
- Go to API Gateway in the AWS Management Console.
- Choose the Lambda function you just wrote.
- Deploy the API to a stage (e.g., dev).
Run the Code
Click on the API Gateway to get the function running. Thus, Event-Driven AWS Lambda in Action
Conclusion
By understanding serverless computing and implementing a basic function with event-driven or scheduled execution, you’ve gained valuable practical experience in this rapidly evolving cloud development paradigm. This knowledge can be applied to build more complex serverless applications that are scalable, cost-effective, and agile.
Source link
lol