Creating an AWS Lambda Function to Retrieve object wise QuickSight Folder.

4 minute read
Content level: Intermediate
0

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

  1. Login to AWS Management Console
  2. Navigate to AWS Lambda service
  3. Click "Create function"
  4. Select "Author from scratch"
  5. Enter function name (e.g., "QuickSightFolderRetriever")
  6. Choose "Python 3.9" runtime
  7. Select/create execution role
  8. Click "Create function"

2. Add Lambda Function Code

  1. 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
        }
    }
  1. Save changes

3. Test Lambda Function

  1. Click "Test" button
  2. Select "Create new test event"
  3. Name test event (e.g., "TestQuickSightFolderRetrieval")
  4. Click "Create"
  5. Run test and check "Execution result"

4. Modify Timeout (Optional)

  1. Go to "Configuration" tab
  2. Under "General configuration", click "Edit"
  3. Adjust "Timeout" value (e.g., 300 seconds)
  4. Save changes

5. Configure IAM Role

  1. Go to "Configuration" tab
  2. Click role name under "Execution role"
  3. Review role permissions
  4. Ensure these permissions exist:
    • quicksight:ListFolders
    • quicksight:ListFolderMembers
    • sts:GetCallerIdentity
  5. Add missing permissions if needed

6. Deploy Function

  1. 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"
        ]
      }
    }
  }
}
profile pictureAWS
EXPERT
published 5 days ago42 views