Skip to main content

Build and deploy a CDB EdgeCompute CDN application

CDB EdgeCompute CDN applications run inside a CDN resource's request pipeline and can inspect or modify requests and responses as they pass through the CDN.

Complete the Rust (CDN / Proxy-Wasm) setup before proceeding. Once deployed, attach the application to a CDN resource to process live traffic — that step is in CDN applications.

Write a handler

A CDN application consists of two parts: a root context that registers the filter factory, and a filter that implements the callbacks. Replace src/lib.rs with:

use proxy_wasm::traits::*;
use proxy_wasm::types::*;

proxy_wasm::main! {{
    proxy_wasm::set_log_level(LogLevel::Info);
    proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
        Box::new(Root)
    });
}}

struct Root;
impl Context for Root {}
impl RootContext for Root {
    fn create_http_context(&self, _: u32) -> Option<Box<dyn HttpContext>> {
        Some(Box::new(Filter))
    }
    fn get_type(&self) -> Option<ContextType> {
        Some(ContextType::HttpContext)
    }
}

struct Filter;
impl Context for Filter {}
impl HttpContext for Filter {
    fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
        self.add_http_request_header("x-edgecompute", "cdn");
        Action::Continue
    }
}

Root registers a new Filter for each incoming request via create_http_context. on_http_request_headers runs when the CDN receives request headers — this example adds a custom x-edgecompute header and returns Action::Continue to pass the request downstream unchanged. To stop the request instead, return Action::Pause.

Build

Compile the handler to a WebAssembly binary. The first build downloads dependencies and takes one to two minutes.

cargo build --release --target wasm32-wasip1

The binary is at ./target/wasm32-wasip1/release/my_cdn_app.wasm. This file is uploaded to CDB EdgeCompute in the next step.

Deploy

  1. In the CDB Technical Web Portal, navigate to CDB EdgeCompute > CDN Applications and click Create new application.
CDN Applications page with Create new application button
  1. Click the Upload binary card.
Create application page with Upload binary card and template options
  1. Select the .wasm file produced in the Build step.
  2. Enter a Name for the application.
  3. Click Save and deploy.

The application is now available in the CDN Applications list. To activate it, attach it to a CDN resource — the full walkthrough is in CDN applications.

Deploying requires two API calls: upload the binary to get its ID, then create the CDN application. An API token is required.

export GCORE_API_KEY="{YOUR_API_KEY}"
Do not commit API keys to source control.

Upload the binary

curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/binaries/raw' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/octet-stream' \
  --data-binary "@./target/wasm32-wasip1/release/my_cdn_app.wasm"

The response contains the binary ID:

{"id": 4695, "api_type": "proxy-wasm", "checksum": "c083e2b6...", "source": 1, "status": 1}

Create the application

export BINARY_ID=4695

curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/apps' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/json' \
  -d "{\"name\": \"my-cdn-app\", \"binary\": $BINARY_ID, \"status\": 1}"

The response contains the application ID:

{"id": 2553, "name": "my-cdn-app", "binary": 4695, "api_type": "proxy-wasm", "plan": "Free", "plan_id": 30, "status": 1}

To activate the application, attach it to a CDN resource. Attachment steps — including the CDN API call — are in CDN applications.