Skip to main content

Lua Policy Reference

Reference of the custom policy: a Lua script run by the gateway on each request and each response. It is deployed as an Envoy Gateway EnvoyExtensionPolicy.

Policy Target​

FieldValuesMeaning
targetRef.kindHTTPRoute, Gateway, GRPCRouteAn API instance, or a whole gateway
targetRef.nameNameThe API instance or the gateway
targetRef.sectionNameName, optionalOne access point of a gateway

Policy Parameters​

ParameterDefaultMeaning
scriptSourceInlineInline: written in the policy. Stored: a Lua Script configuration
scriptAn example scriptThe script, when inline
storedScript—The stored script, when scriptSource is Stored

Entry points​

Script
function envoy_on_request(request_handle)
request_handle:headers():add("x-request-source", "gateway")
end

function envoy_on_response(response_handle)
local status = response_handle:headers():get(":status")
response_handle:logInfo("status " .. status)
end
FunctionCalledTypical use
envoy_on_requestBefore the request is sent to the backendAdd or check headers, reject early
envoy_on_responseBefore the response is sent to the clientAdd headers, log

Behavior and limits​

  • The script runs in the request path of the gateway: keep it short and free of blocking calls.
  • A runtime error in the script fails the request.
  • A stored script is shared by every policy that references it: a change reaches all of them.
Prefer a built-in rule when one exists

Header changes, URL rewrite, authentication and rate limiting have dedicated rules that are validated and observable. Keep Lua for what none of them covers.

Procedure: Lua filter.