2021-02-26 21:40:08 +08:00
---
title: serverless
---
2019-10-31 09:27:28 +08:00
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-->
2022-03-13 19:49:15 +08:00
## Description
2019-11-24 21:15:39 +08:00
2019-08-26 13:28:08 +08:00
There are two plug-ins for serverless, namely `serverless-pre-function` and `serverless-post-function` .
2019-08-26 13:10:15 +08:00
The former runs at the beginning of the specified phase, while the latter runs at the end of the specified phase.
Both plug-ins receive the same parameters.
2019-11-24 21:15:39 +08:00
## Attributes
2019-08-26 13:10:15 +08:00
2020-09-23 08:11:27 +08:00
| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
2022-01-11 10:29:23 +08:00
| phase | string | optional | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"] | |
2020-09-23 08:11:27 +08:00
| functions | array[string] | required | | | A list of functions that are specified to run is an array type, which can contain either one function or multiple functions, executed sequentially. |
2019-08-26 13:10:15 +08:00
2021-03-01 12:00:06 +08:00
Note that only function is accepted here, not other types of Lua code. For example, anonymous functions are legal:< br / >
2020-04-28 15:53:29 +08:00
```lua
2019-08-26 13:10:15 +08:00
return function()
ngx.log(ngx.ERR, 'one')
end
```
Closure is also legal:
2020-04-28 15:53:29 +08:00
```lua
2019-08-26 13:10:15 +08:00
local count = 1
return function()
count = count + 1
ngx.say(count)
end
```
2019-11-24 21:15:39 +08:00
But code that is not a function type is illegal:
2020-04-28 15:53:29 +08:00
```lua
2019-08-26 13:10:15 +08:00
local count = 1
ngx.say(count)
```
2021-04-08 18:29:32 +08:00
Since `v2.6` , we pass the `conf` and `ctx` as the first two arguments to the servelss function, like a regular plugin.
2022-01-11 10:29:23 +08:00
Prior to `v2.12.0` , the phase `before_proxy` used to be called `balancer` . Considering that this method actually runs after `access` and before the request goes upstream, and has nothing to do with `balancer` , the new naming would be more appropriate.
2019-11-24 21:15:39 +08:00
## How To Enable
2019-08-26 13:10:15 +08:00
Here's an example, enable the serverless plugin on the specified route:
```shell
2021-12-03 08:54:07 +08:00
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-08-26 13:10:15 +08:00
{
"uri": "/index.html",
"plugins": {
2019-12-29 17:15:53 +08:00
"serverless-pre-function": {
"phase": "rewrite",
2019-12-30 20:28:36 +08:00
"functions" : ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
2021-04-08 18:29:32 +08:00
},
"serverless-post-function": {
"phase": "rewrite",
"functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
2021-12-03 08:54:07 +08:00
}
2019-08-26 13:10:15 +08:00
},
"upstream": {
"type": "roundrobin",
"nodes": {
2021-11-26 11:09:39 +08:00
"127.0.0.1:1980": 1
2019-08-26 13:10:15 +08:00
}
}
}'
```
2019-11-24 21:15:39 +08:00
## Test Plugin
2021-03-01 12:00:06 +08:00
Use curl to access:
2020-04-28 15:53:29 +08:00
```shell
2019-08-26 13:10:15 +08:00
curl -i http://127.0.0.1:9080/index.html
```
2021-04-08 18:29:32 +08:00
Then you will find the message 'serverless pre-function' and 'match uri /index.html' in the error.log,
2019-08-26 13:10:15 +08:00
which indicates that the specified function is in effect.
2019-11-24 21:15:39 +08:00
## Disable Plugin
2019-08-26 13:10:15 +08:00
When you want to disable the serverless plugin, it is very simple,
2021-03-01 12:00:06 +08:00
you can delete the corresponding json configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:
2019-08-26 13:10:15 +08:00
```shell
2020-11-28 19:05:14 +08:00
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-08-26 13:10:15 +08:00
{
"methods": ["GET"],
"uri": "/index.html",
"upstream": {
"type": "roundrobin",
"nodes": {
2021-11-26 11:09:39 +08:00
"127.0.0.1:1980": 1
2019-08-26 13:10:15 +08:00
}
}
}'
```
The serverless plugin has been disabled now. It works for other plugins.