Migrate from VCL
If you already have VCL services with Fastly, all the logic you wrote in VCL can be accomplished in Compute services, in any supported language. This page provides the equivalent Compute service code for the most common patterns we see in VCL.
This guide is intended as a quick-start for an initial migration to the Compute platform, not to be a comprehensive library of code examples. If the pattern you are trying to migrate is not here or you are looking to do something more complex, you might find a code example in our examples library.
For more information about each of the languages with official SDK support, see choosing a language.
Boilerplate
The examples below assume that you start with the default starter kit for your chosen language when building your application. This is the default when you run fastly compute init.
Naming conventions
These examples use a common set of naming conventions to draw parallels with VCL:
req
: For the incoming client requestbeReq
: For a custom request built from scratch or by copyingreq
beResp
: For the return value of an origin fetchresp
: For a custom response built from scratch or by copying abeResp
Configuration
Load configuration data from a separate file
- Fastly VCL
table settings { "section.key": "some-value"}
declare local var.some_value STRING;set var.some_value = table.lookup( settings, "section.key");
- Rust
- JavaScript
- Go
// Using the config crate: https://docs.rs/configuse config::{Config, FileFormat}; let config_builder = Config::builder().add_source(config::File::from_str( include_str!("config.toml"), // assumes the existence of src/config.toml FileFormat::Toml, )); let settings = config_builder.build()?; let some_value = settings.get_string("section.key")?;
Load configuration data from an edge dictionary
- Fastly VCL
declare local var.some_value STRING;set var.some_value = table.lookup( example_dictionary, "key_name");
- Rust
- JavaScript
- Go
let settings = ConfigStore::open("example_config_store"); let some_value = match settings.get("key_name") { Some(value) => value, _ => panic!("Value not set") };
Requests
Add a header to a client request
- Fastly VCL
# constant valueset req.http.Accept-Encoding = "br";# dynamic valueset req.http.Accept-Encoding = var.accept_encoding;
- Rust
- JavaScript
- Go
// constant value req.set_header("Accept-Encoding", "br"); // dynamic value req.set_header("Accept-Encoding", accept_encoding);
Sort and sanitize a query string
- Fastly VCL
set req.url = querystring.filter_except(req.url, "a" + querystring.filtersep() + "b" + querystring.filtersep() + "c");set req.url = querystring.sort(req.url);
- Rust
- JavaScript
- Go
let mut qs: Vec<(String, String)> = req.get_query()?; qs.retain(|param| ["a", "b", "c"].contains(¶m.0.as_str())); qs.sort_by(|(a, _), (b, _)| a.cmp(b)); req.set_query(&qs)?;
Extract a query string parameter from the request
- Fastly VCL
declare local var.field STRING;set var.field = subfield(req.url.qs, "paramName", "&");
- Rust
- JavaScript
- Go
// assuming a request http://example.com?paramName=someValue let params: HashMap<String, String> = req.get_query()?; assert_eq!(params["paramName"], "someValue");
Remove a header from a client request
- Fastly VCL
unset req.http.Some-Header;
- Rust
- JavaScript
- Go
req.remove_header("some-header");
Modify a request URL path
- Fastly VCL
set req.url = "/new/path" + if(req.url.qs == "", "", "?") + req.url.qs;
- Rust
- JavaScript
- Go
req.set_path("/new/path");
Check for header presence on a client request
- Fastly VCL
if (req.http.Some-Header) { # ... do something ...}
- Rust
- JavaScript
- Go
if req.contains_header("some-header") { // ... do something ... }
Check whether a request header value contains a substring
- Fastly VCL
if (std.strstr(req.http.foo, "someValue")) { # ... do something ...}
- Rust
- JavaScript
- Go
fn header_val(header: Option<&HeaderValue>) -> &str { match header { Some(h) => h.to_str().unwrap_or(""), None => "", }} if header_val(req.get_header("some-header")).contains("someValue") { // ... do something ... }
Extract constituent parts of a request
- Fastly VCL
declare local var.req_method STRING;declare local var.req_url STRING;declare local var.req_header STRING;declare local var.req_protocol STRING;set var.req_method = req.method;set var.req_url = req.url;set var.req_header = req.http.My-Header;set var.req_body = req.body;set var.req_protocol = if (fastly_info.is_h2, "2", "1.1");
- Rust
- JavaScript
- Go
let method = req.get_method(); let url = req.get_url(); let my_header = req.get_header("my-header"); let version = req.get_version();
Identify a client's geolocation information
- Fastly VCL
declare local var.country_code STRING;declare local var.country_name STRING;declare local var.city STRING;set var.country_code = client.geo.country_code;set var.country_name = client.geo.country_name;set var.city = client.geo.city;
- Rust
- JavaScript
- Go
let client_ip = req.get_client_ip_addr().unwrap(); let geo = geo_lookup(client_ip).unwrap(); let country_code = geo.country_code(); let country_name = geo.country_name(); let city_name = geo.city();
Send data to a log endpoint
- Fastly VCL
log "syslog " + req.service_id + " request_logger :: " + now.sec + " " + client.ip + " " + req.url + " " + req.http.user-agent;
- Rust
- JavaScript
- Go
log_fastly::init_simple("request_logger", log::LevelFilter::Info);
let service_id = std::env::var("FASTLY_SERVICE_ID").unwrap_or("-".to_string());
let since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
let client_ip = req .get_client_ip_addr() .map(|ip| ip.to_string()) .unwrap_or_else(String::new);
let req_url = req.get_url_str();
let user_agent = req .get_header("USER_AGENT") .map(|header| header.to_str()) .transpose()? .unwrap_or("");
log::info!( "fastly_service_id: {service_id}, since_epoch: {since_epoch}, client_ip: {client_ip}, request_url: {req_url}, user_agent: {user_agent}" );
[dependencies] fastly = "^0.10.2" log-fastly = "^0.10.2" log = "^0.4.17"
Backends
Route requests to backends based on URL path match
- Fastly VCL
if (req.url.path == "/") { set req.backend = F_origin_0;} else if (req.url.path ~ "^/other/") { set req.backend = F_origin_1;} else { set req.backend = F_origin_2;}
- Rust
- JavaScript
- Go
match (req.get_method(), req.get_path()) { (&Method::GET, "/") => Ok(req.send("backend_one")?), (&Method::GET, path) if path.starts_with("/other/") => Ok(req.send("backend_two")?), _ => Ok(req.send("default_backend")?), }
Retry a request on error, using a different backend
- Fastly VCL
# not achievable for POST requests# because after a restart the body of a POST request will not be preservedsub vcl_recv { set req.backend = F_Host_1; if (req.restarts == 1) { set req.backend = F_Host_2; }}
sub vcl_fetch { if (req.restarts == 0 && beresp.status >= 500 && beresp.status < 600 && (req.method == "GET" or req.method == "HEAD")) { restart; }}
- Rust
- JavaScript
- Go
let body_bytes = req.take_body_bytes();
req.set_body(body_bytes.as_slice()); let mut beresp = req.send("backend_one")?; if beresp.get_status().is_server_error() { let mut retry_req = beresp.take_backend_request().unwrap(); retry_req.set_body(body_bytes); let beresp_retry = retry_req.send("backend_two")?; if !beresp_retry.get_status().is_server_error() { return Ok(beresp_retry); } } Ok(beresp)
Responses
Build a response from scratch
- Fastly VCL
set obj.status = 200;synthetic "Hello world";return(deliver);
- Rust
- JavaScript
- Go
let res = Response::from_body("Hello world");
Build an image response
- Fastly VCL
synthetic.base64 "R0lGODlh...=";
- Rust
- JavaScript
- Go
let res = Response::from_body(include_bytes!("fastly.jpg").as_ref()) .with_content_type(mime::IMAGE_JPEG) .with_header("cache-control", "private, no-store");
Add a header to a response
- Fastly VCL
# constant valueset resp.http.Some-Header = "someValue";# dynamic valueset resp.http.Set-Cookie = "origin-session=" + var.session + "; HttpOnly";
- Rust
- JavaScript
- Go
// constant value let mut res = req.send("example_backend")?; res.set_header("some-header", "bar"); // dynamic value let session = String::from("some-session-id"); res.set_header("set-cookie", format!("origin-session={}; HttpOnly", session));
Controlling the cache
Explicitly set a TTL
- Fastly VCL
set beresp.ttl = 60s;
- Rust
- JavaScript
- Go
// You can use individual helper methods, like `set_ttl`, // to modify individual cache override settings. req.set_ttl(60);
Force a pass
- Fastly VCL
return(pass);
- Rust
- JavaScript
- Go
// drop all overrides and force pass req.set_pass(true);
Explicitly set stale-while-revalidate
- Fastly VCL
set beresp.stale_while_revalidate = 60s;
- Rust
- JavaScript
- Go
req.set_stale_while_revalidate(60);