@@ -83,4 +83,84 @@ if err != nil {
8383
8484defer res.Body.Close()
8585log.Println(res)
86- ------------------------------------
86+ ------------------------------------
87+
88+ [discrete]
89+ [[connecting-faas]]
90+ === Using the Client in a Function-as-a-Service Environment
91+
92+ This section illustrates the best practices for leveraging the {es} client in a Function-as-a-Service (FaaS) environment.
93+ The most influential optimization is to initialize the client outside of the function, the global scope.
94+ This practice does not only improve performance but also enables background functionality as – for example –
95+ https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[sniffing].
96+ The following examples provide a skeleton for the best practices.
97+
98+ [discrete]
99+ [[connecting-faas-gcp]]
100+ ==== GCP Cloud Functions
101+
102+ [source,go]
103+ ----------------------------
104+ package httpexample
105+
106+ import (
107+ "github.com/elastic/go-elasticsearch/v7"
108+ )
109+
110+ var client *elasticsearch.Client
111+
112+ func init() {
113+ var err error
114+
115+ ... # Client configuration
116+ client, err = elasticsearch.NewClient(cfg)
117+ if err != nil {
118+ log.Fatalf("elasticsearch.NewClient: %v", err)
119+ }
120+ }
121+
122+ func HttpExample(w http.ResponseWriter, r *http.Request) {
123+ ... # Client usage
124+ }
125+
126+ ----------------------------
127+
128+ [discrete]
129+ [[connecting-faas-aws]]
130+ ==== AWS Lambda
131+
132+ [source,go]
133+ ----------------------------
134+ package httpexample
135+
136+ import (
137+ "github.com/aws/aws-lambda-go/lambda"
138+ "github.com/elastic/go-elasticsearch/v7"
139+ )
140+
141+ var client *elasticsearch.Client
142+
143+ func init() {
144+ var err error
145+
146+ ... # Client configuration
147+ client, err = elasticsearch.NewClient(cfg)
148+ if err != nil {
149+ log.Fatalf("elasticsearch.NewClient: %v", err)
150+ }
151+ }
152+
153+ func HttpExample() {
154+ ... # Client usage
155+ }
156+
157+ func main() {
158+ lambda.Start(HttpExample)
159+ }
160+ ----------------------------
161+
162+ Resources used to assess these recommendations:
163+
164+ * https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations[GCP Cloud Functions: Tips & Tricks]
165+ * https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions]
166+ * https://docs.aws.amazon.com/lambda/latest/operatorguide/global-scope.html[AWS Lambda: Comparing the effect of global scope]
0 commit comments