Adding Monitoring and Secrets Caching with Extensions
This lesson explains how extensions add capabilities to a function without changing its code. Extensions run alongside the function inside its execution environment and provide features such as monitoring, secrets caching and configuration retrieval.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how internal and external extensions run within the execution environment.
- Identify the AWS extension that suits a monitoring, secrets or configuration requirement.
- Describe how extensions affect initialisation time, duration and cost.
What extensions are
An extension is code that runs in the same execution environment as the function and is delivered as a layer. Extensions start during the Init phase, before the handler, and can register for lifecycle events through the Extensions API, so they know when each invocation starts and when the environment is about to shut down. Their memory and CPU use counts towards the function's configuration, and slow extension start-up adds to the cold start.
There are two kinds. An internal extension runs inside the runtime process, for example as a Java agent or through a wrapper script that changes how the runtime starts; it is used to instrument the runtime itself. An external extension runs as a separate process, can be written in any language and can continue running after the handler returns. This lets it send telemetry or refresh a cache without delaying the response, although the invocation is not complete until all extensions have finished.
The following table summarises the two kinds.
| Kind | How it runs | Typical use |
|---|---|---|
| Internal extension | Inside the runtime process, for example through a language wrapper script or an agent loaded by the runtime | Instrumenting the runtime, such as an APM agent |
| External extension | As a separate process that can be written in any language and can keep running after the handler returns | Collecting and sending telemetry, caching parameters and secrets |
Common AWS extensions
The extensions most relevant to developers remove repeated API calls from function code. The Parameters and Secrets Lambda extension retrieves values from Parameter Store and Secrets Manager and caches them in the execution environment. The function reads a value with an HTTP request to localhost:2773, and the extension calls the service only when the cached value is older than its time to live. This reduces latency, cost and the risk of throttling, and picks up rotated secrets after the cache expires.
The AWS AppConfig Lambda extension works in the same way for configuration and feature flags on port 2772, polling AppConfig in the background. CloudWatch Lambda Insights collects system-level metrics, such as CPU time, memory and network use, that the standard Lambda metrics do not include. Monitoring vendors provide their own extensions that forward logs, metrics and traces to their platforms.
The following table summarises these extensions.
| Extension | What it does |
|---|---|
| Parameters and Secrets Lambda extension | Caches Parameter Store and Secrets Manager values behind a local HTTP endpoint on port 2773, refreshing them on a TTL |
| AWS AppConfig Lambda extension | Polls AppConfig in the background and serves configuration and feature flags from a local endpoint on port 2772 |
| CloudWatch Lambda Insights | Collects system-level metrics such as CPU, memory and network usage |
| Partner extensions | Send logs, metrics and traces to monitoring tools such as Datadog or New Relic |
Effect on performance and cost
Lambda extensions run alongside the function code and therefore affect its performance and cost. The following list describes these effects.
- Extensions initialise during the Init phase, so a heavy extension lengthens cold starts.
- External extensions share the function's memory and CPU, so memory may need to rise.
- Time an extension spends after the handler returns, such as flushing telemetry, is billed as part of the invocation.
- Extensions receive lifecycle events through the Extensions API, and can subscribe to logs and platform events through the Telemetry API.
Caching a secret without changing the code structure
A function reads a third-party API key from Secrets Manager on every invocation. At 300 requests per second this adds latency and Secrets Manager charges, and occasionally throttling.
| Option | Result |
|---|---|
| Call Secrets Manager in the handler | One API call per invocation: slow and costly |
| Read the secret once outside the handler | Fast, but a rotated secret is not picked up until the environment is recycled |
| Parameters and Secrets Lambda extension | Local cached reads with a TTL, so rotations are picked up within minutes |
The developer adds the extension's layer, sets SECRETS_MANAGER_TTL to 300 seconds and reads the secret from http://localhost:2773 with the session token header. Secrets Manager calls fall from hundreds per second to a few per environment per hour.
How does the AWS Parameters and Secrets Lambda Extension reduce latency and cost when a function reads a secret?
Show answer
It runs alongside the function and caches the secret in the execution environment, so most invocations read it from a local HTTP endpoint instead of calling Secrets Manager every time.
Exam Tips
Remember for the exam
- To cache secrets without custom code, use the Parameters and Secrets Lambda extension.
- To read AppConfig feature flags efficiently from Lambda, use the AppConfig Lambda extension.
- Extensions are added to a function as layers and run in the same execution environment.
- A slow extension increases cold-start time and can increase billed duration.