Skip to content

Commit 7c69f04

Browse files
[Backport 7.x] Documentation Update for FaaS use cases (#1541)
Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 93a7e52 commit 7c69f04

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

docs/connecting.asciidoc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This page contains the information you need to connect and use the Client with
88

99
* <<auth-reference, Authentication options>>
1010
* <<client-usage, Using the client>>
11+
* <<client-faas-env, Using the Client in a Function-as-a-Service Environment>>
1112
* <<client-connect-proxy, Connecting through a proxy>>
1213
* <<client-error-handling, Handling errors>>
1314
* <<product-check, Automatic product check>>
@@ -419,6 +420,76 @@ _Default:_ `null`
419420
_Default:_ `null`
420421
|===
421422

423+
[discrete]
424+
[[client-faas-env]]
425+
=== Using the Client in a Function-as-a-Service Environment
426+
427+
This section illustrates the best practices for leveraging the {es} client in a Function-as-a-Service (FaaS) environment.
428+
The most influential optimization is to initialize the client outside of the function, the global scope.
429+
This practice does not only improve performance but also enables background functionality as – for example – https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[sniffing].
430+
The following examples provide a skeleton for the best practices.
431+
432+
[discrete]
433+
==== GCP Cloud Functions
434+
435+
[source,js]
436+
----
437+
'use strict'
438+
439+
const { Client } = require('@elastic/elasticsearch')
440+
441+
const client = new Client({
442+
// client initialisation
443+
})
444+
445+
exports.testFunction = async function (req, res) {
446+
// use the client
447+
}
448+
----
449+
450+
[discrete]
451+
==== AWS Lambda
452+
453+
[source,js]
454+
----
455+
'use strict'
456+
457+
const { Client } = require('@elastic/elasticsearch')
458+
459+
const client = new Client({
460+
// client initialisation
461+
})
462+
463+
exports.handler = async function (event, context) {
464+
// use the client
465+
}
466+
----
467+
468+
[discrete]
469+
==== Azure Functions
470+
471+
[source,js]
472+
----
473+
'use strict'
474+
475+
const { Client } = require('@elastic/elasticsearch')
476+
477+
const client = new Client({
478+
// client initialisation
479+
})
480+
481+
module.exports = async function (context, req) {
482+
// use the client
483+
}
484+
----
485+
486+
Resources used to assess these recommendations:
487+
488+
- https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations[GCP Cloud Functions: Tips & Tricks]
489+
- https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions]
490+
- https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level#global-variables[Azure Functions Python developer guide]
491+
- https://docs.aws.amazon.com/lambda/latest/operatorguide/global-scope.html[AWS Lambda: Comparing the effect of global scope]
492+
422493

423494
[discrete]
424495
[[client-connect-proxy]]

0 commit comments

Comments
 (0)