Skip to content

Instantly share code, notes, and snippets.

@vaquarkhan
Created May 27, 2024 06:42
Show Gist options
  • Save vaquarkhan/d9c1abd2a8410ef0b14712bfe3556339 to your computer and use it in GitHub Desktop.
Save vaquarkhan/d9c1abd2a8410ef0b14712bfe3556339 to your computer and use it in GitHub Desktop.
quicksight-info-lambda
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# Configure DynamoDB and QuickSight clients
dynamodb = boto3.resource('dynamodb')
quicksight_client = boto3.client('quicksight')
# Define table name (replace with your actual table name)
table_name = 'your_dashboard_inventory'
# Get all dashboards
try:
response = quicksight_client.list_dashboards(MaxResults=100)
dashboards = response['DashboardSummaryList']
while 'NextToken' in response:
response = quicksight_client.list_dashboards(NextToken=response['NextToken'], MaxResults=100)
dashboards.extend(response['DashboardSummaryList'])
except ClientError as e:
print(f"Error listing dashboards: {e}")
return
# Iterate through dashboards and get details
for dashboard in dashboards:
dashboard_id = dashboard['Arn'].split('/')[-1]
dashboard_name = dashboard['Name']
# Get analyses in the dashboard
try:
analyses_response = quicksight_client.list_analyses(DashboardId=dashboard_id)
analyses = analyses_response['AnalysisSummaryList']
except ClientError as e:
print(f"Error listing analyses for dashboard {dashboard_name}: {e}")
analyses = []
# Get datasets in the dashboard
try:
datasets_response = quicksight_client.list_data_sets(DashboardId=dashboard_id)
datasets = datasets_response['DataSetSummaries']
except ClientError as e:
print(f"Error listing datasets for dashboard {dashboard_name}: {e}")
datasets = []
# Prepare data for DynamoDB
item = {
'DashboardId': {'S': dashboard_id},
'DashboardName': {'S': dashboard_name},
'Analyses': {'L': [{'S': a['Arn']} for a in analyses]},
'Datasets': {'L': [{'S': d['Arn']} for d in datasets]}
}
# Insert data into DynamoDB
try:
table = dynamodb.Table(table_name)
table.put_item(Item=item)
except ClientError as e:
print(f"Error adding data to DynamoDB for dashboard {dashboard_name}: {e}")
return 'Inventory of QuickSight dashboards and datasets updated in DynamoDB'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment