Create ECA Function

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.

What is a Serverless Function?

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.

Start to Create Your Serverless Function

Before creating your serverless function, ensure you have a valid, configured CDN acceleration domain on the CDNetworks platform.

  1. Log in to the CDNetworks Console, select Edge Application product, and navigate to the Function List page under Edge Cloud Apps menu bar.
  2. Click Create Function to access the function creation page.
  3. Provide the following basic information for your function:
    • Function Name: A globally unique internal identifier for your function. Only lowercase English letters, numbers, underscores (_), and hyphens (-) are allowed, with a length between 3-32 characters. For example: image-resizer or user-auth.
    • Function Alias: A user-friendly alias for easy identification in the console. This can be in English or Chinese, include numbers, underscores (_), and hyphens (-), and have a length between 3-64 characters. For example: “Image Resizer” or “User Authentication”.
    • Test Domain: Select a domain under your account as the test domain. This domain will be used for online debugging and will only take effect in the test environment, without impacting your live production traffic.
  4. Choose a function creation method. Edge Cloud Apps supports two approaches:
  • 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.


Writing Your Function 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 an index.js file as the entry point. When a function is triggered, the platform executes code starting from the index.js file.

Testing Your Function

After writing your code in CloudIDE, you can use the built-in testing tools to test your function:

  1. Click the Save and Test button to save your current code version, automatically commit it to your Git repository, and deploy it to the test environment. This also initializes the simulated testing plugin, facilitating function testing.
  2. A unique test link will be generated and assigned to each function, displayed in the address bar of the testing page. This link points to an intermediate service that forwards your test requests to the function execution component in the test environment. Results are processed and returned to CloudIDE for your review and analysis. To begin debugging, append the URI path you want to test to this test link. For instance, to test the /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.
  3. You can configure request methods, headers, and body content in the respective tabs on the test page to simulate different request scenarios.
  4. Click the Send button to send the request to the test environment and view the function’s response and debugging information.


Deploying Your Function

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.

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!