Skip to content

[Backport 7.x] Documentation Update for FaaS use cases #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/connecting.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This page contains the information you need to connect and use the Client with

* <<auth-reference, Authentication options>>
* <<client-usage, Using the client>>
* <<client-faas-env, Using the Client in a Function-as-a-Service Environment>>
* <<client-connect-proxy, Connecting through a proxy>>
* <<client-error-handling, Handling errors>>
* <<product-check, Automatic product check>>
Expand Down Expand Up @@ -419,6 +420,76 @@ _Default:_ `null`
_Default:_ `null`
|===

[discrete]
[[client-faas-env]]
=== Using the Client in a Function-as-a-Service Environment

This section illustrates the best practices for leveraging the {es} client in a Function-as-a-Service (FaaS) environment.
The most influential optimization is to initialize the client outside of the function, the global scope.
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].
The following examples provide a skeleton for the best practices.

[discrete]
==== GCP Cloud Functions

[source,js]
----
'use strict'

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
// client initialisation
})

exports.testFunction = async function (req, res) {
// use the client
}
----

[discrete]
==== AWS Lambda

[source,js]
----
'use strict'

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
// client initialisation
})

exports.handler = async function (event, context) {
// use the client
}
----

[discrete]
==== Azure Functions

[source,js]
----
'use strict'

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
// client initialisation
})

module.exports = async function (context, req) {
// use the client
}
----

Resources used to assess these recommendations:

- https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations[GCP Cloud Functions: Tips & Tricks]
- https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions]
- https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level#global-variables[Azure Functions Python developer guide]
- https://docs.aws.amazon.com/lambda/latest/operatorguide/global-scope.html[AWS Lambda: Comparing the effect of global scope]


[discrete]
[[client-connect-proxy]]
Expand Down