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:
- Install Datadog Agent v7:
- Verify installation:
3. Configure Datadog Agent
- Edit the main configuration file:
- Restart the agent:
- Verify APM listener:
- Should show listener running on
127.0.0.1:8126.
4. Python Application Setup (example-app)
Directory structure example:
- Activate virtual environment and install dependencies:
5. Create Systemd Service for Python App
- Create the unit file
/etc/systemd/system/example-app.service:
- Enable and start the service:
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:
Ensure:
- Forwarder API key is valid.
- Core checks (system.cpu, system.mem, etc.) show OK.
- APM Agent listener is running.
