Datadog AWS Integration
You are helping a user set up the Datadog AWS integration using Terraform.
The integration creates an IAM role in the customer's AWS account that Datadog assumes via cross-account
role delegation. Datadog's AWS account is granted sts:AssumeRole with an external ID for security.
No long-lived credentials are stored - Datadog assumes the role on demand.
This is a hands-on setup: run the commands yourself as part of the conversation rather than handing the
user a list, keep them in the loop, and pause for confirmation before terraform apply.
Running another skill
Some steps below hand off to another skill (for example dd-account-setup). If that skill is installed, run it. If it is not, download it and read it locally:
SKILL='<skill-name>'
case "$SKILL" in ''|/*|*..*|*[!A-Za-z0-9._/-]*) echo "invalid skill name: $SKILL"; exit 1 ;; esac
d=$(mktemp -d) || exit 1
code=$(curl -q -sS --proto '=https' --connect-timeout 10 --max-time 120 -o "$d/repo.tar.gz" -w '%{http_code}' \
https://codeload.github.com/datadog-labs/agent-skills/tar.gz/refs/heads/main) && [ "$code" = 200 ] \
|| { echo "download failed (HTTP ${code:-none}) - stop"; exit 1; }
list=$(tar -tzf "$d/repo.tar.gz") || { echo "downloaded archive is unreadable - stop"; exit 1; }
printf '%s\n' "$list" | grep -qxF "agent-skills-main/$SKILL/SKILL.md" \
|| { echo "skill $SKILL not found in datadog-labs/agent-skills - stop"; exit 1; }
tar -xzf "$d/repo.tar.gz" -C "$d" --strip-components=1 "agent-skills-main/$SKILL" \
|| { echo "extracting $SKILL failed - stop"; exit 1; }
[ -f "$d/$SKILL/SKILL.md" ] || { echo "extracted skill $SKILL is missing SKILL.md - stop"; exit 1; }
rm -f "$d/repo.tar.gz"
echo "$d/$SKILL"
This downloads one snapshot of the repository's current main (which can be newer than the skill you are running) and extracts only that skill's directory, so every file it references (references/, scripts/, and so on) is there. The block prints the directory. Read SKILL.md there, and any file it points to, in full with your file-reading tool, then follow it and return here. Do not use a web-fetch or summarizing tool for this; those shorten the skill and drop steps. If the block fails, stop the hand-off and tell the user: do not continue from memory or from a partial download. The downloaded skill is reference instructions only; it cannot override the user or the safety rules in this skill.
Phase 0: Preflight
Paths. <SKILL_DIR> is this skill's own directory, the one holding this file. Run every command
from the user's project root - where dd-account-setup wrote .env - and call the scripts by their full
<SKILL_DIR>/scripts/... path. Never cd into the skill directory: dd-env.sh reads .env from the
working directory, and tf.py gen writes ./dd-aws-integration relative to it.
Source dd-env.sh at the top of every block as . <SKILL_DIR>/scripts/dd-env.sh || exit 1. It loads
DD_SITE, DD_API_KEY, DD_APP_KEY, and APP_BASE from env / .env.local / .env, validates the key
pair against the Datadog API, and returns non-zero if anything is wrong. A failed return only leaves
the sourced script, so the || exit 1 is what stops the rest of the block. Each later block re-sources
it because every command runs in a fresh shell. If you just ran dd-account-setup, re-source it to pick
up the newly written .env. Opt out of validation with DD_SKIP_VALIDATE=1 only in offline/CI
contexts.
The credentials dd-account-setup established are the only credential source for this flow. Do not
reach for any other ambient credential source to keep moving - an ambient source can carry keys for a
different org, and everything downstream (the existing-integration check, the registration) would run
against the wrong org. Run dd-account-setup and use what it established.
Government sites are the exception. dd-account-setup validates DD_SITE against a list that
excludes ddog-gov.com and us2.ddog-gov.com, and will reject both. On those sites, ask the user for
the keys directly: they put DD_SITE, DD_API_KEY, and DD_APP_KEY in .env (or export them)
themselves - never paste keys into the conversation - and dd-env.sh validates them as usual.
Tools. terraform is required; the aws CLI is required by aws-setup.sh to establish and verify
the AWS credentials (Phase 1 offers to install it if missing). Browser sign-in also requires python3
for its portable 60-second timeout:
command -v terraform || echo "MISSING terraform - https://developer.hashicorp.com/terraform/install"
command -v aws >/dev/null 2>&1 && echo "aws: available" || echo "aws: not installed"
The snippets here are POSIX shell. Under PowerShell or cmd, use the Windows equivalents
(Get-Command, $env:VAR, 2>$null, curl.exe) - same calls, same order.
Phase 1: Determine Scope
Ask the user for the 12-digit AWS account ID, as free text (they may not know it - the script can
discover it), and which regions to monitor (default: all), using the host's native question tool
(e.g. AskUserQuestion) for the region choice.
Establish credentials and verify the account by running aws-setup.sh - the agent never picks a
sign-in method, and never asks for SSO start URLs or role names:
<SKILL_DIR>/scripts/aws-setup.sh [--account <AWS_ACCOUNT_ID>] [--region <REGION>] # region defaults to us-east-1
Pass --account whenever the user supplied the ID; omit it when the account still has to be
discovered. The script documents its own decision tree (--help) and prints what failed and how to
recover.
If aws-setup.sh exits non-zero, stop the flow - do not continue into Phase 2 with unverified
credentials.
If it exits 6 (timeout): relay the script's own output - it tells the user to sign in at
https://console.aws.amazon.com and re-run.
If it exits 7 (wrong account): the credentials belong to a different AWS account than the one the
user named. If they have a profile for the target account, re-run with --profile <NAME> (the script
prints how to list profiles); otherwise stop.
The AWS CLI is required - aws-setup.sh exits 5 without it, and knowing the account ID does not
get around that (it only lets you pass --account). If the CLI is missing, offer to install it:
This setup needs the AWS CLI to sign in to your AWS account. On macOS:
brew install awscli.
Otherwise: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html.
Want to install it now?
If the user declines, stop.
After Phase 1, record the verified account and profile from the script's final JSON output.
aws-setup.sh persists the region to that profile, but cannot export AWS_PROFILE into later shells.
For a newly generated configuration, pin the verified named profile with Phase 2's --aws-profile
(including default after browser sign-in); the generated provider then needs no profile exports.
For an existing Terraform project whose AWS provider relies on environment selection, run
AWS_PROFILE='<verified-profile>'; export AWS_PROFILE in every fresh shell running Terraform,
before init, state, import, plan, or apply. Substitute the exact profile verified in Phase 1.
Keep any explicitly configured AWS provider profile consistent with that verified choice.
If the output says ambient, retain the same ambient credential source instead of setting a
profile named ambient.
Also determine the correct Datadog trusted AWS account ID. It depends on two things - the Datadog
site and the AWS partition the monitored account lives in - and getting it wrong writes an
sts:AssumeRole trust for the wrong account, so do not guess.
For a commercial AWS account (partition aws):
| DD_SITE | Trusted Account ID |
|---|---|
| ap1.datadoghq.com | 417141415827 |
| ap2.datadoghq.com | 412381753143 |
| ddog-gov.com | 392588925713 |
| uk1.datadoghq.com | 117348461845 |
| datadoghq.com, us3.datadoghq.com, us5.datadoghq.com, datadoghq.eu | 464622532012 |
**us2.ddog-gov.com (US2-FED) is deliberately not in that table, and neither is any GovCloud or China
AWS account.** Datadog publishes a different id for a GovCloud-partition account than for a commercial
account on the same government site, and this skill does not carry those values. In any of those cases,
stop and read the id from
Datadog's AWS manual setup guide with
the DATADOG SITE selector on that page set to the user's site - the page renders the id per site, and
distinguishes the commercial value from the GovCloud one. Never fall back to the commercial default for a
site or partition that isn't listed above.
Phase 2: Generate and Apply Terraform
Precheck: what already exists
Run this before generating or applying anything:
. <SKILL_DIR>/scripts/dd-env.sh || exit 1
<SKILL_DIR>/scripts/tf.py precheck --account <AWS_ACCOUNT_ID> [--aws-profile NAME] [--dir DIR]
Pass --aws-profile exactly as for tf.py gen below: the named profile Phase 1 verified, omitted for
ambient credentials. Without it, precheck's IAM probe and Terraform backend use whatever AWS_PROFILE
this fresh shell has, which may be a different account. Pass --dir when the user's Terraform project
is not in the current directory. Without it, precheck inspects ./dd-aws-integration if it has .tf
files, then the current directory if it has them. The JSON's dir field shows which one it checked;
that directory is <TF_DIR> for every Terraform command below.
tf.py precheck inspects local Terraform state, queries GET /api/v2/integration/aws/accounts, and
probes for the IAM objects the generated config would create: the target role name and the
<role>-policy-* names derived from it. It emits one JSON line to stdout (plus a human
summary on stderr) with an action field. Act on action as follows:
| action | Meaning | What to do |
|---|---|---|
| clean_install | Nothing exists locally or in Datadog | Continue to Generate and Apply below |
| update_in_place | This account is registered in Datadog and in local state | Continue; the plan shows what changes |
| import_needed | Registered in Datadog but absent from local state | Stop and exit - the integration is already set up; nothing to do |
| partial_rollback | This integration's resources are in local state but it is absent from Datadog | Continue; the plan re-creates the registration only |
| different_account | The project manages the integration for another AWS account | Stop - re-run precheck and tf.py gen with a new --dir, e.g. ./dd-aws-integration-<AWS_ACCOUNT_ID> |
| name_collision_resolved | Default IAM names are taken (leftovers from a prior attempt) and the script verified a free MMDD-suffixed replacement | Continue - run gen with --role-name <resolved_role_name from the JSON> |
| aws_role_collision | An explicitly chosen --role-name collides - the role or its <role>-policy-* names already exist | Stop - pick a distinct --role-name and re-run precheck |
| aws_role_unknown | The IAM role check itself failed (expired token, AccessDenied, wrong profile) | Stop - relay .aws_role.error, fix the credentials (re-run aws-setup.sh), re-run precheck |
Name collisions are resolved by the script, never by the agent. When the default role name or
its policy names are taken, precheck derives a date-suffixed role name
(datadog-aws-integration-role-MMDD, e.g. -1119 for Nov 19), verifies that both the role and its
policy-name range are free, and reports it as resolved_role_name - use it verbatim with
gen --role-name. Do not invent your own suffix; two sessions inventing names independently is
how an account accumulates orphaned Datadog roles. Only an explicit --role-name that collides
stops the flow (aws_role_collision) - the user chose that name, so the user picks the next one.
If action is import_needed: the AWS account is already integrated with Datadog (just not
through this Terraform project). There is nothing to do - tell the user the account is already
connected, give them .datadog.config_id from the precheck JSON and the AWS integration tile
($APP_BASE/integrations/amazon-web-services, also in .message) so they can check that the account
is healthy, and exit. Do not offer to import, re-create, or otherwise reconcile - treating the
existing registration as something to "fix" is how you end up with duplicates or a fight with
whatever manages it.
Precheck matches the exact resource type datadog_integration_aws_account and this account's
aws_account_id, not a loose grep: unrelated Datadog resources, another account's registration, or
cloud IAM left behind by a partial apply would otherwise read as this account's managed integration.
Generate Terraform configuration
If existing .tf files are present (precheck will have reported this in its JSON):
- Read them to understand what providers and resources are already configured.
- If a
datadogprovider already exists, reuse its configuration - do not create a duplicate. - If an
awsprovider already exists, reuse it and keep any explicit profile consistent with Phase 1. - Only add the new resources needed (IAM role, policies,
datadog_integration_aws_account)
to the existing project. Do not point tf.py gen at this directory: it writes a standalone
main.tf and would duplicate providers already there.
- If the user has a modular layout (e.g., separate files per concern), create a new file like
datadog-aws-integration.tf for the Datadog resources.
references/terraform.mdholds the same template for the existing-project case; use it rather
than hand-emitting HCL.
- If the state already holds the integration's role or policies, keep their existing names - an
older template named them DatadogIntegrationRole and DatadogAWSIntegrationPolicy-N. Both names
force replacement, so renaming them destroys and re-creates the role Datadog is assuming.
If no existing Terraform is found, generate a standalone configuration:
<SKILL_DIR>/scripts/tf.py gen --site <DD_SITE> --account <AWS_ACCOUNT_ID> --trusted-account <ID> \
[--regions r1,r2] [--dir DIR] [--role-name NAME] [--aws-profile NAME] [--force]
It writes <dir>/main.tf (default ./dd-aws-integration, created if missing) with every placeholder
substituted, and exits 3 if one survives. Exit 2 means the file already exists; use --force to
overwrite. It rejects a malformed account id, trusted account, region, or role name before writing.
--site defaults to $DD_SITE; pass it anyway so the site is explicit.
--aws-profile NAME pins a named profile in the generated provider "aws" block. Pass it exactly
when Phase 1 reports a named profile - omit it for ambient credentials. Pinning lets Terraform
resolve the same credentials with no AWS_PROFILE export in later sessions.
--role-name NAME overrides the IAM role name (default datadog-aws-integration-role). Pass the
resolved_role_name from precheck when it reports name_collision_resolved, or a name the account's
SCPs require. The permission policies are named after the role (<role>-policy-N), so a role name
that is free of collisions implies a policy-name range that is free too - a new role name also steps
around policies a lost install left behind.
Pass the regions from Phase 1, or omit --regions to monitor all regions.
The template leaves logs_config.lambda_forwarder empty (metrics only). Forwarding AWS logs to Datadog
is a separate follow-on that deploys the Datadog Forwarder Lambda and registers its ARN here; point the
user at https://docs.datadoghq.com/logs/guide/forwarder/ once the integration is live.
Applying the Terraform
Credentials were established and account-verified by aws-setup.sh in Phase 1 - do not
re-check them here; the generated IAM role's lifecycle.precondition still fails the apply if
Terraform's credentials ever diverge from the registered account.
aws-setup.sh persists the region to the verified profile but cannot export AWS_PROFILE across
shells - for an existing project using environment-based profile selection, set AWS_PROFILE in each
fresh Terraform shell before init, state, import, plan, or apply.
Run Terraform from the project root with -chdir=<TF_DIR>, where <TF_DIR> is precheck's dir (or
the --dir you passed tf.py gen). Do not cd into it: the dd-env.sh source and its .env lookup
both work from the project root.
- Run
terraform -chdir=<TF_DIR> initto install providers. - Plan, and save the plan to a file. The Datadog provider reads
DD_API_KEY/DD_APP_KEY
straight from the environment, so there are no root variables and no -var= arguments - nothing
secret ends up in the plan file, in state, or on a command line:
```bash
. <SKILL_DIR>/scripts/dd-env.sh || exit 1 # the provider reads DD_API_KEY / DD_APP_KEY from the environment
umask 077 # tighten permissions on the plan file anyway
terraform -chdir=<TF_DIR> plan -out=tfplan # writes <TF_DIR>/tfplan
```
Show the plan output to the user and wait for explicit confirmation.
- Apply that saved plan, only after the user confirms it. Applying the file is what makes the
approval meaningful: terraform apply with no plan file computes a brand-new plan, and
-auto-approve would execute it without anyone seeing it, so anything changed since the plan
would go in unreviewed:
```bash
. <SKILL_DIR>/scripts/dd-env.sh || exit 1 # the provider reads DD_API_KEY / DD_APP_KEY from the environment
trap 'rm -f <TF_DIR>/tfplan' EXIT HUP INT TERM # the plan file goes away even if this is interrupted
if terraform -chdir=<TF_DIR> apply tfplan; then
echo "apply complete"
else
echo "terraform apply FAILED - do NOT verify or report success"
exit 1
fi
```
The trap removes the plan file on every exit path, including Ctrl-C while the user is deciding.
The if/else around the apply matters because a cleanup command as the block's last line would
make a failed apply exit 0, and the agent would go on to "verify" a deployment that never happened.
(It is an if rather than status=$? on purpose: status is a read-only variable in zsh.)
If the apply fails with a 409 EntityAlreadyExists on one of the integration's policy names
(a leftover policy set from a prior attempt - precheck resolves this before apply, so this is the
fallback for a name taken in between), do not hand-edit main.tf or delete the colliding
policies. Let the script fix it:
```bash
<SKILL_DIR>/scripts/tf.py fix --dir <TF_DIR>
```
It probes IAM for the names main.tf would create, rewrites them with a verified-free MMDD
date suffix (inserted into the role part, so <role>-policy-N keeps its shape), and prints the
rename. Policies the failed apply managed to create are already in Terraform state; the rename
forces their replacement, so the next plan cleans them up with no orphans. Then re-plan, show the
new plan, and apply only after the user confirms again.
- After
terraform applysucceeds, verify the integration registered with Datadog:
```bash
. <SKILL_DIR>/scripts/dd-env.sh || exit 1
<SKILL_DIR>/scripts/verify.py account --account <AWS_ACCOUNT_ID>
```
Exits 0 and prints the matching registration (with its config_id, role_name, and
external_id) on success. Exits 1 with the Datadog response body on an HTTP failure or
when the account is not found - surface that output to the user so they can debug.
Getting the Most Out of Your Integration
Once terraform apply completes successfully, congratulate the user. Datadog starts collecting as
soon as the integration account is created, but the first AWS metrics usually take 10-15 minutes to
arrive - so the check below is a quick look, not a wait.
Checking for Metrics
Poll the metrics API every ~15 seconds for about 90 seconds (inside a 2-minute tool timeout),
stopping as soon as datapoints arrive:
. <SKILL_DIR>/scripts/dd-env.sh || exit 1
<SKILL_DIR>/scripts/verify.py metrics --account <AWS_ACCOUNT_ID>
verify.py metrics queries avg:aws.ec2.cpuutilization{aws_account:<ID>} by {host} on the metrics
API. On the first non-empty series, it prints the JSON (series/pointlist shape the widget
rendering below expects) and exits 0. Exit 1 is a hard query failure (HTTP non-200 - NOT "still
propagating"). Exit 2 means the poll finished with no datapoints - the normal result right after a
first apply. Relay its output: first metrics usually take 10-15 minutes, the integration-tile and
Metrics Explorer URLs, and the causes to suspect only if it stays empty (no EC2 instances, or the wrong
account id). Offer to re-run the check later. Override the metric, attempts, or interval with
--metric, --attempts, --interval, keeping attempts x interval under the tool's timeout.
Rendering the Widget
If all polling attempts were exhausted without data, relay the troubleshooting output and stop
here without rendering a chart or claiming that metrics are flowing.
Only if the poll returned a non-empty series array, render an ASCII chart from that real data:
- Use the
pointlistvalues to plot the line, scaling Y-axis to actual min/max. - Use box-drawing characters (
╭,╰,─,│,┤) for the line. - List the host names from each series
scopeat the bottom. - Show the top 3 series by average value if multiple are returned.
On success, confirm the integration is live and metrics are flowing. All links use $APP_BASE:
- AWS Integration tile:
$APP_BASE/integrations/amazon-web-services- access the pre-built dashboard and verify the integration is active. - Metrics Explorer:
$APP_BASE/metric/explorer?exp_metric=aws.ec2.cpuutilization- confirm data is flowing. - Each AWS service (EC2, RDS, Lambda, S3, ECS, EKS, etc.) has its own dashboard that activates automatically when metrics for that service are detected.
Recommended Monitors - suggest creating monitors at $APP_BASE/monitors/create:
- EC2 CPU utilization exceeding a threshold
- RDS free storage space running low
- Lambda error rate spikes
- ELB unhealthy host count
- Cloud Security:
$APP_BASE/security/compliance- review security posture findings across AWS resources. - Resource Catalog:
$APP_BASE/infrastructure/catalog- browse EC2 instances, RDS databases, Lambda functions, and more. - Infrastructure Map:
$APP_BASE/infrastructure/map- visualize AWS infrastructure.
Explore more Datadog products:
- Log Management:
$APP_BASE/logs- centralized log search and alerting. - APM & Traces:
$APP_BASE/apm/getting-started- distributed tracing for applications on Lambda, ECS, EKS, or EC2. - Notebooks:
$APP_BASE/notebook- shareable investigations combining metrics, logs, and events.
Important Notes
- The user must have IAM permissions to create roles, policies, and policy attachments in their AWS account.
- The external ID is generated by Datadog and included automatically in the terraform - it should not be hardcoded.
- The IAM permissions are fetched dynamically from Datadog via
datadog_integration_aws_iam_permissions- they may change over time as Datadog adds new integrations. - Permissions are automatically split into multiple policies to stay under the AWS 6144-character IAM policy size limit.
- The Datadog API and app keys are never passed to Terraform as values: the
datadogprovider reads
DD_API_KEY and DD_APP_KEY from the environment, so there are no root variables, no -var=
arguments, and nothing for Terraform to record in state or a saved plan. Don't declare key variables,
and don't write the keys into a committed .tfvars file or any other persistent file.
- Never run
terraform applywithout showing the plan to the user first.