Edge Functions: Available anytime

Updated at:

Edge Functions is a serverless solution that connects frontend applications to backend logic. You can deploy custom code to global edge nodes for low-latency, high-availability APIs and event handling without managing servers. Use Edge Functions to validate data, call third-party services like AI models, or aggregate APIs. PolarDB Supabase offers a complete set of Edge Functions features, allowing you to easily build and deploy these functions without server management.

Overview

PolarDB Supabase Edge Functions is a lightweight, serverless service built on the Deno runtime and designed for modern full-stack applications.

Key features include:

  • Millisecond cold starts and strong isolation: Built on Deno and V8 Isolates, each request runs in a separate sandbox. This ensures complete memory and scope isolation while reducing cold start times to milliseconds.

  • Native TypeScript support: You can write and deploy .ts files directly without build tools like Webpack.

  • Seamless integration with the Supabase ecosystem: Functions can directly access Supabase services like the database, authentication, and storage without additional configuration.

The open-source version of Supabase includes the runtime engine for Edge Functions, but its management backend (FaaS Backend) is not open source. If you self-host Supabase, you can simulate running a function locally but cannot deploy it to your environment using the graphical interface (Studio) or the command-line interface (CLI). This prevents you from using this core feature in a production environment. PolarDB Supabase bridges this gap with a self-developed, lightweight FaaS management system. It provides full functionality and ensures your code and metadata are stored in your own resources, giving you complete control.

Use cases

Edge Functions act as an intelligent hub connecting your frontend, database, and third-party services. Typical use cases include:

  • Integrate AI services: Call Large Language Model (LLM) APIs within a function to implement AI features like content generation, summarization, semantic search, or content moderation.

  • Data validation and transformation: Execute complex data validation, permission checks, or format transformation logic before writing data to the database.

  • Secure third-party API calls: Encapsulate third-party service calls with sensitive credentials (like an API key) or CORS restrictions in a function to avoid exposing keys on the client.

  • API aggregation: Combine responses from multiple database queries or external APIs into a single data source for your frontend, reducing the number of network requests.

  • Secure proxy: Provide a secure middle layer for sensitive operations, such as deletions or high-privilege modifications, to prevent clients from performing high-risk actions directly.

Advantages

Compared to self-hosting the open-source version or using the fully managed Supabase Cloud, PolarDB Supabase offers full functionality with enterprise-grade control and flexibility.

  • Complete development experience: Deploy functions using either the online editor (Studio) in the PolarDB Supabase console or by uploading a local package (supports eszip and zip formats), matching the experience on Supabase Cloud.

  • Enterprise-grade resource control: Your function code is stored in your private repository, and metadata is stored in your PolarDB for PostgreSQL cluster. This ensures data security.

  • High performance and strong isolation: Based on Deno and V8 Isolates technology, it achieves millisecond cold starts and secure isolation of memory and scope between requests.

  • Native seamless integration: Directly access the database, authentication, and storage services within PolarDB Supabase without extra configuration.

  • Native TypeScript support: Write and deploy .ts files directly without complex build steps.

Notes

  • Code deployment: When you deploy using the online editor in the PolarDB Supabase console, the system automatically handles dependencies and bundling. If you upload a local package, ensure the format is correct:

    • eszip: A single, executable file package that includes all dependencies.

    • zip: Contains only the source code. The server bundles the dependencies after upload.

  • Resource limits: Each function execution has a maximum runtime duration and memory limit to prevent abuse. Design your code to be efficient.

  • Network access restrictions: By default, PolarDB Supabase cannot access the public internet. You must first configure a NAT Gateway to enable public network access.

Procedure

Step 1: Configure public NAT gateway

You must first configure a NAT Gateway for PolarDB Supabase to enable public network access.

  1. Create a NAT Gateway: Go to the NAT Gateway purchase page to create a gateway. During creation, ensure you select the same VPC and VSwitch as your PolarDB for PostgreSQL instance.

  2. Configure an SNAT entry: Go to the NAT Gateway page. In the Operation column of the target gateway, click Set SNAT, then click Create SNAT Entry. Configure the parameters as follows:

    • SNAT Entry Scope: VPC.

    • Select EIP: Select the EIP that provides public access from the drop-down list.

Step 2 (Optional): Mount storage

Important
  • If storage was automatically mounted when you created the PolarDB Supabase application, you can skip this step.

  • Mounting storage automatically restarts the PolarDB Supabase application. Plan your operations accordingly.

The Edge Functions feature requires storage for the bundled code files. Before you proceed, you must mount a storage instance to your PolarDB Supabase application.

  1. Log in to the PolarDB console. On the cluster details page, go to AI Capability > AI Application in the left-side navigation pane, and find your PolarDB Supabase application.

  2. In the Dependent Resources column, click Mount Storage. On the Select Cold Storage Directory page, configure the following settings to mount the storage.

    • Cold Archive Bucket ID:

      • If you have previously created a cold storage instance, select it as needed.

      • If you have not created a cold storage instance, click Create Directory. Configure the following parameters to create a cold storage instance.

        Parameter

        Description

        Storage Type

        Select your storage type as needed.

        • Locally Redundant Storage (LRS): Data is redundantly stored within a specific availability zone.

        • Zone-Redundant Storage (ZRS): Data is redundantly stored across multiple availability zones.

        UID

        Enter the user ID (UID) of your Alibaba Cloud account.

    • AI Cache AccessKey and AI Cache SK: Enter the AccessKey of your Alibaba Cloud account.

Step 3: Configure application parameters

Important

Modifying parameters automatically restarts the PolarDB Supabase application. Plan your operations accordingly.

  1. Log in to the PolarDB console. On the cluster details page, go to AI Capability > AI Application in the left-side navigation pane, and find your PolarDB Supabase application.

  2. Click your Application ID to go to the application details page. On the Configure tab, modify the configuration information: replace auth.GOTRUE_SITE_URL, studio.SUPABASE_PUBLIC_URL, and auth.API_EXTERNAL_URL with your application's public URL.

Step 4: Create function

  1. Log in to the PolarDB console. On the cluster details page, go to AI Capability > AI Application in the left-side navigation pane, and find your PolarDB Supabase application.

  2. Connect to the Supabase application, go to the Edge Functions page from the left-side navigation pane, and click Deploy a new function. You can create a function in either of the following ways:

    • Via Editor: Write or paste your function code directly into the built-in code editor.

    • Via Upload: Upload a local package file.

  3. After writing code in the editor or uploading a file, enter a Function Name and click Deploy Function.

Step 5: Deploy and invoke

After a successful deployment, the system generates a unique invocation URL with the following format: http://<your Supabase instance public URL>/functions/<version>/<your function name>.

  1. Log in to the PolarDB console. On the cluster details page, go to AI Capability > AI Application in the left-side navigation pane, and find your PolarDB Supabase application.

  2. Connect to the Supabase application, go to the Edge Functions page from the left-side navigation pane, and find your function. Find the invocation URL in the URL column.