Skip to main content

OpenWhisk

The openwhisk plugin invokes an Apache OpenWhisk action as a blocking request. It returns the action result or a structured status, headers, and body.

The plugin forwards only the request body. Client query parameters and headers are not forwarded; OpenWhisk receives a POST request with a JSON content type and the configured Basic credentials.

Examples​

The examples show how to invoke an action with JSON and return a custom HTTP response from an action.

Set Up OpenWhisk​

Use OpenWhisk Standalone for local evaluation, or connect the gateway to an existing OpenWhisk deployment.

Start OpenWhisk Standalone​

Start OpenWhisk Standalone, install the wsk CLI, and connect the gateway container to its network:

export GATEWAY_CONTAINER=replace-with-gateway-container-name
docker network create gateway-openwhisk-net
docker network connect gateway-openwhisk-net "$GATEWAY_CONTAINER"
docker run -d --name ow-standalone \
--network gateway-openwhisk-net \
-e DOCKER_API_VERSION=1.40 \
-p 127.0.0.1:3233:3233 \
-p 127.0.0.1:3232:3232 \
-v /var/run/docker.sock:/var/run/docker.sock \
openwhisk/standalone:nightly
docker exec ow-standalone waitready

The explicit Docker API version allows the older Docker client bundled in the Standalone image to communicate with current Docker engines.

Configure wsk with the API host and guest credentials printed by the Standalone container, then export the values used by the gateway:

docker logs ow-standalone 2>&1 | grep 'wsk property set'

wsk property set \
--apihost "http://127.0.0.1:3233" \
--auth "replace-with-guest-uuid:replace-with-guest-secret"

export OPENWHISK_API_HOST="http://ow-standalone:3233"
export OPENWHISK_SERVICE_TOKEN="replace-with-guest-uuid:replace-with-guest-secret"
export OPENWHISK_NAMESPACE="guest"

Connect to an Existing Deployment​

For a shared or production deployment, configure wsk with the existing OpenWhisk API, then export an API host reachable from every gateway instance and a service token for the action namespace:

wsk property set \
--apihost "https://replace-with-openwhisk-api-host" \
--auth "replace-with-service-token"

export OPENWHISK_API_HOST="https://replace-with-openwhisk-api-host"
export OPENWHISK_SERVICE_TOKEN="replace-with-service-token"
export OPENWHISK_NAMESPACE="replace-with-action-namespace"

Invoke an Action with JSON​

Create an action that returns a greeting:

hello.js
function main(params) {
return { greeting: `Hello ${params.name}!` };
}

Deploy the action:

wsk action update hello hello.js --kind nodejs:default

Configure a route to invoke the action:

curl "http://127.0.0.1:9180/apisix/admin/routes/openwhisk-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/openwhisk",
"plugins": {
"openwhisk": {
"api_host": "${OPENWHISK_API_HOST}",
"service_token": "${OPENWHISK_SERVICE_TOKEN}",
"namespace": "${OPENWHISK_NAMESPACE}",
"action": "hello",
"timeout": 60000
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF

The configurations include a conventional fallback backend. The plugin returns the action response before that backend is contacted. The 60-second timeout allows the local Standalone deployment to pull and start an action runtime on its first invocation.

Invoke the action with a JSON body:

curl "http://127.0.0.1:9080/openwhisk" \
-H "Content-Type: application/json" \
-d '{"name":"API7"}'

The gateway returns the action result:

{"greeting":"Hello API7!"}

Return a Custom HTTP Response​

Create an action that returns a status, header, and body:

custom-response.js
function main() {
return {
statusCode: 201,
headers: { "X-Action": "created" },
body: "created by OpenWhisk",
};
}

Deploy the action:

wsk action update custom-response custom-response.js --kind nodejs:default

Update the existing plugin configuration to invoke the new action:

Replace the plugin configuration on the existing route:

curl "http://127.0.0.1:9180/apisix/admin/routes/openwhisk-route/plugins/openwhisk" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"api_host": "${OPENWHISK_API_HOST}",
"service_token": "${OPENWHISK_SERVICE_TOKEN}",
"namespace": "${OPENWHISK_NAMESPACE}",
"action": "custom-response",
"timeout": 60000
}
EOF

Send the request again:

curl --include "http://127.0.0.1:9080/openwhisk" \
-H "Content-Type: application/json" \
-d '{}'

The action controls the client response:

HTTP/1.1 201 Created
X-Action: created
...
created by OpenWhisk