How to get Overall or Total Purchased Spice Capacity of an Amazon Quicksight for a particular region with the help of Lambda function using Boto3/API

3 minute read
Content level: Intermediate
3

This article will provide step by step instructions to get Overall Purchased Spice capacity in Amazon QuickSight in programmatic way with the help of Lambda function

Introduction:

There are some cases where we need to fetch the overall purchased spice capacity of an Amazon Quicksight for a particular region, we usually navigate to manage Quicksight page and then we check the details under spice capacity manually.

This blog provides a solution to get the details of purchased spice capacity in programmatic way.

Solution:

  1. Open IAM console and click on Policies to create a policy as shown below
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:GetMetricData"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Policy details

  1. Next create a role which can be used by Lambda function as shown below and the attach the policy which was created in step1

Lambda-role

  1. Once role is created, open lambda service and then click on create function with runtime as Python 3.12 as shown below and select the role which was created in step2

Lambda-function

  1. Copy and paste the below code into the Lambda function
import boto3,os,json,sys,datetime,io
from datetime import datetime, date, time, timezone,timedelta

def lambda_handler(event, context): 
   
             aws_region = os.environ['AWS_REGION']
           
             client1 = boto3.client('quicksight',region_name = aws_region)
             client2 = boto3.client('sts')
             aws_account_id = client2.get_caller_identity()['Account']
             client3 = boto3.client('cloudwatch',region_name = aws_region)
           

             
             starttime = datetime.now()
             
             endtime = starttime - timedelta(days = 1000) 
             

             response = client3.get_metric_data(
               MetricDataQueries=[
                   {
                     'Id': 'cloudwatch',
                     'MetricStat': {
                         'Metric': {
                                  'Namespace': 'AWS/QuickSight',
                                   'MetricName': 'SPICECapacityLimitInMB',
            
                                    },
                         'Period': 3600,
                         'Stat': 'Maximum',
                         'Unit': 'Megabytes'
                                    },
            
                     'Label': 'cloudwatch',
                     'ReturnData': True,
                     'AccountId': aws_account_id
                   },
                   ],
                     StartTime=endtime,
                     EndTime=starttime)
             
             for i in response['MetricDataResults']:
              
              time = i['Timestamps'][0]
              SPICECapacityLimitInMB = i['Values'][0]
              if SPICECapacityLimitInMB == float(102400.0):
                
                SPICECapacityLimitInMB = '10240.0'
              else:
                SPICECapacityLimitInMB = SPICECapacityLimitInMB
                
              
              SpiceinGB = (SPICECapacityLimitInMB/1024)
              print("Total Spice capacity in",aws_region, "region as of", time,"is", SpiceinGB, "GB")


             return {
                    'statusCode': 200,
                     'body': json.dumps("successfully executed")}



  1. Run the Lambda function and we get sample output like below
Total Spice capacity in us-east-1 region as of 2024-07-05 06:35:00+00:00 is 40.0 GB

Tips:

  1. We can schedule this Lambda function to run periodically to pull the spice capacity data
  2. We can integrate with Simple Notification Service (SNS) to send alerts if purchased spice capacity exceeds certain threshold values accordingly.
  3. We can push the result into the S3 bucket and then we can use this data in Quicksight visuals with the help of S3 datasource.
  4. We can use Athena tables which can be created based on the S3 bucket data and then we can use Athena as a datasource in Quicskight to visualise the data.

Conclusion:

This blog provides the details of purchased spice capacity of a particular region in programmatic way and this data can be used to build dashboards in quicksight.

profile pictureAWS
EXPERT
published 3 months ago2267 views