serverless

Both Plugins have the same attributes.

IMPORTANT

Only Lua functions are allowed here and not other Lua code.

For example, anonymous functions are legal:

  1. local count = 1
  2. return function()
  3. count = count + 1
  4. ngx.say(count)
  5. end

But code other than functions are illegal:

serverless - 图2note

From v2.6, conf and ctx are passed as the first two arguments to a serverless function like regular Plugins.

Prior to v2.12.0, the phase before_proxy was called balancer. This was updated considering that this method would run after access and before the request goes Upstream and is unrelated to balancer.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. "uri": "/index.html",
  3. "plugins": {
  4. "serverless-pre-function": {
  5. "phase": "rewrite",
  6. "functions" : ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
  7. },
  8. "serverless-post-function": {
  9. "phase": "rewrite",
  10. "functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
  11. }
  12. },
  13. "upstream": {
  14. "type": "roundrobin",
  15. }
  16. }
  17. }'

Once you have configured the Plugin as shown above, you can make a request as shown below:

You will find a message “serverless pre-function” and “match uri /index.html” in the error.log.

To disable the serverless Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }