# Fixing AWS Performance Insights API NotAuthorizedException Error: A Complete Guide

## **Problem Statement**

I'm encountering an error while running Performance Insights CLI commands or making API calls via Python. My account has the `AdministratorAccess` AWS-managed policy, and I can successfully execute CLI commands and API calls for other AWS services without issues.

When executing the following AWS CLI command:

```bash
aws pi get-resource-metrics \
    --service-type RDS \
    --identifier database-1 \
    --metric-queries '[{"Metric": "db.load.avg"}]' \
    --start-time $(date -u -d '-5 minutes' +%Y-%m-%dT%H:%M:%SZ) \
    --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
    --region us-west-1
```

The following error occurs:

```bash
An error occurred (NotAuthorizedException) when calling the GetResourceMetrics operation: The specified resource is not authorized for this account
```

## **Solution:-**

### **1\. Ensure Performance Insights is Enabled**

Run the following command to check if Performance Insights is enabled for your RDS instance:

```bash
aws rds describe-db-instances --db-instance-identifier database-1 --region us-west-1 --query 'DBInstances[0].PerformanceInsightsEnabled'
```

If the output is `false`, allow performance insights to for your RDS instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740726652884/0bc33d59-764e-44cb-8db1-69b5e2d2530f.png align="center")

### **2\. Check IAM Permissions**

Even with `AdministratorAccess`Ensure your IAM role has the necessary permissions:

```bash
aws iam list-attached-user-policies --user-name YOUR_USER_NAME
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740727172425/125bfab6-93ea-4dd4-be25-02610d787a18.png align="center")

### **3\. Get the** `dbiResourceId`

Retrieve the correct `dbiResourceId`:

```bash
aws rds describe-db-instances --db-instance-identifier database-1 --region us-west-1 --query 'DBInstances[0].DbiResourceId'
```

Use this `dbiResourceId` in the `--identifier` field of the `get-resource-metrics` command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740727302334/88791f42-4f8e-43be-ae8c-d90d757a19c0.png align="center")

### **4\.** Get Resource Metrics using AWS CLI

Replace the identifier `dbiResourceId`, then run the command to get performance insights.

```bash
aws pi get-resource-metrics \
--service-type RDS \
--identifier DBI_RESOURCE_ID \
--metric-queries '[{"Metric": "db.load.avg"}]' \
--start-time $(date -u -d '2 hours ago' +%Y-%m-%dT%H:%M:%SZ) \
--end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
--period-in-seconds 60 \
--region us-west-1
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740727962347/110cef6f-93d0-46ed-834e-325f3915d149.png align="center")

### **5\. Check Performance Insights Data Availability Using Python Script**

```bash
import boto3
from datetime import datetime, timedelta

def get_rds_performance_insights_metrics(db_instance_identifier, region_name, metric_name, period_seconds=60, minutes_ago=30):
    """
    Retrieves RDS Performance Insights metrics using the DbiResourceId.
    """
    try:
        rds_client = boto3.client('rds', region_name=region_name)
        pi_client = boto3.client('pi', region_name=region_name)

        response = rds_client.describe_db_instances(DBInstanceIdentifier=db_instance_identifier)
        dbi_resource_id = response['DBInstances'][0]['DbiResourceId']

        end_time = datetime.utcnow()
        start_time = end_time - timedelta(minutes=minutes_ago)

        response = pi_client.get_resource_metrics(
            ServiceType='RDS',
            Identifier=dbi_resource_id,
            MetricQueries=[{'Metric': metric_name}],
            StartTime=start_time,
            EndTime=end_time,
            PeriodInSeconds=period_seconds
        )

        return response

    except Exception as e:
        print(f"Error: {e}")
        return None

# Example usage
db_instance_id = 'database-1'
region = 'us-west-1'
metric = 'db.load.avg'

metrics_data = get_rds_performance_insights_metrics(db_instance_id, region, metric)

if metrics_data:
    print(metrics_data) #print the full response.
    for metric_list in metrics_data['MetricList']:
        print(f"Metric: {metric_list['Key']['Metric']}")
        #Correct the key to DataPoints.
        for data_point in metric_list['DataPoints']:
            timestamp = data_point['Timestamp']
            value = data_point.get('Value', None) #safely get value.
            print(f"  Timestamp: {timestamp}, Value: {value}")
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740733932507/d8d69f1c-714d-4976-ad76-c73ed45058f3.png align="center")

## **Conclusion**

Following these steps should help identify and resolve the issue with AWS Performance Insights API calls. If problems persist, check AWS CloudTrail logs for failed API calls or contact AWS Support.
