Last update:2024-09-03 15:10:10
This guide will walk you through creating your serverless function on the CDNetworks Edge Cloud Apps Function Compute platform.
In a serverless architecture, a function is a self-contained block of code designed to perform a specific task or logic. It’s triggered by events such as HTTP requests, database operations, message queues, or file uploads. Serverless platforms automatically scale these functions to handle varying traffic demands. Each function execution is independent and stateless, meaning it doesn’t retain any state from previous invocations. For persistent data storage, external services like databases or object storage should be used.
Functions are the core building blocks of serverless architecture. Developers write function code and deploy it to a serverless platform, which handles the execution, scaling, and resource allocation of the function. This approach is often referred to as “serverless functions” or “Function-as-a-Service” (FaaS). Unlike traditional server architectures, serverless architecture allows developers to focus on writing business logic without managing the underlying servers and infrastructure.
Edge Cloud Apps Function Compute, built on the V8 engine, provides a lightweight and high-performance serverless runtime environment with millisecond-level cold starts and resource isolation. We’ve optimized runtime context switching to significantly boost the execution efficiency of function code under high concurrency scenarios. Leveraging V8 technology’s ultra-low latency cold start capabilities, functions can be loaded rapidly upon request, switching between different functionalities quickly and without interfering with each other.
Before creating your serverless function, ensure you have a valid, configured CDN acceleration domain on the CDNetworks platform.
image-resizer
or user-auth
.Using a Template: The platform offers a range of pre-built function templates covering various common functionalities. These templates allow you to quickly build new function content without writing code from scratch. You can quickly create functions for features like Stream M3u8 Rewriting, Web Link Rewriting, and Aggregated Request & Responses. Each template comes with detailed instructions to help you understand its usage. After selecting a template to create a function, you’ll see the corresponding sample code in the CloudIDE interface. Our template library is constantly updated and expanded, with new templates in development.
Without a Template: If you need to create more customized functions or want to build your function from scratch, you can choose to create a function “Without a Template.” This will takes you to a blank CloudIDE interface where you can freely write your code.
Upon function creation, the page will automatically redirect to the CloudIDE interface. Within CloudIDE, you have the freedom to write, edit, and debug your code. CloudIDE offers features like syntax highlighting, auto-completion, and error prompts for a streamlined coding experience, similar to Visual Studio Code. For more information on CloudIDE, please refer to our CloudIDE Documentation.
Note
Each function can contain multiple JavaScript files for better organization and management of complex function logic. However, each function must include anindex.js
file as the entry point. When a function is triggered, the platform executes code starting from theindex.js
file.
After writing your code in CloudIDE, you can use the built-in testing tools to test your function:
/example
path, enter an address like http://dev.wsls.haplat.net:8123/cloudide/staging/test/xxxxxxxxxx/example
. Ensure that the resource corresponding to this URI actually exists and is valid under your test domain.
Once you have finished writing and testing your function, you can deploy it to the production environment. The Edge Cloud Apps platform supports grayscale deployment to ensure a secure and stable deployment process. For more detailed information on function deployment, please refer to the Function Deployment documentation.