This document outlines the steps to create an AWS Lambda function that retrieves the list of folders and their associated objects (dashboards, analyses, and datasets) from Amazon QuickSight.
Prerequisites
- AWS account with permissions for Lambda functions, IAM roles, and QuickSight service
- Python 3.9 or compatible version.
Implementation Steps
1. Create AWS Lambda Function
- Login to AWS Management Console
- Navigate to AWS Lambda service
- Click "Create function"
- Select "Author from scratch"
- Enter function name (e.g., "QuickSightFolderRetriever")
- Choose "Python 3.9" runtime
- Select/create execution role
- Click "Create function"
2. Add Lambda Function Code
- In code editor, paste:
import boto3
def lambda_handler(event, context):
"""
AWS Lambda function that retrieves the list of folders and their associated objects from Amazon QuickSight.
This function uses the AWS SDK for Python (Boto3) to interact with the QuickSight API.
Parameters:
event (dict): The event object passed to the Lambda function.
context (object): The context object passed to the Lambda function.
Returns:
dict: A dictionary containing the account ID and the folder objects, organized by object type.
"""
# Set up the QuickSight client
quicksight = boto3.client('quicksight')
# Get the current AWS account ID
sts = boto3.client('sts')
account_id = sts.get_caller_identity()['Account']
# Get a list of all the folders in QuickSight
folders = []
next_token = None
while True:
try:
# Check if there is a NextToken available from the previous response
if next_token:
response = quicksight.list_folders(AwsAccountId=account_id, NextToken=next_token)
else:
# If there is no NextToken, make the initial call to list_folders()
response = quicksight.list_folders(AwsAccountId=account_id)
folders.extend(response['FolderSummaryList'])
next_token = response.get('NextToken')
if not next_token:
# If there is no NextToken, break out of the loop
break
except KeyError as e:
print(f"Error: {e}")
print(f"Response from list_folders(): {response}")
raise
except Exception as e:
print(f"An error occurred: {e}")
raise
# Create a dictionary to store the folders and their associated objects
folder_objects = {}
# Iterate through the folders and list the objects in each folder
for folder in folders:
folder_id = folder['FolderId']
folder_name = folder['Name']
objects = []
next_token = None
while True:
try:
# Check if there is a NextToken available from the previous response
if next_token:
response = quicksight.list_folder_members(AwsAccountId=account_id, FolderId=folder_id, NextToken=next_token)
else:
# If there is no NextToken, make the initial call to list_folder_members()
response = quicksight.list_folder_members(AwsAccountId=account_id, FolderId=folder_id)
objects.extend(response['FolderMemberList'])
next_token = response.get('NextToken')
if not next_token:
# If there is no NextToken, break out of the loop
break
except KeyError as e:
print(f"Error: {e}")
print(f"Response from list_folder_members(): {response}")
break
except Exception as e:
print(f"An error occurred: {e}")
break
# Add the objects to the folder_objects dictionary
for obj in objects:
if 'MemberArn' in obj:
member_arn = obj['MemberArn']
if 'dashboard' in member_arn:
obj_type = 'DASHBOARD'
elif 'analysis' in member_arn:
obj_type = 'ANALYSIS'
elif 'dataset' in member_arn:
obj_type = 'DATASET'
elif 'datasource' in member_arn:
obj_type = 'DATASOURCE'
else:
obj_type = 'UNKNOWN'
obj_id = obj['MemberId']
if obj_type not in folder_objects:
folder_objects[obj_type] = {obj_id: [folder_name]}
elif obj_id not in folder_objects[obj_type]:
folder_objects[obj_type][obj_id] = [folder_name]
else:
folder_objects[obj_type][obj_id].append(folder_name)
else:
print(f"Unexpected object structure: {obj}")
# Return the results
return {
'statusCode': 200,
'body': {
'account_id': account_id,
'folder_objects': folder_objects
}
}
- Save changes
3. Test Lambda Function
- Click "Test" button
- Select "Create new test event"
- Name test event (e.g., "TestQuickSightFolderRetrieval")
- Click "Create"
- Run test and check "Execution result"
4. Modify Timeout (Optional)
- Go to "Configuration" tab
- Under "General configuration", click "Edit"
- Adjust "Timeout" value (e.g., 300 seconds)
- Save changes
5. Configure IAM Role
- Go to "Configuration" tab
- Click role name under "Execution role"
- Review role permissions
- Ensure these permissions exist:
- quicksight:ListFolders
- quicksight:ListFolderMembers
- sts:GetCallerIdentity
- Add missing permissions if needed
6. Deploy Function
- Click "Deploy" button
Output JSON Sample.
{
"statusCode": 200,
"body": {
"account_id": "7*****76",
"folder_objects": {
"DATASET": {
"5a906*********e3bcee3": [
"test_shared"
],
"b84d***c7cd": [
"sfolder2"
]
},
"DASHBOARD": {
"986***d006b": [
"test_shared"
],
"3dba3***5595": [
"test_shared",
"sfolder2",
"sfolder3",
"new_shared"
],
"08db***ebb": [
"sfolder2"
]
},
"ANALYSIS": {
"ea345***3565e": [
"sfolder3"
]
}
}
}
}