> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trace0hq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python — Containers & VMs

> Instrument a Python application with OpenTelemetry and send traces, metrics, and logs to Trace0.

<Note>
  This guide covers Python applications running on **ECS**, **EC2**, **Kubernetes**, or any container/VM environment. For AWS Lambda, see the [Python Lambda guide](/setup/python/lambda).
</Note>

## 1. Install the OpenTelemetry SDK

```bash theme={null}
pip install opentelemetry-distro 
pip install opentelemetry-exporter-otlp
pip install opentelemetry-instrumentation-system-metrics
```

## 2. Install Instrumentation Libraries

```bash theme={null}
opentelemetry-bootstrap -a install
```

`opentelemetry-bootstrap` automatically detects your installed packages (Django, FastAPI, Boto3, etc.) and installs the matching instrumentation libraries.

## 3. Install the Resource Detector Package

Install the resource detector package for the platform your application is running on.

<Tabs>
  <Tab title="AWS">
    ```bash theme={null}
    pip install opentelemetry-sdk-extension-aws
    ```
  </Tab>

  <Tab title="Kubernetes">
    ```bash theme={null}
    pip install opentelemetry-resourcedetector-kubernetes
    ```
  </Tab>

  <Tab title="Containers">
    ```bash theme={null}
    pip install opentelemetry-resourcedetector-container
    ```
  </Tab>
</Tabs>

## 4. Set Environment Variables

Add these environment variables to your service. Replace `YOUR_TRACE0_ENV_API_KEY` with the API key from your Trace0 environment.

| Variable                          | Value                               |
| --------------------------------- | ----------------------------------- |
| `OTEL_EXPORTER_OTLP_ENDPOINT`     | `https://app.trace0hq.com/api`      |
| `OTEL_EXPORTER_OTLP_HEADERS`      | `X-API-KEY=YOUR_TRACE0_ENV_API_KEY` |
| `OTEL_EXPORTER_OTLP_PROTOCOL`     | `http/protobuf`                     |
| `OTEL_PYTHON_LOG_CORRELATION`     | `true`                              |
| `OTEL_PYTHON_LOG_CODE_ATTRIBUTES` | `true`                              |

If your application is running on AWS, set the following additional environment variable for your deployment platform.

<Tabs>
  <Tab title="ECS">
    | Variable                               | Value                         |
    | -------------------------------------- | ----------------------------- |
    | `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `env,host,os,process,aws_ecs` |
  </Tab>

  <Tab title="EKS">
    | Variable                               | Value                         |
    | -------------------------------------- | ----------------------------- |
    | `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `env,host,os,process,aws_eks` |
  </Tab>

  <Tab title="EC2">
    | Variable                               | Value                         |
    | -------------------------------------- | ----------------------------- |
    | `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `env,host,os,process,aws_ec2` |
  </Tab>

  <Tab title="Elastic Beanstalk">
    | Variable                               | Value                               |
    | -------------------------------------- | ----------------------------------- |
    | `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `env,host,os,process,aws_beanstalk` |
  </Tab>
</Tabs>

## 5. Kubernetes Setup

If your application runs on Kubernetes, including `Amazon EKS`, `Google Kubernetes Engine` or `Azure Kubernetes Service`, add the following environment variables to your container specification.

```yaml theme={null}
env:
  - name: K8S_POD_UID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid

  - name: K8S_POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name

  - name: K8S_NAMESPACE_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace

  - name: K8S_CONTAINER_NAME
    value: my-container-name

  - name: OTEL_RESOURCE_ATTRIBUTES
    value: k8s.pod.uid=$(K8S_POD_UID),k8s.pod.name=$(K8S_POD_NAME),k8s.namespace.name=$(K8S_NAMESPACE_NAME),k8s.container.name=$(K8S_CONTAINER_NAME)
```

## 6. Deploy Service

Deploy your service and watch traces, metrics, and logs appear in the [Trace0 dashboard](https://app.trace0hq.com) within seconds.

## Example Service

See our [Python FastAPI EC2 example app](https://github.com/Trace0-HQ/trace0-examples/tree/main/python-fast-api-ec2) for a working service you can deploy to your AWS account.
