

# Enable trace delivery

## Prerequisites
<a name="prerequisites"></a>

 **Turn on Transaction Search.** Spans are stored in the `aws/spans` log group of CloudWatch Transaction Search. Turn on Transaction Search once for your account in each Region where you use traces. For instructions on turning on Transaction Search, see [Enable Transaction Search](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Enable-TransactionSearch.html) in the *Amazon CloudWatch User Guide*.

 **Grant permissions.** To enable trace delivery, the user or role that calls `PutDeliverySource` (or turns on **Tracing** in the console) must have the `context:AllowVendedLogDeliveryForResource` permission for the Agent Context, in addition to the permissions required to deliver to AWS X-Ray. Callers of the operations don’t need this permission. For more information, including example IAM policies, see [Logging that requires additional permissions [V2]](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AWS-vended-logs-permissions-V2.html) in the *Amazon CloudWatch Logs User Guide*.

The following example policy grants the permission for an Agent Context:

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "context:AllowVendedLogDeliveryForResource",
            "Resource": "arn:aws:context:us-east-1:111122223333:agent-context/ac-1234567890abcdef"
        }
    ]
}
```

## Enable trace delivery using the console
<a name="enable-trace-delivery-using-the-console"></a>

1. Open the AWS Context console and choose your Agent Context.

1. On the **Integration** tab, turn on **Tracing**. AWS Context creates the trace delivery for the Agent Context.

## Enable trace delivery using the CloudWatch Logs API
<a name="enable-trace-delivery-using-the-cloudwatch-logs-api"></a>

To enable trace delivery using the CloudWatch Logs API, follow these steps:

1.  **Get the Amazon Resource Name (ARN) of your Agent Context.** Call the `GetAgentContext` API. An Agent Context ARN has the following format: `arn:aws:context:region:account-id:agent-context/agent-context-id `.

1.  **Call `PutDeliverySource`.** Use the [PutDeliverySource](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliverySource.html) API to create a delivery source for the Agent Context. Pass the Agent Context ARN as the `resourceArn` and specify `TRACES` as the `logType`.

   ```json
   {
       "name": "my-agent-context-traces",
       "resourceArn": "arn:aws:context:us-east-1:111122223333:agent-context/ac-1234567890abcdef",
       "logType": "TRACES"
   }
   ```

1.  **Call `PutDeliveryDestination`.** Use the [PutDeliveryDestination](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutDeliveryDestination.html) API with a `deliveryDestinationType` of `XRAY`. X-Ray is a managed destination, so you do not specify a destination resource ARN. Note the ARN that the response returns.

   ```json
   {
       "name": "my-xray-destination",
       "deliveryDestinationType": "XRAY"
   }
   ```

1.  **Call `CreateDelivery`.** Use the [CreateDelivery](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_CreateDelivery.html) API to link the delivery source to the X-Ray delivery destination.

   ```json
   {
       "deliverySourceName": "my-agent-context-traces",
       "deliveryDestinationArn": "arn:aws:logs:us-east-1:111122223333:delivery-destination:my-xray-destination"
   }
   ```

## View traces
<a name="traces-view"></a>

After you enable trace delivery, call one of the operations to generate spans. Then view the spans in either of these places:
+ The X-Ray trace map and trace details in the CloudWatch console, to see each span inside your agent’s trace.
+ Transaction Search, or CloudWatch Logs Insights on the `aws/spans` log group, to search and count spans by their fields. For examples, see [Example queries](traces-queries.md).