---
title: Monitoring AWS Lambda MicroVMs
description: Set up Datadog Serverless Monitoring for AWS Lambda MicroVMs.
breadcrumbs: >-
  Docs > Serverless > Serverless Monitoring for AWS Lambda > Monitoring AWS
  Lambda MicroVMs
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Monitoring AWS Lambda MicroVMs

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

{% callout %}
# Important note for users on the following Datadog sites: app.datadoghq.com, us3.datadoghq.com, us5.datadoghq.com, app.datadoghq.eu, ap1.datadoghq.com, ap2.datadoghq.com, uk1.datadoghq.com

{% callout %}
##### Join the Preview!

Datadog Serverless Monitoring for AWS Lambda MicroVMs is in Preview. To sign up, contact your Datadog representative.
{% /callout %}

{% /callout %}

Datadog collects logs, traces, and metrics, including CPU utilization, from your [AWS Lambda MicroVMs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-microvms-guide.html) throughout their lifecycle. MicroVMs run applications in isolated [Firecracker](https://firecracker-microvm.github.io/) virtual machines. Each instance starts from an image snapshot and can suspend when idle, then resume with its memory and disk state intact.

## Prerequisites{% #prerequisites %}

To use Datadog Serverless Monitoring for AWS Lambda MicroVMs, you must have the following:

- A Datadog API key and network egress to Datadog
- An application that follows the AWS MicroVM service model

Optionally, to collect traces, install a Datadog SDK that meets the following minimum version. Without an SDK, `serverless-init` still collects logs and metrics.

| Component                | Minimum version |
| ------------------------ | --------------- |
| Node.js SDK (`dd-trace`) | 6.15.0          |
| Python SDK (`ddtrace`)   | 4.17.0          |

If your application uses OpenSSL, use a snapshot-compatible version. AWS provides one in the `public.ecr.aws/lambda/microvms:al2023-minimal` container base image. You can also use your own base image with the patched OpenSSL library. See [AWS guidance on snapshots](https://docs.aws.amazon.com/lambda/latest/dg/microvms-images-snapshots.html) for more information.

## Setup{% #setup %}

### Instrument the image{% #instrument-the-image %}

1. Add `serverless-init` v1.11.4 or later to your MicroVM image. Set it as the container entrypoint, with your application as the command. Sidecar mode is not supported. The image is available on [Docker Hub](https://hub.docker.com/r/datadog/serverless-init) and the [Amazon ECR Public Gallery](https://gallery.ecr.aws/datadog/serverless-init).

The following examples include optional tracing and use an application on port `8080` and lifecycle hooks on port `9000`. Replace the application filenames and startup commands as needed.

   {% tab title="Node.js" %}
Add `dd-trace` v6.15.0 or later to `package.json` and update `package-lock.json`, such as with `npm install dd-trace@6.15.0`. Install Node.js from the Amazon Linux packages to use snapshot-compatible OpenSSL.

   ```dockerfile
   FROM public.ecr.aws/lambda/microvms:al2023-minimal
   WORKDIR /app
   RUN dnf install -y nodejs22 nodejs22-npm && dnf clean all
   COPY package.json package-lock.json ./
   RUN npm ci --omit=dev
   COPY . .
   COPY --from=datadog/serverless-init:1.11.4 /datadog-init /serverless-init
   ENV DD_AWS_MICROVM_USER_APP_PORT=8080 \
       DD_AWS_MICROVM_ENABLE_RUN=true
   EXPOSE 8080 9000
   ENTRYPOINT ["/serverless-init"]
   CMD ["node", "--require", "dd-trace/init", "app.js"]
   ```

The `--require dd-trace/init` option initializes the SDK before loading application code. If `app.js` already calls `require('dd-trace').init()` before importing other libraries, use `CMD ["node", "app.js"]` instead.

For more information, see [Tracing Node.js Applications](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/nodejs.md).
   {% /tab %}

   {% tab title="Python" %}
Add `ddtrace` v4.17.0 or later to `requirements.txt`. Install the dependencies, then start the application with `ddtrace-run`:

   ```dockerfile
   FROM public.ecr.aws/lambda/microvms:al2023-minimal
   WORKDIR /app
   RUN dnf install -y python3.12 python3.12-pip && dnf clean all
   COPY requirements.txt ./
   RUN python3.12 -m pip install --no-cache-dir -r requirements.txt
   COPY . .
   COPY --from=datadog/serverless-init:1.11.4 /datadog-init /serverless-init
   ENV PYTHONUNBUFFERED=1 \
       DD_AWS_MICROVM_USER_APP_PORT=8080 \
       DD_AWS_MICROVM_ENABLE_RUN=true
   EXPOSE 8080 9000
   ENTRYPOINT ["/serverless-init"]
   CMD ["ddtrace-run", "python3.12", "app.py"]
   ```

`ddtrace-run` initializes tracing before the application starts. Python tracing supports WSGI and ASGI applications, including Flask, Django, FastAPI, and Starlette.

For more information, see [Tracing Python Applications](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/python.md).
   {% /tab %}

For tracing, forward the `/run` hook to your application, as shown in the examples. MicroVM instances start from the same image snapshot, so trace and span IDs can collide across instances. Forwarding `/run` allows the tracer to generate new IDs for each instance.
Important alert (level: info): To collect logs and metrics without tracing, omit the SDK dependency and tracer initialization. Use `CMD ["node", "app.js"]` for Node.js or `CMD ["python3.12", "app.py"]` for Python. You can also omit `DD_AWS_MICROVM_USER_APP_PORT` and `DD_AWS_MICROVM_ENABLE_RUN` unless your application needs forwarded lifecycle hooks.
1. Set the following environment variables:

   ```text
   DD_API_KEY=<DATADOG_API_KEY>
   DD_SITE=<DATADOG_SITE>
   DD_SERVICE=<SERVICE_NAME>
   DD_ENV=<ENVIRONMENT>
   DD_VERSION=<VERSION>
   ```

Set `DD_SITE` to your [Datadog site](https://docs.datadoghq.com/getting_started/site.md): <YOUR_DATADOG_SITE>.

1. Enable the AWS Lambda MicroVM lifecycle hooks in the image configuration.

`serverless-init` starts and supervises your application. It handles the `/ready` and `/validate` hooks during image setup, and the `/run`, `/resume`, `/suspend`, and `/terminate` hooks at runtime.

### Enable logs{% #enable-logs %}

Set `DD_LOGS_ENABLED=true` to collect logs. To correlate logs with traces, enable log injection in your tracer and configure the logs pipeline to parse trace and span IDs. With log injection enabled, JSON logs include active trace and span IDs. Supported Node.js loggers include Bunyan, Pino, and Winston. For Python, use `structlog`.

See [correlating logs and traces](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces.md) for [Node.js](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/nodejs.md) and [Python](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/python.md).

### (Optional) Forward additional lifecycle hooks to your application{% #optional-forward-additional-lifecycle-hooks-to-your-application %}

The Dockerfile examples in the Instrument the image section already forward the `/run` hook for tracing.

By default, `serverless-init` handles lifecycle hooks on port `9000` and does not forward them to your application. To forward a hook, set `DD_AWS_MICROVM_USER_APP_PORT` to the port your application listens on at `127.0.0.1`, then set the matching `DD_AWS_MICROVM_ENABLE_<HOOK>` variable to `true`. The app and lifecycle ports must differ. You need both settings. Either one alone leaves forwarding disabled.

| Environment variable                 | Description                                                                                   | Default                 |
| ------------------------------------ | --------------------------------------------------------------------------------------------- | ----------------------- |
| `DD_AWS_MICROVM_LIFECYCLE_PORT`      | Port `serverless-init` listens on for lifecycle hooks.                                        | `9000`                  |
| `DD_AWS_MICROVM_USER_APP_PORT`       | Port your application listens on at `127.0.0.1`. Required to forward hooks.                   | Not set (no forwarding) |
| `DD_AWS_MICROVM_ENABLE_READY`        | Forward `/ready` to your application.                                                         | `false`                 |
| `DD_AWS_MICROVM_ENABLE_VALIDATE`     | Forward `/validate` to your application.                                                      | `false`                 |
| `DD_AWS_MICROVM_ENABLE_RUN`          | Forward `/run` to your application.                                                           | `false`                 |
| `DD_AWS_MICROVM_ENABLE_RESUME`       | Forward `/resume` to your application.                                                        | `false`                 |
| `DD_AWS_MICROVM_ENABLE_SUSPEND`      | Forward `/suspend` to your application.                                                       | `false`                 |
| `DD_AWS_MICROVM_ENABLE_TERMINATE`    | Forward `/terminate` to your application.                                                     | `false`                 |
| `DD_AWS_MICROVM_READY_TIMEOUT_MS`    | Timeout for a forwarded `/ready` hook, in milliseconds.                                       | `30000` (30 s)          |
| `DD_AWS_MICROVM_VALIDATE_TIMEOUT_MS` | Timeout for a forwarded `/validate` hook, in milliseconds.                                    | `30000` (30 s)          |
| `DD_AWS_MICROVM_FORWARD_TIMEOUT_MS`  | Timeout for forwarded `/run`, `/resume`, `/suspend`, and `/terminate` hooks, in milliseconds. | `1000` (1 s)            |

Timeouts apply only to forwarded hooks. You don't need to forward hooks to collect logs and metrics.

## Collected data{% #collected-data %}

### Metrics{% #metrics %}

Datadog collects the following CPU utilization and lifecycle metrics for each MicroVM without additional setup:

| Metric                                         | Description                                                                                                       |
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `aws.lambda.microvm.enhanced.cpu.usage`        | CPU usage for the MicroVM, in nanocores.                                                                          |
| `aws.lambda.microvm.enhanced.cpu.limit`        | Number of CPUs available to the guest, in nanocores.                                                              |
| `aws.lambda.microvm.enhanced.validate`         | Number of validation hook events.                                                                                 |
| `aws.lambda.microvm.enhanced.run`              | Number of run hook events.                                                                                        |
| `aws.lambda.microvm.enhanced.resume`           | Number of resume hook events.                                                                                     |
| `aws.lambda.microvm.enhanced.suspend`          | Number of suspend hook events.                                                                                    |
| `aws.lambda.microvm.enhanced.terminate`        | Number of termination hook events.                                                                                |
| `aws.lambda.microvm.enhanced.active_instances` | Periodic heartbeat while a MicroVM is running or resumed. This is a periodic sample, not an exact instance count. |

#### CPU metrics{% #cpu-metrics %}

`serverless-init` samples CPU metrics every 3 seconds, starting when the MicroVM starts. CPU metrics are enabled by default. To disable them, set `DD_ENHANCED_METRICS_ENABLED=false`. Lifecycle and heartbeat metrics are still reported.

Both CPU metrics report raw values in nanocores: 1 CPU core equals 1,000,000,000 nanocores. The provided dashboard converts them to cores, the unit AWS uses for CPU capacity, by dividing the values by `1e9`. The metrics use the same units as [`container.cpu.usage` and `container.cpu.limit`](https://docs.datadoghq.com/integrations/container.md).

CPU metrics are available with or without cgroups. When cgroups are unavailable, `cpu.limit` reflects the CPU count visible to the MicroVM, which can differ from its AWS CPU capacity.

### Tags{% #tags %}

Logs and traces include the MicroVM image ARN as `resource_id`, the image name as `image_name`, and the image version as `lambda_microvm_image_version`. CPU and lifecycle metrics include the `resource_id` and `image_name` tags. The instance heartbeat also includes the image ARN as `microvm_image_arn`.

After AWS sends the `/run` hook, logs, traces, lifecycle metrics, and the instance heartbeat also include the `lambda_microvm_id` instance ID tag. CPU metrics use image tags, not instance tags. When configured, CPU metrics, lifecycle metrics, and the instance heartbeat include the unified service tags `env`, `service`, and `version`.

### Data flush on suspend and terminate{% #data-flush-on-suspend-and-terminate %}

When a MicroVM suspends or terminates, `serverless-init` sends pending metrics, logs, and traces to Datadog.

## Further reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [AWS Lambda MicroVMs overview](https://docs.aws.amazon.com/lambda/latest/dg/lambda-microvms-guide.html)
- [How AWS Lambda MicroVMs work](https://docs.aws.amazon.com/lambda/latest/dg/microvms-how-it-works.html)
- [AWS Lambda MicroVM images and snapshots](https://docs.aws.amazon.com/lambda/latest/dg/microvms-images-snapshots.html)
- [Tracing Node.js applications](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/nodejs.md)
- [Tracing Python applications](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/python.md)
- [Correlate logs and traces](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces.md)
- [Unified service tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging.md)
- [Instrumenting Google Cloud Run containers in-container](https://docs.datadoghq.com/serverless/google_cloud_run/containers/in_container.md)
