# AWS Lambda + APM Extension: How to inject custom fields into logs

**URL:** <https://discuss.elastic.co/t/aws-lambda-apm-extension-how-to-inject-custom-fields-into-logs/378761>\
**Category:** APM\
**Tags:** aws-lambda\
**Created:** [May 31, 2025, 9:04am UTC](https://discuss.elastic.co/t/aws-lambda-apm-extension-how-to-inject-custom-fields-into-logs/378761 "2025-05-31T09:04:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhannhathegreat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nhannhathegreat/32/143146_2.png) [@nhannhathegreat](https://discuss.elastic.co/u/nhannhathegreat)\
**Post date:** [May 31, 2025, 9:04am UTC](https://discuss.elastic.co/t/aws-lambda-apm-extension-how-to-inject-custom-fields-into-logs/378761/1 "2025-05-31T09:04:36Z")

</div>

**Kibana version** : 8.5

**Elasticsearch version** : 8.5

**APM Server version** : 8.5

**APM Agent language and version** : apm-aws-lambda(Nodejs), 1.5.8

**Extension Layer ARN** : arn:aws:lambda:ap-northeast-2:267093732750:layer:elastic-apm-extension-ver-1-5-8-x86\_64:1

**Install method** : Self-hosted via Helm chart

**Log shipping** : via APM Extension (not using CloudWatch)

* * *

HI  
I’m currently using the **Elastic APM AWS Lambda extension** together with the elastic-apm-node agent, as described in the [official documentation](https://www.elastic.co/docs/reference/apm/agents/nodejs/lambda).

APM data and logs are successfully collected and visible in Kibana via our self-hosted Elastic Stack.

* * *

**What I'm trying to achieve:**  
I would like to **inject additional context fields** (e.g., user\_id, called\_path) into **every log message** emitted by my Lambda function — **specifically in documents where data\_stream.type: logs** , so they appear as indexed fields in Kibana, just like faas.id, trace.id, etc.

* * *

**What I already know:**

1. Using apm.addLabels() or apm.setCustomContext() works well for APM traces/spans.
2. However, these custom fields **do not appear** in log documents (data\_stream.type: logs).
3. I know that wrapping console.log(JSON.stringify({...})) and parsing it later via **Ingest Pipeline** works, but this requires me to **manually format every log call** , which I’d like to avoid.

* * *

**My Question**  
**Is there any recommended way or best practice to automatically enrich every log message with custom fields (like user\_id) without manually wrapping console.log()?**

I'm looking for something like:

- A middleware, hook, or logger adapter to apply globally in the Lambda handler,
- Or some integration with the APM Extension or elastic-apm-node agent that allows injecting fields into log messages,
- A Lambda-level solution to centrally inject contextual metadata into all logs sent via the Extension.

Thanks in advance for your insights!

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/9/393e70d3608531dd74e76e935a920e77b073a339.png)

---

<div class="post-metadata">

**Author:** ![Rafa\_Silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rafa_silva/32/147814_2.png) [@Rafa\_Silva](https://discuss.elastic.co/u/Rafa_Silva)\
**Post date:** [June 1, 2025, 3:30am UTC](https://discuss.elastic.co/t/aws-lambda-apm-extension-how-to-inject-custom-fields-into-logs/378761/2 "2025-06-01T03:30:33Z")

</div>

Hi!

You’re right — addLabels() and setCustomContext() only affect traces, not logs.

There’s no built-in way yet to auto-inject fields into console.log() logs via the APM agent or Extension.

Best option for now is to wrap console.log() in a custom logger that adds your context (like user\_id).

You can also use ingest pipelines to parse consistent log formats.

Would be great if native support comes in future updates!
