Problem
When using aws-embedded-metrics (or similar custom metric libraries) inside a durable function, metrics emitted in the main handler context are emitted multiple times due to replay. Wrapping metric emission in context.step() ensures metrics are emitted exactly once.
This is a non-obvious gotcha — the same code works correctly in a standard Lambda but produces inflated metric data in a durable function.
Current state
The monitoring docs cover CloudWatch metrics published by the service, but there is no guidance on emitting custom metrics (EMF, Powertools, PutMetricData) from within durable function code. The replay-awareness requirement is not documented.
Suggested improvement
- Add a section to the best practices or monitoring docs: "Emitting custom metrics from durable functions"
- Document the replay gotcha: always wrap metric emission in
context.step() to avoid duplicate data points
- Provide a code example showing the correct pattern, e.g.:
// ❌ Wrong — emitted on every replay
metrics.putMetric("OrderProcessed", 1);
// ✅ Correct — emitted exactly once
await context.step("emit-metric", async () => {
metrics.putMetric("OrderProcessed", 1);
});
Problem
When using
aws-embedded-metrics(or similar custom metric libraries) inside a durable function, metrics emitted in the main handler context are emitted multiple times due to replay. Wrapping metric emission incontext.step()ensures metrics are emitted exactly once.This is a non-obvious gotcha — the same code works correctly in a standard Lambda but produces inflated metric data in a durable function.
Current state
The monitoring docs cover CloudWatch metrics published by the service, but there is no guidance on emitting custom metrics (EMF, Powertools, PutMetricData) from within durable function code. The replay-awareness requirement is not documented.
Suggested improvement
context.step()to avoid duplicate data points