Datadog Agent Setup for Python App on EC2
Purpose: Install and configure the Datadog Agent to collect host metrics and APM traces from a Python FastAPI application. This setup works for autoscaling environments and ensures host metrics and traces are reported automatically.
1. Prerequisites
- EC2 instance with Python 3.8+ and a virtual environment (
venv) installed. - Datadog account with API key.
- Security group allowing outbound HTTPS (port 443) for agent reporting.
- Python app installed at
/home/<user>/<app-name>(example usesexample-app).
2. Install Datadog Agent
- Set environment variables for your Datadog API key, site, and environment:
export DD_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
export DD_SITE="datadoghq.com" # Use datadoghq.eu for EU, datadoghq.in for India
export DD_ENV=prod- Install Datadog Agent v7:
bash -c "$(curl -L https://install.datadoghq.com/scripts/install_script_agent7.sh)"- Verify installation:
sudo systemctl status datadog-agent
sudo datadog-agent status3. Configure Datadog Agent
- Edit the main configuration file:
sudo nano /etc/datadog-agent/datadog.yamlEnsure the following entries exist:
api_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
site: datadoghq.com
tags:
- env:prod
apm_config:
enabled: true
env: prod- Restart the agent:
sudo systemctl restart datadog-agent- Verify APM listener:
sudo datadog-agent status | grep -A10 "APM Agent"- Should show listener running on
127.0.0.1:8126.
4. Python Application Setup (example-app)
Directory structure example:
/home/ec2-user/example-app/
├── venv/
├── main.py
├── requirements.txt- Activate virtual environment and install dependencies:
source /home/ec2-user/example-app/venv/bin/activate
pip install ddtrace uvicorn fastapi5. Create Systemd Service for Python App
- Create the unit file
/etc/systemd/system/example-app.service:
[Unit]
Description=Example Python FastAPI Application
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/home/ec2-user/example-app
Environment=DD_AGENT_HOST=127.0.0.1
Environment=DD_SERVICE=example-app
Environment=DD_ENV=prod
Environment=DD_VERSION=1.0.0
Environment=DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES=true
ExecStart=/home/ec2-user/example-app/venv/bin/ddtrace-run /home/ec2-user/example-app/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target- Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable example-app
sudo systemctl start example-app
sudo journalctl -u example-app -f
ddtrace-runensures traces from the Python app are sent to the local Datadog Agent. Logs are captured in the systemd journal and can be viewed usingjournalctl.
6. Verification
-
Host Metrics:
- Go to Datadog Dashboard → Host Metrics
- Verify CPU, memory, disk, and network metrics from the EC2 instance.
-
APM Traces:
- Go to Datadog APM → Tracing Dashboard
- Requests to your FastAPI app should appear automatically with traces.
-
Agent Status:
sudo datadog-agent statusEnsure:
- Forwarder API key is valid.
- Core checks (system.cpu, system.mem, etc.) show OK.
- APM Agent listener is running.