|
1 | | -[[aws-lambda-extension]] |
2 | | -= AWS Lambda Extension (Experimental) |
3 | | - |
4 | | -experimental::[] |
5 | | - |
6 | | -Elastic's APM Agents instrument AWS Lambda functions and dispatch APM data via an AWS Lambda Extension. |
7 | | - |
8 | | -[discrete] |
9 | | -[[aws-lambda-arch-old]] |
10 | | -== Extension Architecture |
11 | | - |
12 | | -Normally, during the execution of a Lambda function, there's only a single language process running in the AWS Lambda execution environment. However, with an AWS Lambda Extension, Lambda users can run a _second_ process alongside their main service/application process. |
13 | | - |
14 | | -image:images/data-flow.png[image showing data flow from lambda function, to extension, to APM Server] |
15 | | - |
16 | | -By using a custom-built AWS Lambda Extension, Elastic APM Agents can send data to a locally running Lambda Extension process, and that process will forward data on to APM Server. The Lambda Extension ensures that any latency between the Lambda function and the AWS Server instance will not cause latency in the Lambda function/Service itself. |
17 | | - |
18 | | -[discrete] |
19 | | -[[aws-lambda-instrumenting]] |
20 | | -== Instrumenting a Lambda Function |
21 | | - |
22 | | -The rest of this guide contains instructions for instrumenting a Lambda function. There are two high level steps to instrumenting an AWS Lambda function. |
23 | | - |
24 | | -1. <<aws-lambda-configure-layer>> |
25 | | -2. <<aws-lambda-handler>> |
26 | | - |
27 | | -[discrete] |
28 | | -[[aws-lambda-configure-layer]] |
29 | | -=== Configuring the APM Lambda Extension Layer |
30 | | - |
31 | | -First, you'll need to https://github.com/elastic/apm-aws-lambda/releases[pick the right ARN from the extension release table] for your AWS Lambda function. The ARN has the pattern `arn:aws:lambda:<AWS_REGION_KEY>:267093732750:layer:elastic-apm-extension-<APM_EXTENSION_VERSION>-<ARCHITECTURE_KEY>:<LAYER_VERSION>` and depends on: |
32 | | - |
33 | | -* The AWS region your Lambda function runs in. The APM Lambda Extension layer needs to be in the same region as your Lambda function. |
34 | | -* The architecture (_x86_64_ or _arm64_) used for your Lambda function. |
35 | | -* The version of the APM Lambda Extension you would like to use. |
| 1 | +[[monitoring-aws-lambda]] |
| 2 | += Monitoring AWS Lambda Functions |
36 | 3 |
|
37 | | -You'll then need to configure your function to use that layer. To add a layer |
| 4 | +Elastic APM lets you monitor your AWS Lambda functions. |
| 5 | +The natural integration of {apm-guide-ref}/apm-distributed-tracing.html[distributed tracing] into your AWS Lambda functions provides insights into the functions' execution and runtime behavior as well as their relationships and dependencies to other services. |
38 | 6 |
|
39 | | -1. Navigate to your function in the AWS Console |
40 | | -2. Scroll to the Layers section and click the _Add a layer_ button image:images/config-layer.png[image of layer configuration section in AWS Console] |
41 | | -3. Choose the _Specify an ARN_ radio button |
42 | | -4. Enter the Version ARN of the APM Lambda Extension layer in the _Specify an ARN_ text input |
43 | | -5. Click the _Add_ button |
| 7 | +To get started with the setup of Elastic APM for your Lambda functions, checkout the language-specific guides: |
44 | 8 |
|
45 | | -[discrete] |
46 | | -[[aws-lambda-env-vars]] |
47 | | -==== Configure Environment Variables |
| 9 | +* {apm-node-ref}/lambda.html[Quick Start with APM on AWS Lambda - Node.js] |
| 10 | +* {apm-py-ref}/lambda-support.html[Quick Start with APM on AWS Lambda - Python] |
| 11 | +* {apm-java-ref}/aws-lambda.html[Quick Start with APM on AWS Lambda - Java] |
48 | 12 |
|
49 | | -Finally, once the layer's in place you'll need to configure a few environment variables. To configure variables |
| 13 | +Learn more about the <<aws-lambda-arch, architecture>> of Elastic APM for AWS Lambda. |
50 | 14 |
|
51 | | -1. Navigate to your function in the AWS Console |
52 | | -2. Click on the _Configuration_ tab |
53 | | -3. Click on _Environment variables_ |
54 | | -4. Add the necessary variables. |
| 15 | +[[aws-lambda-arch]] |
| 16 | +== APM Architecture for AWS Lambda |
55 | 17 |
|
56 | | -The following environment variables are relevant for instrumenting a Lambda function: |
| 18 | +AWS Lambda uses a special execution model to provide a scalable, on-demand compute service for code execution. In particular, AWS freezes the execution environment of a lambda function when no active requests are being processed. This execution model poses additional requirements on APM in the context of AWS Lambda functions: |
57 | 19 |
|
58 | | -* (required) `ELASTIC_APM_LAMBDA_APM_SERVER`: + |
59 | | -This required config option controls where the Lambda extension will ship data. This should be the URL of the final APM Server destination for your telemetry. |
60 | | - |
61 | | -* (required) `ELASTIC_APM_SECRET_TOKEN` or `ELASTIC_APM_API_KEY`: + |
62 | | -One of these needs to be set as the authentication method that the extension uses when sending data to the URL configured via `ELASTIC_APM_LAMBDA_APM_SERVER`. |
63 | | - |
64 | | -* (optional) `ELASTIC_APM_SERVICE_NAME`: + |
65 | | -The configured name of your application or service. The APM Agent will use this value when reporting data to APM Server. If unset, the APM Agent will automatically set the value based on the Lambda function name. Use this config option if you want to group multiple Lambda functions under a single service entity in APM. |
| 20 | +1. To avoid data loss, APM data collected by APM agents needs to be flushed before the execution environment of a lambda function is frozen. |
| 21 | +2. Flushing APM data must be fast so as not to impact the response times of lambda function requests. |
66 | 22 |
|
67 | | -* (optional) `ELASTIC_APM_DATA_RECEIVER_TIMEOUT_SECONDS`: + |
68 | | -The timeout value, in seconds, for the Lambda Extension's server receiving data from the agent. The _default_ is `15`. |
69 | | - |
70 | | -* (optional) `ELASTIC_APM_SEND_STRATEGY`: + |
71 | | -Whether to synchronously flush APM agent data from the extension to the APM server at the end of the function invocation. |
72 | | -The two accepted values are `background` and `syncflush`. The _default_ is `syncflush`. |
73 | | -** The `background` strategy indicates that the extension will not flush when it receives a signal that the function invocation |
74 | | -has completed. It will instead send any remaining buffered data on the next function invocation. The result is that, if the |
75 | | -function is not subsequently invoked for that Lambda environment, the buffered data will be lost. However, for lambda functions |
76 | | -that have a steadily frequent load pattern the extension could delay sending the data to the APM server to the next lambda |
77 | | -request and do the sending in parallel to the processing of that next request. This potentially would improve both the lambda |
78 | | -function response time and its throughput. |
79 | | -** The other value, `syncflush` will synchronously flush all remaining buffered APM agent data to the APM server when the |
80 | | -extension receives a signal that the function invocation has completed. This strategy blocks the lambda function from receiving |
81 | | -the next request until the extension has flushed all the data. This has a negative effect on the throughput of the function, |
82 | | -though it ensures that all APM data is sent to the APM server. |
| 23 | +To accomplish the above, Elastic's APM Agents instrument AWS Lambda functions and dispatch APM data via an https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html[AWS Lambda Extension]. |
83 | 24 |
|
84 | | -An example configuration of the APM Lambda Extension might look like the following |
| 25 | +Normally, during the execution of a Lambda function, there's only a single language process running in the AWS Lambda execution environment. With an AWS Lambda Extension, Lambda users run a _second_ process alongside their main service/application process. |
85 | 26 |
|
86 | | -[source,bash] |
87 | | ----- |
88 | | -ELASTIC_APM_LAMBDA_APM_SERVER=https://your-apm-server-url:443 # (required) this is your APM Server URL |
89 | | -ELASTIC_APM_SECRET_TOKEN=shhhhhhitsasecret # (required) this is your APM Server's secret token |
90 | | -ELASTIC_APM_SERVICE_NAME=yourApmServiceName # (optional) use this to group multiple lambda functions |
91 | | -ELASTIC_APM_DATA_RECEIVER_TIMEOUT_SECONDS=20 # (optional) wait 20s to receive data from the APM agent |
92 | | -ELASTIC_APM_SEND_STRATEGY=background # (optional) this is the default strategy |
93 | | ----- |
| 27 | +image:images/architecture-white.png[image showing data flow from lambda function, to extension, to APM Server] |
94 | 28 |
|
95 | | -For a _minimal configuration_ you need to specify the `ELASTIC_APM_LAMBDA_APM_SERVER` and the `ELASTIC_APM_SECRET_TOKEN` config options. |
96 | | - |
97 | | -[discrete] |
98 | | -[[aws-lambda-handler]] |
99 | | -=== Configuring the Agent and Lambda Function handler |
100 | | - |
101 | | -After the extension has been installed, install the relevant language agent and wrap the Lambda function handler. |
102 | | - |
103 | | - |
104 | | -[discrete] |
105 | | -[[aws-lambda-nodejs]] |
106 | | -==== Node.js |
107 | | - |
108 | | -To install the Node.js agent in Lambda you'll use a second layer. You'll need to https://github.com/elastic/apm-agent-nodejs/releases[pick the right ARN from the agent release table]. The ARN has the pattern `arn:aws:lambda:<AWS_REGION_KEY>:267093732750:layer:elastic-apm-node-ver-<APM_EXTENSION_VERSION>:<LAYER_VERSION>` and depends on: |
109 | | - |
110 | | -* The AWS region your Lambda function runs in. The APM Lambda Extension layer needs to be in the same region as your Lambda function. |
111 | | -* The version of the APM Lambda Extension you would like to use. |
112 | | - |
113 | | -You'll then need to configure your function to use that layer. To add a layer |
114 | | - |
115 | | -1. Navigate to your function in the AWS Console |
116 | | -2. Scroll to the Layers section and click the _Add a layer_ button image:images/config-layer.png[image of layer configuration section in AWS Console] |
117 | | -3. Choose the _Specify an ARN_ radio button |
118 | | -4. Enter the Version ARN of the APM Lambda Extension layer in the _Specify an ARN_ text input |
119 | | -5. Click the _Add_ button |
120 | | - |
121 | | -Finally, to have the Node.js agent automatically wrap your Lambda function handler, add the following environment variable. |
122 | | - |
123 | | -[source] |
124 | | ---- |
125 | | -NODE_OPTIONS=-r elastic-apm-node/start |
126 | | ---- |
127 | | - |
128 | | -See the {apm-node-ref}/lambda.html[Node.js agent setup guide] for detailed instructions on setting up the Node.js agent for AWS Lambda. |
129 | | - |
130 | | -[discrete] |
131 | | -[[aws-lambda-python]] |
132 | | -==== Python |
133 | | - |
134 | | -In Python, you wrap a Lambda function handler using the following syntax. |
135 | | - |
136 | | -[source,python] |
137 | | ----- |
138 | | -from elasticapm import capture_serverless |
139 | | -@capture_serverless() |
140 | | -def handler(event, context): |
141 | | - return {"statusCode": r.status_code, "body": "Success!"} |
142 | | ----- |
143 | | - |
144 | | -See the {apm-py-ref}/lambda-support.html[Python agent setup guide] for detailed instructions on setting up the Python agent for AWS Lambda. |
145 | | - |
146 | | -[discrete] |
147 | | -[[aws-lambda-java]] |
148 | | -==== Java |
149 | | - |
150 | | -Like the extension, the Elastic APM Java agent is installed as a Lambda layer. |
151 | | - |
152 | | -See the {apm-java-ref}/aws-lambda.html[Java agent setup guide] for detailed instructions on setting up the Java agent for AWS Lambda. |
| 29 | +By using an AWS Lambda Extension, Elastic APM Agents can send data to a local Lambda Extension process, and that process will forward data on to APM Server asynchronously. The Lambda Extension ensures that any potential latency between the Lambda function and the APM Server instance will not cause latency in the request flow of the Lambda function itself. |
153 | 30 |
|
154 | 31 | [[aws-lambda-config-options]] |
155 | 32 | == Configuration Options for APM on AWS Lambda |
156 | 33 |
|
157 | 34 | The recommended way of configuring the APM Lambda Extension and the APM Agents on AWS Lambda is through the Lambda function's environment variables. |
158 | 35 |
|
159 | | -[float] |
| 36 | +The configuration options for the APM Agents are documented in the corresponding language agents: |
| 37 | + |
| 38 | +* {apm-node-ref}/configuration.html[Configuration options - Node.js APM Agent] |
| 39 | +* {apm-py-ref}/configuration.html[Configuration options - Python APM Agent] |
| 40 | +* {apm-java-ref}/configuration.html[Configuration options - Java APM Agent] |
| 41 | + |
| 42 | +The following configuration options are particularly relevant for Elastic's APM on AWS Lambda: |
| 43 | + |
| 44 | +[[aws-lambda-extension]] |
160 | 45 | === `ELASTIC_APM_LAMBDA_APM_SERVER` |
161 | 46 | This required config option controls where the Lambda extension will ship data. This should be the URL of the final APM Server destination for your telemetry. |
162 | 47 |
|
163 | | -[float] |
164 | 48 | === `ELASTIC_APM_SECRET_TOKEN` or `ELASTIC_APM_API_KEY` |
165 | 49 | One of these needs to be set as the authentication method that the extension uses when sending data to the URL configured via `ELASTIC_APM_LAMBDA_APM_SERVER`. |
166 | 50 |
|
167 | | -[float] |
168 | 51 | === `ELASTIC_APM_SERVICE_NAME` |
169 | 52 | The configured name of your application or service. The APM Agent will use this value when reporting data to the APM Server. If unset, the APM Agent will automatically set the value based on the Lambda function name. Use this config option if you want to group multiple Lambda functions under a single service entity in APM. |
170 | 53 |
|
171 | | -[float] |
172 | 54 | === `ELASTIC_APM_DATA_RECEIVER_TIMEOUT_SECONDS` |
173 | | -The timeout value, in seconds, for the Lambda Extension's server receiving data from the agent. The _default_ is `15`. |
| 55 | +The APM Lambda Extension's timeout value, in seconds, for receiving data from the APM Agent. The _default_ is `15`. |
| 56 | + |
| 57 | +=== `ELASTIC_APM_DATA_RECEIVER_SERVER_PORT` |
| 58 | +The port on which the APM Lambda Extension listens to receive data from the APM Agent. The _default_ is `8200`. |
| 59 | + |
| 60 | +=== `ELASTIC_APM_DATA_FORWARDER_TIMEOUT_SECONDS` |
| 61 | +The timeout value, in seconds, for the Lambda Extension's HTTP client sending data to the APM Server. The _default_ is `3`. If the Extension's attempt to send APM data during this time interval is not successful, the extension queues back the data. Further attempts at sending the data are governed by an exponential backoff algorithm: data will be sent after a increasingly large grace period of 0, then circa 1, 4, 9, 16, 25 and 36 seconds, provided that the Lambda function execution is ongoing. |
174 | 62 |
|
175 | | -[float] |
176 | 63 | === `ELASTIC_APM_SEND_STRATEGY` |
177 | | -Whether to synchronously flush APM agent data from the extension to the APM server at the end of the function invocation. |
| 64 | +Whether to synchronously flush APM agent data from the extension to the APM Server at the end of the function invocation. |
178 | 65 | The two accepted values are `background` and `syncflush`. The _default_ is `syncflush`. |
179 | 66 |
|
180 | 67 | * The `background` strategy indicates that the extension will not flush when it receives a signal that the function invocation |
181 | 68 | has completed. It will instead send any remaining buffered data on the next function invocation. The result is that, if the |
182 | 69 | function is not subsequently invoked for that Lambda environment, the buffered data will be lost. However, for lambda functions |
183 | | -that have a steadily frequent load pattern the extension could delay sending the data to the APM server to the next lambda |
| 70 | +that have a steadily frequent load pattern the extension could delay sending the data to the APM Server to the next lambda |
184 | 71 | request and do the sending in parallel to the processing of that next request. This potentially would improve both the lambda |
185 | 72 | function response time and its throughput. |
186 | | -* The other value, `syncflush` will synchronously flush all remaining buffered APM agent data to the APM server when the |
| 73 | +* The other value, `syncflush` will synchronously flush all remaining buffered APM agent data to the APM Server when the |
187 | 74 | extension receives a signal that the function invocation has completed. This strategy blocks the lambda function from receiving |
188 | 75 | the next request until the extension has flushed all the data. This has a negative effect on the throughput of the function, |
189 | 76 | though it ensures that all APM data is sent to the APM server. |
190 | 77 |
|
191 | | -[role="exclude",id="aws-lambda-arch"] |
192 | | -== AWS Lambda architecture |
193 | | - |
194 | | -Please see <<aws-lambda-arch>> instead. This link will be fixed soon. |
| 78 | +=== `ELASTIC_APM_LOG_LEVEL` |
| 79 | +The logging level to be used by both the APM Agent and the Lambda Extension. Supported values are `trace`, `debug`, `info`, `warning`, `error`, `fatal` and `panic`. |
0 commit comments