---
isPrivate: true
title: How Datadog Instrumentation through the AWS Integration Works
description: >-
  Understand how Datadog instruments Amazon EC2 instances and AWS Lambda
  functions through the AWS integration: the AWS resources created, the
  instrumentation mechanism, the security model, and how Datadog keeps
  instrumentation in place.
breadcrumbs: >-
  Docs > Integrations > Integration Guides > How Datadog Instrumentation through
  the AWS Integration Works
---

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

# How Datadog Instrumentation through the AWS Integration Works

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

{% alert level="info" %}
To find out if this integration is available in your organization, see your [Datadog Integrations](https://app.datadoghq.com/integrations) page or ask your organization administrator.

To initiate an exception request to enable this integration for your organization, email [support@ddog-gov.com](mailto:support@ddog-gov.com).
{% /alert %}

{% /callout %}

This page explains how Datadog instruments and maintains your AWS workloads through the AWS integration. For setup instructions and the permissions Datadog requires, see [Install Datadog instrumentation through the AWS Integration](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md).

This page covers Amazon EC2 instances and AWS Lambda functions. Amazon EKS is not supported.

Datadog also offers [remote instrumentation](https://docs.datadoghq.com/serverless/aws_lambda/remote_instrumentation.md) for Lambda functions. Remote instrumentation deploys an instrumenter function into your own account rather than making the changes from Datadog. For a comparison of the two, see [Choose between the AWS integration and remote instrumentation](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md#choose-between-the-aws-integration-and-remote-instrumentation) in the setup guide.

## AWS resources that Datadog creates{% #aws-resources-that-datadog-creates %}

### Created once, by the CloudFormation stack{% #created-once-by-the-cloudformation-stack %}

The CloudFormation template you launch creates the following resources one time, in a single stack:

| Resource                    | Name                                               | Purpose                                                                                                |
| --------------------------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| EventBridge connection      | `datadog-agent-resource-update-intake-connection`  | Holds your Datadog API and application keys so events can be sent to Datadog                           |
| EventBridge API destination | `datadog-agent-resource-update-intake-destination` | Sends resource change events to Datadog                                                                |
| EventBridge rule            | `datadog-agent-resource-update-rule-ec2`           | Notifies Datadog when a covered instance changes. Created when you select the EC2 workload             |
| EventBridge rule            | `datadog-agent-resource-update-rule-lambda`        | Notifies Datadog when a covered function changes. Created when you select the Lambda workload          |
| IAM role                    | auto-named                                         | Lets EventBridge send events to the `datadog-agent-resource-update-intake-destination` API destination |
| IAM role                    | `datadog-eventbridge-cross-region-role`            | Lets other regions forward events to your primary region                                               |

The stack also attaches the IAM permissions for the workloads you selected to your AWS integration role. If you select only the Lambda workload, the stack grants no EC2 permissions.

### Created as needed, for EC2 instances{% #created-as-needed-for-ec2-instances %}

| Resource                                | Name                                                                | Purpose                                                                                                                                                                                                                                     |
| --------------------------------------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Systems Manager document                | `datadog-ec2-instrumenter`                                          | The install and uninstall script. One document per account.                                                                                                                                                                                 |
| Secrets Manager secret                  | `/datadog/ec2-instrumenter/<ACCOUNT_ID>/<INSTANCE_ID>`              | Holds the Datadog API key so the instance can fetch it itself. Encrypted with the default AWS-managed key.                                                                                                                                  |
| IAM role and instance profile           | `datadog-ssm-<INSTANCE_ID>` and `datadog-ssm-profile-<INSTANCE_ID>` | Created only when the instance has no instance profile, under the IAM path `/datadog-ec2-instrumenter/` so they are identifiable. Receives the AWS-managed `AmazonSSMManagedInstanceCore` policy so Systems Manager can reach the instance. |
| Inline IAM policy                       | `datadog-ec2-instrumenter-secrets`                                  | Added to the instance's role. Grants read access only to secrets under `/datadog/ec2-instrumenter/`.                                                                                                                                        |
| EventBridge rules in your other regions | Same names as the primary-region resources                          | Forward change events to your primary region.                                                                                                                                                                                               |

Datadog does not create S3 buckets, event buses, log groups, or SSM parameters, and does not tag your instances.

### No additional resources created for Lambda functions{% #no-additional-resources-created-for-lambda-functions %}

Apart from the Lambda EventBridge rule that the CloudFormation stack creates, Datadog creates no AWS resources for Lambda instrumentation. The only other change is to the configuration of the functions your rule covers. Datadog does not create secrets, IAM roles, or SSM documents for Lambda, and does not tag your functions.

## How instrumentation works{% #how-instrumentation-works %}

After you save an instrumentation rule, Datadog evaluates the query you defined against your account to determine the covered resources, then runs the following sequence against each one. For prerequisites, including supported platforms and runtimes, see [Prerequisites](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md#prerequisites) in the setup guide.

### On Amazon EC2{% #on-amazon-ec2 %}

1. Datadog checks that each covered instance is running, on a supported platform, and reachable by AWS Systems Manager.
1. When an instance has no IAM instance profile, Datadog creates one so Systems Manager can reach it. When an instance already has one, Datadog adds the SSM policy and the scoped secret-read policy to the existing role.
1. Datadog checks whether an Agent is already present. When an Agent is present that Datadog did not install, Datadog stops and leaves the instance alone.
1. Datadog calls `ssm:SendCommand`, one instance at a time, running the `datadog-ec2-instrumenter` document.
1. On the instance, the document fetches the API key from Secrets Manager using the instance's own IAM role. It then runs Datadog's standard Agent installer (`install_script_agent7.sh` on Linux, or the standard MSI on Windows) with log collection and APM host instrumentation enabled.

Datadog does not reboot or restart your instances. The only service Datadog touches is the Datadog Agent itself, which is started on install and stopped on uninstall. Your applications and other services are untouched.

### On AWS Lambda{% #on-aws-lambda %}

Lambda instrumentation runs entirely from Datadog. Datadog does not deploy any compute, such as an instrumenter function, into your account to instrument your functions.

1. Datadog reads the function's current configuration and tags, and checks that it meets the [Lambda prerequisites](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md#aws-lambda-functions).
1. Datadog checks whether the function is already instrumented. Datadog skips a function that carries Datadog layers, a Datadog handler, or Datadog environment variables that Datadog did not apply. Datadog also skips a function managed by [remote instrumentation](https://docs.datadoghq.com/serverless/aws_lambda/remote_instrumentation.md), and reports which of the two reasons applies. 3.Datadog uses the layer versions specified in the instrumentation rule to identify matching layers based on the function's runtime, architecture, region, and AWS partition.
1. Datadog computes the complete desired configuration and records exactly what it is about to change, before changing anything.
1. Datadog authorizes the function's execution role to send telemetry to your Datadog organization. See the How Lambda telemetry is authenticated section.
1. Datadog calls `lambda:UpdateFunctionConfiguration` once, submitting the complete layer list and environment map. Datadog marks the change as applied only after AWS reports success.

A Lambda update is a replace-style operation: the submitted layer list and environment map become the new configuration. Datadog therefore computes the full desired state rather than appending to it, which preserves your existing layers and environment variables. The update carries the function's revision ID, so a change made in your account between Datadog's read and write causes the update to fail instead of overwriting the change.

### What Datadog changes on a function{% #what-datadog-changes-on-a-function %}

| Change                                                                                                                                                              | Applies to                            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| Adds the Datadog extension layer (`Datadog-Extension` or `Datadog-Extension-ARM`)                                                                                   | All supported runtimes                |
| Adds the matching Datadog tracing layer                                                                                                                             | Node.js, Python, Ruby, Java, and .NET |
| Sets `DD_SITE` and `DD_ORG_UUID`                                                                                                                                    | All supported runtimes                |
| Sets `DD_TRACE_ENABLED` and `DD_SERVERLESS_LOGS_ENABLED` to `true` or `false` to match the instrumentation rule's APM and Logs toggles, overwriting existing values | All supported runtimes                |
| Redirects the handler to the Datadog handler and moves the original into `DD_LAMBDA_HANDLER`                                                                        | Node.js and Python                    |
| Sets `AWS_LAMBDA_EXEC_WRAPPER` to `/opt/datadog_wrapper`                                                                                                            | Java and .NET                         |

The instrumentation rule takes precedence for `DD_TRACE_ENABLED` and `DD_SERVERLESS_LOGS_ENABLED`: when either is changed on a covered function, Datadog sets it back to match the rule. On uninstall, Datadog restores each variable's original value, or removes it if the function didn't set it before instrumentation.

Datadog does not change function code, memory size, timeout, VPC configuration, concurrency, or any other function setting.

### Resources that Datadog excludes{% #resources-that-datadog-excludes %}

On EC2, Datadog excludes:

- Instances that are not running
- EKS worker nodes
- ECS container instances
- Instances that already have a non-Datadog-installed Agent

On Lambda, Datadog excludes:

- Container image functions and functions on an unsupported runtime or architecture
- Functions outside the commercial `aws` partition
- Lambda@Edge replicas and the functions they replicate
- Functions already instrumented by you or by remote instrumentation
- Functions that already set `AWS_LAMBDA_EXEC_WRAPPER` to a non-Datadog wrapper
- Functions where adding the Datadog layers would exceed the AWS five-layer limit

## Security, auditing, and change control{% #security-auditing-and-change-control %}

### How Datadog gets access{% #how-datadog-gets-access %}

Datadog uses the same cross-account IAM role as the AWS integration, authenticated with an external ID. Datadog receives short-lived, temporary credentials, and each type of work (reading EC2, managing IAM, sending commands, updating functions) uses a separately scoped credential session rather than one broad session. Datadog does not store any long-lived AWS keys.

### Auditing Datadog's actions{% #auditing-datadogs-actions %}

Every action Datadog takes is a standard AWS API call, so all actions appear in AWS CloudTrail. Everything Datadog creates is identifiable by name: resources are prefixed with `datadog-`, secrets are stored under `/datadog/ec2-instrumenter/`, and IAM roles use the immutable path `/datadog-ec2-instrumenter/`. Because an IAM path cannot be edited after creation, the path cannot be silently changed. On-instance command results appear in the Systems Manager Run Command history. Lambda configuration changes appear as `UpdateFunctionConfiguration` events attributed to your AWS integration role.

### How the API key is handled on EC2{% #how-the-api-key-is-handled-on-ec2 %}

The API key is stored in your own Secrets Manager, encrypted at rest. Only the secret's Amazon Resource Name (ARN) is passed in the SSM command; the key itself never appears in command parameters or in CloudTrail. The instance reads the secret with its own IAM role, restricted to a single path. Datadog stores only a reference to the key internally, not the key itself.

### How Lambda telemetry is authenticated{% #how-lambda-telemetry-is-authenticated %}

Lambda instrumentation stores no Datadog credential in your account. The Datadog extension authenticates with the function's AWS execution identity through [Workload Identity Federation](https://docs.datadoghq.com/account_management/workload_identity_federation.md), using the `DD_ORG_UUID` and `DD_SITE` values Datadog sets on the function. No Datadog API key, secret ARN, or KMS-encrypted key is written into the function's configuration.

For that authentication to succeed, Datadog authorizes the function's execution role to send telemetry to your Datadog organization. Datadog sets up this authorization before it updates a function and matches the execution role exactly, rather than by a broader pattern.

Because a single execution role is often shared across functions, Datadog creates these authorizations but does not remove them on uninstall. Removing the authorization for a shared role could break another function that still depends on it.

### Who can change instrumentation{% #who-can-change-instrumentation %}

- **In AWS**: Access is governed by your own IAM policies. Removing the cross-account permissions stops Datadog immediately.
- **In Datadog**: Viewing instrumentation rules requires the **Hosts Read** permission. Creating, editing, or deleting rules requires the **Agent Install** permission. Rule changes are rate-limited.

### Guardrails{% #guardrails %}

- Datadog never removes instrumentation it did not install.
- Datadog tracks which resources it instrumented, so it cleans up only its own work.
- On EC2, when some regions cannot be listed, Datadog skips cleanup rather than risk removing instrumentation in bulk.
- On Lambda, Datadog restores a function from the configuration it recorded before instrumenting it, so an uninstall reverses exactly the change Datadog made.
- A failure is scoped to the individual resource. One resource that fails does not affect resources that are already instrumented.

## How Datadog maintains instrumentation{% #how-datadog-maintains-instrumentation %}

### Continuous reconciliation{% #continuous-reconciliation %}

Datadog continuously maintains the state you define on the covered resources:

- Datadog re-checks the covered resources on a regular schedule, restoring instrumentation that goes missing, retrying anything that fails, and cleaning up resources that no longer exist.
- Change events forwarded from your account let Datadog react within minutes, rather than waiting for the next scheduled check. Datadog reacts both to a covered resource that changed and to a newly created resource that a query-based rule matches:
  - **EC2**: Events come from the CloudFormation stack's EventBridge rule.
  - **Lambda**: The `datadog-agent-resource-update-rule-lambda` rule forwards function create, configuration update, tag, and untag events.
- On EC2, instances that already have the Agent are re-verified less frequently, to avoid unnecessary activity.
- On Lambda, Datadog calls the Lambda API in your account only for functions that need a change. A fleet whose functions already match the instrumentation rule's layer versions and APM and Logs settings produces no per-function activity.

### How Lambda functions pick up new layer versions{% #how-lambda-functions-pick-up-new-layer-versions %}

Datadog compares a covered function's layers against the versions set on the instrumentation rule. When a layer on a covered function is changed by hand, Datadog sets it back to the rule's version. Datadog does not upgrade the layer versions on a rule for you. A covered function picks up a new layer version only when you edit the rule. For more information on where to set the versions, see [Install Datadog instrumentation](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md#install-datadog-instrumentation) in the setup guide.

A Lambda configuration update that is still in progress is left alone and retried shortly afterward, so Datadog does not race a change already being applied.

### How a rule determines coverage{% #how-a-rule-determines-coverage %}

A rule is not a one-time selection. Datadog re-evaluates its query over time and reacts to the change events forwarded from your account. Datadog instruments a resource as soon as it detects a match, in either of the following cases:

- **It was created after you saved the rule.** `RunInstances` and `CreateFunction` events are forwarded, so a new resource is picked up within minutes.
- **It already existed and started matching.** Tagging a resource to bring it into scope is the common case, so tag events are forwarded too: `CreateTags` and `DeleteTags` on EC2, `TagResource` and `UntagResource` on Lambda. This supports writing a rule such as `@Tags:datadog:true` first, then tagging resources into it as you go.

A rule you built by selecting specific resources holds a query naming those resources, so nothing else ever matches it.

For guidance on writing a query, including when to match a fixed tag, see [Choose how your rule matches resources](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md#choose-how-your-rule-matches-resources) in the setup guide.

### What happens when coverage changes{% #what-happens-when-coverage-changes %}

Datadog re-evaluates the rule and compares the covered resources against the previous set. Resources no longer covered have instrumentation removed. Newly covered resources are instrumented. Deleting a rule removes instrumentation from everything the rule covered.

{% alert level="warning" %}
Datadog removes instrumentation when a resource no longer matches the rule, whether the change comes from an edit in Datadog or from retagging or reconfiguring the resource in AWS. Keep this behavior in mind when you write a rule based on tags that other teams can change.
{% /alert %}

### Terminated, stopped, or deleted resources{% #terminated-stopped-or-deleted-resources %}

On EC2, Datadog detects terminated instances and cleans up the IAM resources it created for them. Datadog leaves stopped instances alone until they return. On Lambda, a deleted function drops out of coverage.

### When instrumentation fails{% #when-instrumentation-fails %}

Datadog retries automatically, with an increasing delay between attempts. Problems that need your action, such as a missing permission or a function at the layer limit, are reported and no longer retried until you resolve them. Missing-permission problems appear as an issue on the **AWS integration tile** and on the Fleet install page.

{% alert level="warning" %}
When someone removes instrumentation from a covered resource by hand, Datadog restores it. The rule is the source of truth. To stop coverage, change the rule.
{% /alert %}

## Remove Datadog instrumentation{% #remove-datadog-instrumentation %}

To remove instrumentation, remove resources from a rule, edit the rule's query, or delete the rule.

- **EC2**: Datadog removes the Datadog Agent, the `/etc/datadog-agent` and `/opt/datadog-agent` directories on Linux (or performs an MSI uninstall on Windows), and any IAM role or instance profile Datadog created for each instance.
- **Lambda**: Datadog removes the layers it added and restores the environment variables and handler the function had beforehand. Datadog first compares its record of the original configuration against the function's current configuration, so it does not remove a layer or variable it did not add. The telemetry authorization for the execution role is left in place, because the role may be shared with other functions.

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

Additional helpful documentation, links, and articles:

- [Install Datadog Instrumentation through the AWS Integration](https://docs.datadoghq.com/integrations/guide/aws-agent-installation.md)
- [AWS Integration](https://docs.datadoghq.com/integrations/amazon_web_services.md)
- [Fleet Automation](https://docs.datadoghq.com/agent/fleet_automation.md)
- [Workload Identity Federation](https://docs.datadoghq.com/account_management/workload_identity_federation.md)
